

{"id":111122,"date":"2023-01-09T10:00:41","date_gmt":"2023-01-09T04:30:41","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=111122"},"modified":"2023-01-09T10:40:07","modified_gmt":"2023-01-09T05:10:07","slug":"pytorch-linear-regression","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/","title":{"rendered":"PyTorch Linear Regression"},"content":{"rendered":"<p>Linear Regression is an ML algorithm that tries to find a linear relationship between the inputs and the outputs of the training set. Then, based on this learned information, it tries to predict the output of the test dataset. It is widely used in prediction models, such as housing prices prediction models, stock prices prediction models etc., when we have some knowledge about the past and the prediction we have to make is a real number.<\/p>\n<h3>What is Linear Regression?<\/h3>\n<p>In linear regression, our job is to fit the given data into a straight line. The equation of a line will be y=mx+c. Here, y is the dependent, and x is the independent variable of that data.<\/p>\n<p>Our task is to find the parameters m and c. Once we have computed m and c, we can predict the output for any new input.<\/p>\n<p>In machine learning, we replace the parameters c and m with \u04e80 and \u04e81. Now the equation becomes y=\u04e80 + \u04e81x. The advantage of this representation is that the parameters and the independent variable can be represented as vectors.<\/p>\n<p><span style=\"font-weight: 400\">x=[1\u00a0 x]<\/span><sup><span style=\"font-weight: 400\">T<\/span><\/sup><span style=\"font-weight: 400\"> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400\">\u04e8=[<\/span><span style=\"font-weight: 400\">\u04e8<\/span><sub><span style=\"font-weight: 400\">0<\/span><\/sub><span style=\"font-weight: 400\"> \u00a0 <\/span><span style=\"font-weight: 400\">\u04e8<\/span><sub><span style=\"font-weight: 400\">1<\/span><\/sub><span style=\"font-weight: 400\">]<\/span><sup><span style=\"font-weight: 400\">T<\/span> <\/sup><\/p>\n<p>And the equation of a line can be written as y=\u04e8.x.<\/p>\n<p>If there are more than one independent variable, this representation can be extended.<\/p>\n<p><span style=\"font-weight: 400\">x=[<\/span><span style=\"font-weight: 400\">x<\/span><sub><span style=\"font-weight: 400\">1<\/span><\/sub><span style=\"font-weight: 400\">\u00a0 <\/span><span style=\"font-weight: 400\">x<\/span><sub><span style=\"font-weight: 400\">2<\/span><\/sub><span style=\"font-weight: 400\">\u00a0 <\/span><span style=\"font-weight: 400\">x<\/span><sub><span style=\"font-weight: 400\">3<\/span><\/sub><span style=\"font-weight: 400\">\u00a0 &#8230;&#8230;&#8230;..\u00a0 <\/span><span style=\"font-weight: 400\">x<\/span><sub><span style=\"font-weight: 400\">n<\/span><\/sub><span style=\"font-weight: 400\">]<\/span><sup><span style=\"font-weight: 400\">T<\/span><\/sup><span style=\"font-weight: 400\"> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u04e8=[<\/span><span style=\"font-weight: 400\">\u04e8<\/span><span style=\"font-weight: 400\"><sub>1<\/sub>\u00a0 <\/span><span style=\"font-weight: 400\">\u04e8<\/span><sub><span style=\"font-weight: 400\">2<\/span><\/sub><span style=\"font-weight: 400\">\u00a0 <\/span><span style=\"font-weight: 400\">\u04e8<\/span><sub><span style=\"font-weight: 400\">3<\/span><\/sub><span style=\"font-weight: 400\">\u00a0 &#8230;&#8230;&#8230;&#8230;&#8230;.\u00a0 <\/span><span style=\"font-weight: 400\">\u04e8<\/span><sub><span style=\"font-weight: 400\">n<\/span><\/sub><span style=\"font-weight: 400\">]<\/span><sup><span style=\"font-weight: 400\">T<\/span><\/sup><span style=\"font-weight: 400\"> ,here <\/span><span style=\"font-weight: 400\">x<\/span><sub><span style=\"font-weight: 400\">1<\/span><\/sub><span style=\"font-weight: 400\">=1<\/span><\/p>\n<p>And the equation of the line remains the same.<\/p>\n<p><span style=\"font-weight: 400\"> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 y=\u04e8\u00a0 . x<\/span><\/p>\n<p>Suppose we have three (x,y) pairs. {(2,5), (4,9), (6,13)}. It is a simple example. To train our model, we need to find the vector \u04e8. Now, for any given x, we can easily compute the value of y. We plot the three training examples, which fall in a straight line in this case. If the given x is 5, we can find the y value using this plot.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/linear-regression-from-scratch-graph-1.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111405\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/linear-regression-from-scratch-graph-1.webp\" alt=\"linear regression from scratch\" width=\"720\" height=\"502\" \/><\/a><\/p>\n<p>The parameters of this model can easily be calculated to be \u04e8<sub>0<\/sub>=1 and \u04e8<sub>1<\/sub>=2.<br \/>\n\u0b83 y=1 + 2x.<\/p>\n<p>Using this learned information we can compute (predict) the output of any new input data.<br \/>\nFor x=5, y=1+2*5=11.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/linear-regression-from-scratch-graph-2.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111406\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/linear-regression-from-scratch-graph-2.webp\" alt=\"linear regression from scratch graph\" width=\"720\" height=\"502\" \/><\/a><\/p>\n<p>This was a simple example, but actual tasks are not so easy. The training examples may be scattered yet they appear linear, and we will have to find the best fit.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/linear-regression-from-scratch-graph-3.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111407\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/linear-regression-from-scratch-graph-3.webp\" alt=\"linear regression\" width=\"720\" height=\"502\" \/><\/a><\/p>\n<p>In this picture, the examples do not lie in a straight line. However, they seem to follow a line closely. This line can be the one that can fit the dataset with the least loss and hence be the required model.<\/p>\n<h3>Methods of finding the parameters of a model in Linear Regression:<\/h3>\n<h4>1. Gradient Descent Method:<\/h4>\n<p>The parameter vector \u03b8 can be computed by plotting a loss curve, which will always be a parabola with its opening in the upward direction, then picking a point on the curve and moving towards the minimum of the curve to find the parameters\u2019 optimal value.<\/p>\n<p>We upgrade the value of the vector \u03b8 as follows.<\/p>\n<p><span style=\"font-weight: 400\"> \u03b8<\/span> <sub><span style=\"font-weight: 400\">j<\/span><\/sub><span style=\"font-weight: 400\">:=\u03b8<\/span> <sub><span style=\"font-weight: 400\">j<\/span><\/sub><span style=\"font-weight: 400\">-(\u03b1\/m)*<\/span><span style=\"font-weight: 400\">\u2211<\/span><sub><span style=\"font-weight: 400\">i=1<\/span><\/sub><sup><span style=\"font-weight: 400\">m<\/span><\/sup><span style=\"font-weight: 400\"> (<\/span><span style=\"font-weight: 400\">h<\/span><sub><span style=\"font-weight: 400\">\u03b8<\/span><\/sub><span style=\"font-weight: 400\">x<\/span><sup><span style=\"font-weight: 400\">i<\/span><\/sup><span style=\"font-weight: 400\">&#8211;<\/span><span style=\"font-weight: 400\">y<\/span><sup><span style=\"font-weight: 400\">i<\/span><\/sup><span style=\"font-weight: 400\">)<\/span><span style=\"font-weight: 400\">x<\/span><sub><span style=\"font-weight: 400\">j<\/span><\/sub><\/p>\n<h4>2. Normal Equation Method:<\/h4>\n<p>The normal equation method is a straightforward method of computing theta without requiring any loop and is, therefore, asymptotically faster.<\/p>\n<p><span style=\"font-weight: 400\">\u04e8=(<\/span><span style=\"font-weight: 400\">X<\/span><sup><span style=\"font-weight: 400\">T<\/span><\/sup><span style=\"font-weight: 400\">*X<\/span><span style=\"font-weight: 400\">)<\/span><sup><span style=\"font-weight: 400\">-1<\/span><\/sup><span style=\"font-weight: 400\">.<\/span><span style=\"font-weight: 400\">X<\/span><sup><span style=\"font-weight: 400\">T<\/span><\/sup><span style=\"font-weight: 400\">*Y<\/span><\/p>\n<h3>Linear Regression using numpy:<\/h3>\n<p>We will try to build a linear regression model which uses the normal equation method to find the value of \u04e8.<\/p>\n<h4>a. Importing the required libraries<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\nimport matplotlib.pyplot as plt\r\n%matplotlib inline<\/pre>\n<h4><span style=\"font-weight: 400\">b. Making dummy dataset<\/span><\/h4>\n<p>We will build our own dataset to train a regression model.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">x=np.linspace(-5,5,30)\r\ny=12*x\r\n\r\nprint(x)<\/pre>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/print-x.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111392\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/print-x.webp\" alt=\"print x\" width=\"1920\" height=\"198\" \/><\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">print(y)\r\n<\/pre>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/print-y.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111393\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/print-y.webp\" alt=\"print y\" width=\"1920\" height=\"226\" \/><\/a><\/p>\n<h4>c. Adding noise to the output vector and reshaping the vectors<\/h4>\n<p>The dataset we have created is a straight line. To make it a bit more challenging, we will add some noise to it. The data thus created will resemble that in practical examples in terms of being scattered.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">noise = np.random.normal(0, 5, y.shape)\r\ny=y+noise\r\n\r\nprint(y)\r\n<\/pre>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/print-y-with-noise.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111394\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/print-y-with-noise.webp\" alt=\"print y with noise\" width=\"1920\" height=\"231\" \/><\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">x=x.reshape(-1,1)\r\ny=y.reshape(-1,1)\r\n \r\nprint(x)\r\n<\/pre>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/print-x-reshaped.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111395\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/print-x-reshaped.webp\" alt=\"print x reshaped\" width=\"1920\" height=\"730\" \/><\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">print(y)\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/print-y-reshaped.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111396\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/print-y-reshaped.webp\" alt=\"print y reshaped\" width=\"1920\" height=\"745\" \/><\/a><\/p>\n<p>As we can see the vector y is no longer precisely linear to the input.<\/p>\n<p>Every other pair of points has a different gradient and intercept. Our job is to find a line so that the distance betweeb these points from it is minimum.<\/p>\n<h4>d. Using the normal equation method to compute the parameters<\/h4>\n<p>For this example, we will use the normal equation to calculate theta.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">theta=np.dot((np.linalg.inv(np.dot(x.T,x))),(np.dot(x.T,y)))\r\n\r\nprint(theta)\r\n<\/pre>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/print-theta.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111397\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/print-theta.webp\" alt=\"print theta\" width=\"1920\" height=\"115\" \/><\/a><\/p>\n<h4>e. Visualising the prediction along with the input initial input and outputs<\/h4>\n<p>Firstly, we will plot the initial x and y. Then, plot the x and the y computed using the calculated theta (prediction). Finally, we will plot both of these graphs together to see if our prediction is good or not.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">plt.scatter(x,y)<\/pre>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/scatte-plot-x-vs-y.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111398\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/scatte-plot-x-vs-y.webp\" alt=\"scatter plot x vs y\" width=\"1920\" height=\"432\" \/><\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">plt.plot(x,x*theta)\r\n<\/pre>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/plot-pred.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111390\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/plot-pred.webp\" alt=\"plot pred\" width=\"1920\" height=\"421\" \/><\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">plt.scatter(x,y)\r\nplt.plot(x,x*theta)\r\n<\/pre>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/final-plot.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111399\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/final-plot.webp\" alt=\"final plot\" width=\"1920\" height=\"466\" \/><\/a><\/p>\n<p>As we can see that the prediction curve follows the initial data points very closely, we can rest assured that our model is working properly.<\/p>\n<h3>Building Linear Regression Model Using PyTorch:<\/h3>\n<p>Now that we have built a model using numpy, we will try to build one using PyTorch. This time we will use Gradient Descent as the optimisation algorithm.<\/p>\n<h4>a. Importing the required modules<\/h4>\n<p>Before doing anything, we have to import torch and other required libraries.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import torch\r\nfrom torch.utils.data import TensorDataset,DataLoader\r\nimport torch.nn as nn\r\nimport numpy\r\nimport matplotlib.pyplot as plt\r\n<\/pre>\n<h4>b. Building the model<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class Model(torch.nn.Module):\r\n    def __init__(self):\r\n    \tsuper(Model, self).__init__()\r\n    \tself.linear = torch.nn.Linear(1, 1)\r\n\r\n    def forward(self, x):\r\n    \ty_pred = self.linear(x)\r\n    \treturn y_pred\r\n\r\nlreg=Model()\r\n#Creating an instance of the model we have created above\r\n\r\ncriterion = torch.nn.MSELoss()\r\n#specifying the loss function. Here we have used Mean Squared Error Loss\r\n\r\noptimizer = torch.optim.SGD(lreg.parameters(), lr = 0.01)\r\n#Optimising the model using Stochastic gradient descent.\r\n<\/pre>\n<h4>c. Preparing data<\/h4>\n<p>#Now we will create random data to train our model<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">x = torch.tensor(range(-5,5)).float()\r\ny = 12*x\r\n\r\nx_train = x[:,None]\r\ny_train = y[:,None]\r\n<\/pre>\n<p>#We have taken the transpose of the input and output tensors to transform its shape from 1 x 10 to 10 x 1.<\/p>\n<h4>d. Training the model<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">for i in range(500):\r\n   \t \r\n    \toptimizer.zero_grad()\r\n    \t#If we do not set grad to zero then the old gradients will add up in the current gradients making our model incorrect.\r\n   \t \r\n    \ty_pred = lreg(x_train)\r\n\r\n   \t \r\n    \tloss = criterion(y_pred, y_train)\r\n   \t \r\n    \tloss.backward()\r\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#Back propagating the neural network to find the gradient of the output with respect to the given parameters.\r\n\r\n   \t \r\n\toptimizer.step()\r\n\t#Optimising the weights of the neurons depending on the gradient calculated during backpropagation.\r\n\r\n\tprint(i,loss)\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/training-loss.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111401\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/training-loss.webp\" alt=\"training loss\" width=\"1920\" height=\"766\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/training-loss-2.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111402\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/training-loss-2.webp\" alt=\"training loss 2\" width=\"1920\" height=\"776\" \/><\/a><\/p>\n<p>On running this part, we will see that the loss slowly decreases and comes closer to zero.<\/p>\n<h3>Summary<\/h3>\n<p>It is fascinating that we can use Neural Networks and PyTorch to build regression models because they were invented for classification problems. Models built this way often perform better than those built by other methods. To build a model using PyTorch, we have to import torch.nn, backpropagate and finally, optimise the network.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linear Regression is an ML algorithm that tries to find a linear relationship between the inputs and the outputs of the training set. Then, based on this learned information, it tries to predict the&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":111404,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26498],"tags":[27191],"class_list":["post-111122","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pytorch-tutorials","tag-pytorch-linear-regression"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PyTorch Linear Regression - DataFlair<\/title>\n<meta name=\"description\" content=\"We can use PyTorch to build regression models because it is invented for classification problems. Learn more about PyTorch Linear regression.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PyTorch Linear Regression - DataFlair\" \/>\n<meta property=\"og:description\" content=\"We can use PyTorch to build regression models because it is invented for classification problems. Learn more about PyTorch Linear regression.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-09T04:30:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-09T05:10:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/pytorch-linear-regression.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PyTorch Linear Regression - DataFlair","description":"We can use PyTorch to build regression models because it is invented for classification problems. Learn more about PyTorch Linear regression.","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:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/","og_locale":"en_US","og_type":"article","og_title":"PyTorch Linear Regression - DataFlair","og_description":"We can use PyTorch to build regression models because it is invented for classification problems. Learn more about PyTorch Linear regression.","og_url":"https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-01-09T04:30:41+00:00","article_modified_time":"2023-01-09T05:10:07+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/pytorch-linear-regression.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"PyTorch Linear Regression","datePublished":"2023-01-09T04:30:41+00:00","dateModified":"2023-01-09T05:10:07+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/"},"wordCount":921,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/pytorch-linear-regression.webp","keywords":["PyTorch Linear Regression"],"articleSection":["PyTorch Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/","url":"https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/","name":"PyTorch Linear Regression - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/pytorch-linear-regression.webp","datePublished":"2023-01-09T04:30:41+00:00","dateModified":"2023-01-09T05:10:07+00:00","description":"We can use PyTorch to build regression models because it is invented for classification problems. Learn more about PyTorch Linear regression.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/pytorch-linear-regression.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/01\/pytorch-linear-regression.webp","width":1200,"height":628,"caption":"pytorch linear regression"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/pytorch-linear-regression\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"PyTorch Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/pytorch-tutorials\/"},{"@type":"ListItem","position":3,"name":"PyTorch Linear Regression"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/111122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=111122"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/111122\/revisions"}],"predecessor-version":[{"id":111408,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/111122\/revisions\/111408"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/111404"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=111122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=111122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=111122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}