{"id":17238,"date":"2021-08-16T06:00:00","date_gmt":"2021-08-16T00:30:00","guid":{"rendered":"https:\/\/debuggercafe.com\/?p=17238"},"modified":"2024-09-15T20:21:28","modified_gmt":"2024-09-15T14:51:28","slug":"linear-regression-using-tensorflow-gradienttape","status":"publish","type":"post","link":"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/","title":{"rendered":"Linear Regression using TensorFlow GradientTape"},"content":{"rendered":"\n<p>In this tutorial, you are going to learn about <strong><em>linear regression using TensorFlow GradientTape<\/em><\/strong>. <\/p>\n\n\n\n<div class=\"wp-block-button is-style-outline center\"><a data-sumome-listbuilder-id=\"96503ff5-10a8-4ca0-b728-a5341bdd8116\" class=\"wp-block-button__link has-black-color has-luminous-vivid-orange-background-color has-text-color has-background\"><b>Download the Source Code for this Tutorial<\/b><\/a><\/div>\n\n\n\n<p>This blog post is the third in the series&nbsp;<strong><em>Getting Started with TensorFlow<\/em><\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/debuggercafe.com\/introduction-to-tensors-in-tensorflow\/\" target=\"_blank\" rel=\"noreferrer noopener\">Introduction to Tensors in TensorFlow<\/a><\/strong>.<\/li>\n\n\n\n<li><a href=\"https:\/\/debuggercafe.com\/basics-of-tensorflow-gradienttape\/\"><strong>Basics of TensorFlow GradientTape<\/strong><\/a>.<\/li>\n\n\n\n<li>Linear Regression using TensorFlow GradientTape.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>In the <strong><a href=\"https:\/\/debuggercafe.com\/basics-of-tensorflow-gradienttape\/\" target=\"_blank\" rel=\"noreferrer noopener\">previous post<\/a><\/strong>, we explored TensorFlow&#8217;s GradientTape API which helps us in carrying out automatic differentiation. If you are new to TensorFlow&#8217;s GradientTape API, then going through the previous post will surely help in understanding the concepts in this post.<\/p>\n\n\n\n<p>Also, if you are just starting with TensorFlow, then I suggest checking out the series from the <strong><a href=\"https:\/\/debuggercafe.com\/introduction-to-tensors-in-tensorflow\/\" target=\"_blank\" rel=\"noreferrer noopener\">beginning<\/a><em>.<\/em><\/strong> This you help you to get a clear picture of the tensors in TensorFlow.<\/p>\n\n\n\n<p>You must have carried out simple linear regression in machine learning. Maybe you used a well-known library like Scikit-Learn for that. But let&#8217;s change things a bit. We will not use Scikit-Learn in this tutorial. We will use the TensorFlow deep learning framework which is already very well known for its deep learning functionalities like image recognition, object detection, and image segmentation among many others. For carrying out simple linear regression, we will use its GradientTape API which was introduced starting from TensorFlow version 2.0 and now has become an integral part of the framework.<\/p>\n\n\n\n<p><strong><em>So, what are we going to cover in this tutorial?<\/em><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>We will start off with a bit of basics of linear regression and how to map the concepts to deep learning. This includes:<\/em>\n<ul class=\"wp-block-list\">\n<li><em>The simple linear linear regression equation.<\/em><\/li>\n\n\n\n<li><em>The weights and biases terms in linear regression.<\/em><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><em>Then we will start with the coding part of the tutorial. This is where we will use TensorFlow and it&#8217;s GradientTape API to solve a simple linear regression problem on a dummy dataset.<\/em><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>So, let&#8217;s start with the concept of linear regression.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A Brief About Linear Regression<\/h2>\n\n\n\n<p>This section will briefly cover the basics of linear regression and try to map the concepts to machine learning. After going through this section, we will be able to easily implement linear regression using the TensorFlow <strong><a href=\"https:\/\/www.tensorflow.org\/api_docs\/python\/tf\/GradientTape\" target=\"_blank\" rel=\"noreferrer noopener\">GradientTape<\/a><\/strong> API.<\/p>\n\n\n\n<p><em>In linear regression, we try to predict the values of a dependent variable using one or more independent variables. These independent variables are also called features.<\/em> <\/p>\n\n\n\n<p>When we deal with simple linear regression, then there is only one set of independent variables. This is what we will focus on in this tutorial.<\/p>\n\n\n\n<p>Let&#8217;s start out with the equation of simple linear regression.<\/p>\n\n\n<p>$$<br \/>\ny = mx + c<br \/>\n$$<\/p>\n\n\n\n<p>In the above equation:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\\(y\\) is the dependent variable.<\/li>\n\n\n\n<li>\\(x\\) is the independent variable or the feature.<\/li>\n\n\n\n<li>\\(m\\) and \\(c\\) are the coefficient and bias terms respectively. In terms of machine learning\/deep learning, we can also call  \\(m\\) as the <strong><em>weight vector<\/em><\/strong>.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>Moving ahead, let&#8217;s start calling \\(m\\) and \\(c\\) as \\(W\\) and \\(b\\) respectively.<\/p>\n\n\n\n<p>Our objective is to find the optimal values of \\(W\\) and \\(b\\) such that the difference between the actual \\(y\\) and predicted \\(y\\) (\\(\\hat{y}\\)) is minimum. For this, we will need a loss function or cost function which we need to minimize.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Loss Function<\/h3>\n\n\n\n<p>In simple linear regression, we often tend to use the Mean Square Error (MSE) loss function. It calculates the mean of the squared difference between \\(\\hat{y}\\) and \\(y\\).<\/p>\n\n\n<p>$$<br \/>\nMSE = \\frac{1}{n}\\sum_{i=1}^{n}(\\hat{y_i} &#8211; y)^2<br \/>\n$$<\/p>\n\n\n\n<p>The above is the formula for mean square error where \\(n\\) is the number of data points or the number of samples in the dataset.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Gradient Descent Optimization<\/h3>\n\n\n\n<p>By now, we know that we need to find the optimal values of \\(W\\) and \\(b\\) such that the actual and predicted values of the dependent variable will be as close as possible. This will in turn minimize the loss. For this, we use the <strong><em>Gradient Descent<\/em><\/strong> algorithm which tries to find the global minimum using the gradients of \\(W\\) and \\(b\\). With each iteration, we update the values of \\(W\\) and \\(b\\) by subtracting the gradients multiplied with the learning from current \\(W\\) and \\(b\\).<\/p>\n\n\n\n<p>The TensorFlow GradientTape API helps in this regard. Everything will become clear when we start to actually code through the tutorial.<\/p>\n\n\n\n<p>We will stop the theoretical concepts of linear regression here. Let&#8217;s move on to check the directory structure for this tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Directory Structure<\/h2>\n\n\n\n<p>The following block shows the directory structure for this simple project.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u251c\u2500\u2500 linear_regression_using_gradienttape.ipynb<\/pre>\n\n\n\n<p>We have just one notebook file, that is, <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">linear_regression_using_gradienttape.ipynb<\/code> for this tutorial. If you are coding along while following the tutorial, then I recommend using Jupyter Notebook or Jupyter Lab so that you can visualize each of the outputs while we move forward.<\/p>\n\n\n\n<p>Let&#8217;s start with the coding part of the tutorial without any further delay.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Linear Regression using TensorFlow GradientTape<\/h2>\n\n\n\n<p>Starting with the required imports.<\/p>\n\n\n\n<div class=\"wp-block-button is-style-outline center\"><a data-sumome-listbuilder-id=\"96503ff5-10a8-4ca0-b728-a5341bdd8116\" class=\"wp-block-button__link has-black-color has-luminous-vivid-orange-background-color has-text-color has-background\"><b>Download the Source Code for this Tutorial<\/b><\/a><\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import tensorflow as tf\nimport numpy as np\nimport matplotlib.pyplot as plt<\/pre>\n\n\n\n<p>Along with TensorFlow and NumPy, we are also importing Matplotlib to the plotting of graphs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Learning Parameters and Hyperparameters<\/h3>\n\n\n\n<p>Now, let&#8217;s define some of the learning parameters and hyperparameters that we will use along the way.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># number of training samples to create\nn_samples = 10000\n# `m` and `c` are coefficient and bias to get the initial `y`\nm = 9\nc = -2\nmean = 0.0 # mean of the training data distribution to create\nstd = 1.0 # standard deviation of the of the training data distribution to create\n# number of training epochs \nnum_epochs = 3000\n# learning rate\nlearning_rate = 0.001<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We will create 10000 sample data points for training and testing our linear regression model.<\/li>\n\n\n\n<li>The coeffcient <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">m<\/code> and bias <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">c<\/code> are 9 and -2 respectively. We will use these values to get the intial <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">y<\/code> (actual values) using the equation \\(y = mx + c\\).<\/li>\n\n\n\n<li>Then we define the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">mean<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">std<\/code> (standard deviation) for the training data distribution. They are 0.0 and 1.0 respectivly.<\/li>\n\n\n\n<li>The number of epochs is 3000, that is the number of times we will update the values weight and bias values to reach an optimal solution.<\/li>\n\n\n\n<li>Finally, the learning rate is 0.001.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create the Sample Dataset<\/h3>\n\n\n\n<p>Here, we will create the sample dataset to be used for training. The following code block contains the function to generate the training data.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def create_dataset(n_samples, m, c):\n    # create the sample dataset\n    x = np.random.normal(mean, std, n_samples)\n    random_noise = np.random.normal(mean, std, n_samples)\n    y = m*x + c + random_noise\n    x_train, y_train = x[:8000], y[:8000]\n    x_test, y_test = x[8000:], y[8000:]\n    return x_train, y_train, x_test, y_test<\/pre>\n\n\n\n<p>The <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">create_dataset()<\/code> function accepts the number of samples to generate (<code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">n_samples<\/code>), the coefficient <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">m<\/code>, and bias <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">c<\/code> as input parameters.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, we generate the whole 10000 random data points from a normal Gaussian distribution using mean of 0.0 and standard deviation of 1.0. This we store in a NumPy array, <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">x<\/code>.<\/li>\n\n\n\n<li>Then we create a <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">random_noise<\/code> array using the same method.<\/li>\n\n\n\n<li>We use the generated <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">x<\/code>, <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">random_noise<\/code>, and the default <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">m<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">c<\/code> to generate the actual <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">y<\/code> values.<\/li>\n\n\n\n<li>From the 10000 data points, we split that into 8000 training samples, and 2000 test samples. Those are stored in <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">x_train<\/code>, <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">y_train<\/code>, and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">x_test<\/code>, <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">y_test<\/code> respectively.<\/li>\n\n\n\n<li>Finally, we return the training and test samples.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">x_train, y_train, x_test, y_test = create_dataset(n_samples, m, c)\nprint(f\"Training samples: {len(x_train)}\")\nprint(f\"Test samples: {len(x_test)}\")\nplt.figure(figsize=(12, 9))\nplt.scatter(x_train, y_train)\nplt.xlabel('x_train')\nplt.ylabel('y_train')\nplt.title('Training data distribution')\nplt.savefig('training_data.jpg')<\/pre>\n\n\n\n<p>In the above code block, we call the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">create_dataset()<\/code> function to get the training and test samples. Then we print the number of samples in the training and test set and plot the training data distribution using Matplotlib.<\/p>\n\n\n\n<p>The following is output from the above block.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Training samples: 8000\nTest samples: 2000<\/pre>\n\n\n\n<p>And now, the data distribution plot.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/linear-regression-training-data-distribution.jpg\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"864\" height=\"648\" src=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/linear-regression-training-data-distribution.jpg\" alt=\"The training data distribution for our linear regression training using TensorFlow.\" class=\"wp-image-17336\" srcset=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/linear-regression-training-data-distribution.jpg 864w, https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/linear-regression-training-data-distribution-300x225.jpg 300w, https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/linear-regression-training-data-distribution-768x576.jpg 768w\" sizes=\"auto, (max-width: 864px) 100vw, 864px\" \/><\/a><figcaption class=\"wp-element-caption\"><strong>Figure 1. Scatter plot for the training data distribution<\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p>As we can see, the distribution is pretty linear and our linear regression model might be able to learn it pretty easily. Our main objective is to learn about the concept of linear regression using the TensorFlow GradientTape API. So, it does not matter much whether our data distribution is difficult or easy to learn.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Initialize Random Weight and Bias Values<\/h3>\n\n\n\n<p>The next block contains the code for initializing random values for weight and bias.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># random initial values for weights\nW = tf.Variable(np.random.randn())\nB = tf.Variable(np.random.randn())\nprint(W)\nprint(B)<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;tf.Variable 'Variable:0' shape=() dtype=float32, numpy=1.4115247>\n&lt;tf.Variable 'Variable:0' shape=() dtype=float32, numpy=-0.7143893><\/pre>\n\n\n\n<p>Our <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code> values have been initialized to values very close to zero to start out with. With each training epoch, we will keep updating these weight and bias values so that they can actually start to match the initial m and c, that is, 9 and -2 respectively.<\/p>\n\n\n\n<p>It is also important to note that <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code> should be <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">tf.Variable<\/code> and not <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">tf.contant<\/code> tensors. This ensures that the values can be changed and tracked during runtime operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">A Few Helper Functions<\/h3>\n\n\n\n<p>In this section, we will write a few helper functions that will make our work easier while running the training loop. These are mainly functions to calculate the predictions, the loss, and the gradients of <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code>.<\/p>\n\n\n\n<p>Let&#8217;s start with the prediction function.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def pred(x, W, B):\n    \"\"\"\n    calculate y_hat = m*x + c with learned weights and biases.\n    y_hat is the prediction\n    \"\"\"\n    y_hat = W*x + B\n    return y_hat<\/pre>\n\n\n\n<p>The <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">pred()<\/code> function accepts the training data points <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">x<\/code>, the weights <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code>, and the bias <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code> as the parameters. Then we calculate the values of predicted <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">y<\/code> using the formula <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">y_hat = W*x + B<\/code> and simply return the predicted value. This predicted <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">y_hat<\/code> value is the \\(\\hat{y}\\) that we discussed earlier.<\/p>\n\n\n\n<p>Next, the function to calculate the loss value, which is the Mean Squared Error (MSE) loss.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def loss(x, y, W, B):\n    \"\"\"\n    Mean squared error loss function\n    \"\"\"\n    prediction = pred(x, W, B)\n    squared_error = tf.square(prediction - y)\n    # finally calculate the MSE (Mean Sqaured Error)\n    mse = tf.reduce_mean(squared_error)\n    return mse<\/pre>\n\n\n\n<p>The <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">loss()<\/code> function accepts <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">x<\/code>, the original dependent variable <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">y<\/code>, the weight <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code>, and the bias <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, we call the pred function to get the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">prediction<\/code>, that is, \\(\\hat{y}\\).<\/li>\n\n\n\n<li>Then we use the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">tf.square()<\/code> function to get the squared difference between the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">prediction<\/code> and actual <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">y<\/code>.<\/li>\n\n\n\n<li>Finally, we calculate the MSE using <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">tf.reduce_mean()<\/code> function and return the value.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>The final helper function is to calculate the gradients of <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def calculate_gradient(x, y, W, B):\n    \"\"\"\n    Calculate the derivative of the loss\n    \"\"\"\n    with tf.GradientTape() as tape:\n        loss_value = loss(x, y, W, B)\n        \n    w_grad, b_grad = tape.gradient(loss_value, [W, B])\n    return w_grad, b_grad<\/pre>\n\n\n\n<p>The <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">calculate_gradient()<\/code> function also accepts <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">x<\/code>, the original dependent variable <code><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">y<\/code><\/code>, the weight <code><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code><\/code>, and the bias <code><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code><\/code> as the parameters.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We call the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">loss()<\/code> function within the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">tf.GradientTape()<\/code> context and store it in <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">loss_value<\/code>. This ensures that all the operations on <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">loss_value<\/code> are tracked and recorded so that we use it to calculate the gradients.<\/li>\n\n\n\n<li>Next, we use <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">gradient()<\/code> function to get the gradients of <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code> as <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">w_grad<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">b_grad<\/code> using the loss value. Then we return the two gradient values.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>This is all we need for the helper functions. Now, let&#8217;s move on to the actual training part.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Training<\/h3>\n\n\n\n<p>As defined at the beginning of the coding section, we will train for 3000 epochs. The following are the steps that we will follow. <\/p>\n\n\n\n<p><strong><em>For each training epoch:<\/em><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We will calculate the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">y_hat<\/code> using training data point <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">x<\/code>, <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code>, and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code>.<\/li>\n\n\n\n<li>Then we will calculate the MSE using actual <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">y<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">y_hat<\/code>.<\/li>\n\n\n\n<li>Then the gradients, <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">w_grad<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">b_grad<\/code> get calculated.<\/li>\n\n\n\n<li>We compute the change that we want to subtract from current <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code> by multiplying the learning rate with <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">w_grad<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">b_grad<\/code>.<\/li>\n\n\n\n<li>Finally, we update the values of <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code> to new ones.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>The following code block contains the code which will make things clearer.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for epoch in range(num_epochs):\n    w_grad, b_grad = calculate_gradient(x_train, y_train, W, B)\n    \n    dW, dB = w_grad * learning_rate, b_grad * learning_rate\n    \n    W.assign_sub(dW)\n    B.assign_sub(dB)\n    \n    if epoch % 10 == 0:\n        print(f\"Epoch: {epoch}, loss {loss(x_train, y_train, W, B):.3f}\")<\/pre>\n\n\n\n<p>In the above block:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">dW<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">dB<\/code> are the change in <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code> that we get by multiplying the learning rate with the respective gradients.<\/li>\n\n\n\n<li><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W.assign_sub(dW)<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B.assign_sub(dB)<\/code> are the two operations where we subtract the change and assign the new values for <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code>. This is where the weight and bias get updated.<\/li>\n\n\n\n<li>Then we print the loss values after every 10 epochs.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>Running the above code block will give output similar to the following.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Epoch: 0, loss 51.641\nEpoch: 10, loss 49.641\nEpoch: 20, loss 47.720\nEpoch: 30, loss 45.875\nEpoch: 40, loss 44.103\nEpoch: 50, loss 42.401\nEpoch: 60, loss 40.766\nEpoch: 70, loss 39.195\nEpoch: 80, loss 37.687\nEpoch: 90, loss 36.238\nEpoch: 100, loss 34.847\n...\nEpoch: 2970, loss 1.004\nEpoch: 2980, loss 1.004\nEpoch: 2990, loss 1.004<\/pre>\n\n\n\n<p>The loss started pretty high, around 51.6. But by the end of the training, it has dropped almost near to 1. This shows that our training is working and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code> are getting properly updated with each epoch.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Check the Final W and B Values<\/h3>\n\n\n\n<p>Let&#8217;s check the final <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code> values after the training. If the learning has actually happened, then they should be very near to the initial <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">m<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">c<\/code>, which were 9 and -2 respectively.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># check whether we get the desired `m` and `c` or not\nprint(f\"Learned W: {W.numpy()}, learned B: {B.numpy()}\")<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Learned W: 8.967373847961426, learned B: -2.011993169784546<\/pre>\n\n\n\n<p>As we can see, <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code> is 8.967 and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code> is -2.011. This looks pretty good.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Testing on the Test Set<\/h3>\n\n\n\n<p>If you remember, then we also have one test set, that is, <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">x_test<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">y_test<\/code>. We will try to predict \\(\\hat{y}\\) on this set. But before that, let&#8217;s check out the distribution of the test set.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># plot the test set\nplt.figure(figsize=(12, 9))\nplt.scatter(x_test, y_test)\nplt.xlabel('x_test')\nplt.ylabel('y_test')\nplt.title('Test data distribution')\nplt.savefig('test_data.jpg')<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/linear-regression-test-data-distribution.jpg\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"864\" height=\"648\" src=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/linear-regression-test-data-distribution.jpg\" alt=\"The test data distribution for linear regression testing using TensorFlow.\" class=\"wp-image-17338\" srcset=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/linear-regression-test-data-distribution.jpg 864w, https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/linear-regression-test-data-distribution-300x225.jpg 300w, https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/linear-regression-test-data-distribution-768x576.jpg 768w\" sizes=\"auto, (max-width: 864px) 100vw, 864px\" \/><\/a><figcaption class=\"wp-element-caption\"><strong>Figure 2. Scatter plot for the test data distribution.<\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p>The test scatter plot looks almost similar to the training scatter plot as the data is from the same distribution only.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Check the Final Loss on the Test Set <\/h3>\n\n\n\n<p>Let&#8217;s see what we get when trying to calculate the final loss on the test set.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">test_loss = loss(x_test, y_test, W, B)\nprint(f\"Test loss: {test_loss:.3f}\")<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Test loss: 0.928<\/pre>\n\n\n\n<p>We are getting the loss value as 0.928 which is very near to what we got for the training data by the end of the training epochs.<\/p>\n\n\n\n<p>Finally, let&#8217;s calculate the \\(\\hat{y}\\) on the test data and see if we are actually able to fit on the test data or not.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># predicted y on on the test set\ny_test_predicted = W.numpy()*x_test + B.numpy()\n# plot the predicted y values on the test data distribution\nplt.figure(figsize=(12, 9))\nplt.plot(x_test, y_test_predicted, c='red')\nplt.scatter(x_test, y_test)\nplt.xlabel('x_test')\nplt.ylabel('y_test')\nplt.title('Test data distribution and predicted y plot')\nplt.savefig('test_data_with_predicted_y_values.jpg')<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/test-data-with-predicted-y-plot-for-linear-regression.jpg\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"864\" height=\"648\" src=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/test-data-with-predicted-y-plot-for-linear-regression.jpg\" alt=\"Linear regression using the TensorFlow GradientTape API.\" class=\"wp-image-17340\" srcset=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/test-data-with-predicted-y-plot-for-linear-regression.jpg 864w, https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/test-data-with-predicted-y-plot-for-linear-regression-300x225.jpg 300w, https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/test-data-with-predicted-y-plot-for-linear-regression-768x576.jpg 768w\" sizes=\"auto, (max-width: 864px) 100vw, 864px\" \/><\/a><figcaption class=\"wp-element-caption\"><strong>Figure 3. The test data distribution along with the line plot for the predicted target values. <\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p>The predicted plot (red line) looks pretty good actually. It is almost able to fit in the test data. Looks like our linear regression model has found very optimized values <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">W<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">B<\/code>. Although it must be easy for the model to predict the values as the distribution of test data is the same as the training data. Still, it is good enough, for now, to learn the concepts of simple linear regression using TensorFlow&#8217;s GradientTape API.<\/p>\n\n\n\n<p>With this, we have completed the coding part of this tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary and Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we covered linear regression using TensorFlow&#8217;s GradientTape API. We did very basic training on a simple dummy dataset. We used a simple linear regression model with only one dependent feature vector. And we tried to predict the dependent values while trying to optimize the weight and bias values. But this should be enough to get someone started with the concepts if using the GradientTape API on other complex datasets as well. I hope that you learned something new in this tutorial.<\/p>\n\n\n\n<p>If you have any doubts, thoughts, or suggestions, then please leave them in the comment section. I will surely address them.<\/p>\n\n\n\n<p>You can contact me using the <strong><a aria-label=\"Contact (opens in a new tab)\" href=\"https:\/\/debuggercafe.com\/contact-us\/\" target=\"_blank\" rel=\"noreferrer noopener\">Contact<\/a><\/strong> section. You can also find me on <strong><a aria-label=\"LinkedIn (opens in a new tab)\" href=\"https:\/\/www.linkedin.com\/in\/sovit-rath\/\" target=\"_blank\" rel=\"noreferrer noopener\">LinkedIn<\/a><\/strong>, and <strong><a href=\"https:\/\/x.com\/SovitRath5\" target=\"_blank\" rel=\"noreferrer noopener\">X<\/a><\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to carry out simple linear regression using the TensorFlow GradientTape API.<\/p>\n","protected":false},"author":1,"featured_media":17342,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59,54],"tags":[196,61,197,55,195,192,194],"class_list":["post-17238","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-deep-learning","category-tensorflow","tag-beginner-tensorflow","tag-deep-learning","tag-linear-regression","tag-tensorflow","tag-tensorflow-gradienttape-api","tag-tf-gradienttape","tag-tf-variable"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Linear Regression using TensorFlow GradientTape<\/title>\n<meta name=\"description\" content=\"Learn how to carry out simple linear regression using the TensorFlow GradientTape API. Linear Regression using TensorFlow GradientTape\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linear Regression using TensorFlow GradientTape\" \/>\n<meta property=\"og:description\" content=\"Learn how to carry out simple linear regression using the TensorFlow GradientTape API. Linear Regression using TensorFlow GradientTape\" \/>\n<meta property=\"og:url\" content=\"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/\" \/>\n<meta property=\"og:site_name\" content=\"DebuggerCafe\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/profile.php?id=100013731104496\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-16T00:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-15T14:51:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/Linear-Regression-using-TensorFlow-GradientTape-e1627261511126.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"563\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sovit Ranjan Rath\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SovitRath5\" \/>\n<meta name=\"twitter:site\" content=\"@SovitRath5\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sovit Ranjan Rath\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/\"},\"author\":{\"name\":\"Sovit Ranjan Rath\",\"@id\":\"https:\/\/debuggercafe.com\/#\/schema\/person\/27719b14d930bd4a88ade40d18b0a752\"},\"headline\":\"Linear Regression using TensorFlow GradientTape\",\"datePublished\":\"2021-08-16T00:30:00+00:00\",\"dateModified\":\"2024-09-15T14:51:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/\"},\"wordCount\":2179,\"commentCount\":5,\"image\":{\"@id\":\"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/Linear-Regression-using-TensorFlow-GradientTape-e1627261511126.jpg\",\"keywords\":[\"Beginner TensorFlow\",\"Deep Learning\",\"Linear Regression\",\"TensorFlow\",\"TensorFlow GradientTape API\",\"tf.GradientTape\",\"tf.Variable\"],\"articleSection\":[\"Deep Learning\",\"TensorFlow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/\",\"url\":\"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/\",\"name\":\"Linear Regression using TensorFlow GradientTape\",\"isPartOf\":{\"@id\":\"https:\/\/debuggercafe.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/Linear-Regression-using-TensorFlow-GradientTape-e1627261511126.jpg\",\"datePublished\":\"2021-08-16T00:30:00+00:00\",\"dateModified\":\"2024-09-15T14:51:28+00:00\",\"author\":{\"@id\":\"https:\/\/debuggercafe.com\/#\/schema\/person\/27719b14d930bd4a88ade40d18b0a752\"},\"description\":\"Learn how to carry out simple linear regression using the TensorFlow GradientTape API. Linear Regression using TensorFlow GradientTape\",\"breadcrumb\":{\"@id\":\"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/#primaryimage\",\"url\":\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/Linear-Regression-using-TensorFlow-GradientTape-e1627261511126.jpg\",\"contentUrl\":\"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/Linear-Regression-using-TensorFlow-GradientTape-e1627261511126.jpg\",\"width\":1000,\"height\":563,\"caption\":\"Linear Regression using TensorFlow GradientTape\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/debuggercafe.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linear Regression using TensorFlow GradientTape\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/debuggercafe.com\/#website\",\"url\":\"https:\/\/debuggercafe.com\/\",\"name\":\"DebuggerCafe\",\"description\":\"Machine Learning and Deep Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/debuggercafe.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/debuggercafe.com\/#\/schema\/person\/27719b14d930bd4a88ade40d18b0a752\",\"name\":\"Sovit Ranjan Rath\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/debuggercafe.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f71ca13ec56d630e7d8045e8b846396068791aa204936c3d74d721c6dd2b4d3c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f71ca13ec56d630e7d8045e8b846396068791aa204936c3d74d721c6dd2b4d3c?s=96&d=mm&r=g\",\"caption\":\"Sovit Ranjan Rath\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linear Regression using TensorFlow GradientTape","description":"Learn how to carry out simple linear regression using the TensorFlow GradientTape API. Linear Regression using TensorFlow GradientTape","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/","og_locale":"en_US","og_type":"article","og_title":"Linear Regression using TensorFlow GradientTape","og_description":"Learn how to carry out simple linear regression using the TensorFlow GradientTape API. Linear Regression using TensorFlow GradientTape","og_url":"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/","og_site_name":"DebuggerCafe","article_publisher":"https:\/\/www.facebook.com\/profile.php?id=100013731104496","article_published_time":"2021-08-16T00:30:00+00:00","article_modified_time":"2024-09-15T14:51:28+00:00","og_image":[{"width":1000,"height":563,"url":"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/Linear-Regression-using-TensorFlow-GradientTape-e1627261511126.jpg","type":"image\/jpeg"}],"author":"Sovit Ranjan Rath","twitter_card":"summary_large_image","twitter_creator":"@SovitRath5","twitter_site":"@SovitRath5","twitter_misc":{"Written by":"Sovit Ranjan Rath","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/#article","isPartOf":{"@id":"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/"},"author":{"name":"Sovit Ranjan Rath","@id":"https:\/\/debuggercafe.com\/#\/schema\/person\/27719b14d930bd4a88ade40d18b0a752"},"headline":"Linear Regression using TensorFlow GradientTape","datePublished":"2021-08-16T00:30:00+00:00","dateModified":"2024-09-15T14:51:28+00:00","mainEntityOfPage":{"@id":"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/"},"wordCount":2179,"commentCount":5,"image":{"@id":"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/#primaryimage"},"thumbnailUrl":"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/Linear-Regression-using-TensorFlow-GradientTape-e1627261511126.jpg","keywords":["Beginner TensorFlow","Deep Learning","Linear Regression","TensorFlow","TensorFlow GradientTape API","tf.GradientTape","tf.Variable"],"articleSection":["Deep Learning","TensorFlow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/","url":"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/","name":"Linear Regression using TensorFlow GradientTape","isPartOf":{"@id":"https:\/\/debuggercafe.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/#primaryimage"},"image":{"@id":"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/#primaryimage"},"thumbnailUrl":"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/Linear-Regression-using-TensorFlow-GradientTape-e1627261511126.jpg","datePublished":"2021-08-16T00:30:00+00:00","dateModified":"2024-09-15T14:51:28+00:00","author":{"@id":"https:\/\/debuggercafe.com\/#\/schema\/person\/27719b14d930bd4a88ade40d18b0a752"},"description":"Learn how to carry out simple linear regression using the TensorFlow GradientTape API. Linear Regression using TensorFlow GradientTape","breadcrumb":{"@id":"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/#primaryimage","url":"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/Linear-Regression-using-TensorFlow-GradientTape-e1627261511126.jpg","contentUrl":"https:\/\/debuggercafe.com\/wp-content\/uploads\/2021\/07\/Linear-Regression-using-TensorFlow-GradientTape-e1627261511126.jpg","width":1000,"height":563,"caption":"Linear Regression using TensorFlow GradientTape"},{"@type":"BreadcrumbList","@id":"https:\/\/debuggercafe.com\/linear-regression-using-tensorflow-gradienttape\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/debuggercafe.com\/"},{"@type":"ListItem","position":2,"name":"Linear Regression using TensorFlow GradientTape"}]},{"@type":"WebSite","@id":"https:\/\/debuggercafe.com\/#website","url":"https:\/\/debuggercafe.com\/","name":"DebuggerCafe","description":"Machine Learning and Deep Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/debuggercafe.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/debuggercafe.com\/#\/schema\/person\/27719b14d930bd4a88ade40d18b0a752","name":"Sovit Ranjan Rath","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/debuggercafe.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f71ca13ec56d630e7d8045e8b846396068791aa204936c3d74d721c6dd2b4d3c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f71ca13ec56d630e7d8045e8b846396068791aa204936c3d74d721c6dd2b4d3c?s=96&d=mm&r=g","caption":"Sovit Ranjan Rath"}}]}},"_links":{"self":[{"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/posts\/17238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/comments?post=17238"}],"version-history":[{"count":120,"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/posts\/17238\/revisions"}],"predecessor-version":[{"id":38013,"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/posts\/17238\/revisions\/38013"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/media\/17342"}],"wp:attachment":[{"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/media?parent=17238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/categories?post=17238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/debuggercafe.com\/wp-json\/wp\/v2\/tags?post=17238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}