- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# How does linear regression work with Tensorflow in Python?

Tensorflow is a machine learning framework that is provided by Google. It is an open−source framework used in conjunction with Python to implement algorithms, deep learning applications and much more. It is used in research and for production purposes. It has optimization techniques that help in performing complicated mathematical operations quickly. This is because it uses NumPy and multi−dimensional arrays. These multi−dimensional arrays are also known as ‘tensors’.

The framework supports working with deep neural network. It is highly scalable, and comes with many popular datasets. It uses GPU computation and automates the management of resources. It comes with multitude of machine learning libraries, and is well−supported and documented. The framework has the ability to run deep neural network models, train them, and create applications that predict relevant characteristics of the respective datasets.

The ‘tensorflow’ package can be installed on Windows using the below line of code −

pip install tensorflow

Tensor is a data structure used in TensorFlow. It helps connect edges in a flow diagram. This flow diagram is known as the ‘Data flow graph’. Tensors are nothing but multidimensional array or a list.

Following is an example −

## Example

def linear_reg(x): return A * x + b def mean_square_error(y_pred, y_true): return tf.reduce_mean(tf.square(y_pred - y_true)) optimizer = tf.optimizers.SGD(learning_rate) def run_optimization(): with tf.GradientTape() as g: pred = linear_reg(X) loss = mean_square_error(pred, Y) gradients = g.gradient(loss, [A, b]) optimizer.apply_gradients(zip(gradients, [A, b]))

Code credit − https://github.com/aymericdamien/TensorFlow-Examples/blob/master/tensorflow_v2/notebooks/2_BasicModels/linear_regression.ipynb

## Output

A linear regression function that is defined, is called on the data. Once the optimal data points have been computed, the mean square error function is calculated. The radient descent function is used to find the optimal weights. These values are displayed on the console.

## Explanation

The ‘weight’ and ‘bias’ values are randomly initialized. They will be updated to optimal values once the training is complete.

The general format for a linear equation is ‘Ax + b’ where ‘A’ is the ‘weight’ and ‘b’ is the ‘bias’ value.

Function to calculate the mean square error is defined.

The stochastic gradient descent optimizer is also defined.

A function for optimization is defined, that computes gradients and updates the value of weights and bias.

The data is trained for a specified number of steps.

- Related Articles
- How can Linear Regression be implemented using TensorFlow?
- Linear Regression using Python?
- Locally Weighted Linear Regression in Python
- Linear regression with Matplotlib/Numpy
- Explain how the logistic regression function works with Tensorflow?
- How can Tensorflow be used to work with character substring in Python?
- Linear Regression
- How does modulus work with complex numbers in Python?
- How can Logistic Regression be implemented using TensorFlow?
- Linear Regression using PyTorch?
- What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
- How does underscore "_" work in Python files?
- How does garbage collection work in Python?
- How does class inheritance work in Python?
- How does issubclass() function work in Python?