How does linear regression work with Tensorflow in Python?

PythonServer Side ProgrammingProgramming

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 −


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 −


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.


  • 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.

Published on 19-Jan-2021 13:38:03