- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 Questions & Answers
- How can Linear Regression be implemented using TensorFlow?
- Linear Regression using Python?
- Linear Regression
- Linear regression with Matplotlib/Numpy
- Linear Regression using PyTorch?
- Explain how the logistic regression function works with Tensorflow?
- How does modulus work with complex numbers in Python?
- How does Python while loop work?
- How can Tensorflow be used to work with character substring in Python?
- How does tuple comparison work in Python?
- How does mkdir -p work in Python?
- How does ++ and -- operators work in Python?
- How does garbage collection work in Python?
- How does class inheritance work in Python?
- How does issubclass() function work in Python?