pytorch线性回归_PyTorch中的线性回归
pytorch線性回歸
For all those amateur Machine Learning and Deep Learning enthusiasts out there, Linear Regression is just the right way to kick start your journey. If you are new to Machine Learning with some background in PyTorch, then buckle up because you have just ended up at the right spot. In case you are not, don’t worry, checkout my previous article to pick up some fundamentals of PyTorch and then you are all good to go.
F或所有這些業(yè)余的機器學(xué)習(xí)和深度學(xué)習(xí)愛好者在那里,線性回歸僅僅是開始踢你的旅程,以正確的方式。 如果您是剛接觸PyTorch的機器學(xué)習(xí)的新手,那么請緊扣一下,因為您剛剛來到了正確的位置。 如果您不是,請不要擔(dān)心,請查看我以前的文章以了解PyTorch的一些基礎(chǔ)知識,然后一切都很好。
那么什么是線性回歸? (So what is Linear Regression ?)
Linear Regression is a linear model that predicts the output, given a set of input variables, assuming that there is a linear relationship between the input variables and the single output variable.
線性回歸是在給定一組輸入變量的情況下預(yù)測輸出的線性模型,假設(shè)輸入變量和單個輸出變量之間存在線性關(guān)系。
For instance let’s just consider the simple linear equation y = w*x + b. Here y is the output and x is the input variable. “w” and “b” forms the slope and y intercept of the equation respectively. So we will refer to “w” and “b” as the parameters of the equation because once you get to know these values, you can easily predict the output for a given value of x.
例如,讓我們考慮簡單的線性方程y = w * x + b。 此處y是輸出,x是輸入變量。 “ w”和“ b”分別形成方程的斜率和y截距。 因此,我們將“ w”和“ b”稱為方程式的參數(shù),因為一旦了解了這些值,就可以輕松預(yù)測給定x值的輸出。
Now let’s slightly change the scenario. Assume that you are given the values of x and y (we will call them the training set ) and you are asked to find out the new value of y corresponding to a new variable x. Obviously, simple linear algebra would do the trick and easily give you the right parameters . Once you plug them into the equation you can find out the new value of y, corresponding to x, in no time.
現(xiàn)在讓我們稍微改變一下場景。 假設(shè)給定了x和y的值(我們將它們稱為訓(xùn)練集),并且要求您找出與新變量x對應(yīng)的y的新值。 顯然,簡單的線性代數(shù)可以解決問題,并輕松為您提供正確的參數(shù)。 將它們插入方程式后,您可以立即找到與x對應(yīng)的y的新值。
So what’s the big deal, these are things that we have covered in our high school. But here is what you should consider:
所以有什么大不了的,這些是我們中學(xué)時代所學(xué)的。 但是,您應(yīng)該考慮以下幾點:
This is where Machine learning steps in. I will try to give you an overview of what is happening behind the scenes of the infamous Linear regression method. Initially our Machine know nothing more than an average child. It will take some random values of “w” and “b”, which from now on we will refer to as the weight and the bias, and plug those into the equation y = w*x + b. Now it will take some values of x in the training set and find out the corresponding values of y using the parameters that it had assumed earlier. It will then compare the predicted values of y say yhat, with the actual values of y by calculating something known as the loss function (cost function).
這是機器學(xué)習(xí)的起點。我將嘗試向您概述臭名昭著的線性回歸方法的幕后發(fā)生的事情。 最初,我們的機器只不過是一個普通的孩子而已。 它將取一些“ w”和“ b”的隨機值,從現(xiàn)在開始我們將其稱為權(quán)重和偏差,并將它們插入方程y = w * x + b中。 現(xiàn)在,它將在訓(xùn)練集中獲取x的一些值,并使用之前假定的參數(shù)找出y的對應(yīng)值。 然后,它將通過計算稱為損失函數(shù)(成本函數(shù))的值,將y的預(yù)測值與y的實際值進行比較。
The loss function essentially denotes the error in our prediction. A greater value of the loss function denotes a greater error. Here we will be using Mean Square Error (MSE) as our loss function and it is given by the formula :
損失函數(shù)實質(zhì)上表示我們預(yù)測中的誤差。 損失函數(shù)的值越大表示誤差越大。 在這里,我們將使用均方誤差(MSE)作為損失函數(shù),它由公式給出:
Once the loss is calculated the we perform an optimization method like gradient descent on the loss function. Stuck with gradient descent, don’t worry, for the time sake all you have to know is that gradient descent is simply a method performed on the loss function to find the values of “w” and “b” that minimizes the loss function. It involves the use of learning rate which can be tweaked for better results. Smaller the loss more accurate is our prediction.
一旦計算出損失,我們就對損失函數(shù)執(zhí)行優(yōu)化方法,例如梯度下降。 不必擔(dān)心梯度下降,因為時間上的緣故,您只需要知道梯度下降只是對損失函數(shù)執(zhí)行的一種方法,可以找到使損失函數(shù)最小的“ w”和“ b”值。 它涉及學(xué)習(xí)率的使用,可以對其進行調(diào)整以獲得更好的結(jié)果。 損失越小越準(zhǔn)確,這就是我們的預(yù)測。
The white dot indicates the values of w and b. Gradient descent alters these values until it converges to a minima in the graph of the loss function.白點表示w和b的值。 梯度下降會更改這些值,直到其收斂到損失函數(shù)圖中的最小值為止。Now what we have seen so far essentially constitutes just one step in the training process. This cycle is repeated a number of times until we arrive at an optimum value of “w” and “b”, where the loss function is minimal.
現(xiàn)在,到目前為止,我們所看到的基本上只是培訓(xùn)過程中的一個步驟。 重復(fù)此循環(huán)多次,直到我們獲得損失函數(shù)最小的最佳值“ w”和“ b”。
Okay you have covered the basics of linear regression, now it’s time to code.
好的,您已經(jīng)介紹了線性回歸的基礎(chǔ)知識,現(xiàn)在該進行編碼了。
線性回歸,PyTorch方法 (Linear regression, the PyTorch way)
For simplicity we will be looking at 1D Linear Regression with two parameters. First we import torch for this task.
為簡單起見,我們將研究具有兩個參數(shù)的一維線性回歸。 首先,我們?yōu)榇巳蝿?wù)導(dǎo)入火炬。
import torchThe data we will be looking at would be really simple, a torch tensor from -3 to 3 with a step size of 0.1. So lets create our data.
我們將要查看的數(shù)據(jù)非常簡單,步長為0.1的焊炬張量從-3到3。 因此,讓我們創(chuàng)建數(shù)據(jù)。
# Create f(X) with a slope of 1 and a bias of -1X = torch.arange(-3, 3, 0.1).view(-1, 1)f = 1 * X - 1# Add noiseY = f + 0.1 * torch.randn(X.size())
As you might have noticed the equation that our machine wants to learn in this case is y = x -1 where w = 1 and b = -1. Additionally we add some noise to our data so as to make it similar to real world data.
您可能已經(jīng)注意到,在這種情況下,我們的機器想要學(xué)習(xí)的方程是y = x -1,其中w = 1和b = -1。 另外,我們在數(shù)據(jù)中添加了一些噪點,使其與真實世界的數(shù)據(jù)相似。
A plot of our training set and the function that our machine would like to learn would look like this:
我們的訓(xùn)練集和機器想要學(xué)習(xí)的功能的圖表如下所示:
Now lets create our model and our loss function ( cost function ).
現(xiàn)在讓我們創(chuàng)建模型和損失函數(shù)(成本函數(shù))。
# Define the forward functiondef forward(x):return w * x + b# Define the MSE Loss functiondef criterion(yhat,y):
return torch.mean((yhat-y)**2)
The error surface for the same would look like the figure given below.
相同的錯誤表面如下圖所示。
The first figure shows the contour plot of our loss function while the second one shows it’s 3D representation.第一個圖顯示了損失函數(shù)的等高線圖,第二個圖顯示了其3D表示。Now let’s create model parameters w, b by setting the argument requires_grad to True because we must learn it using the data.
現(xiàn)在讓我們通過將參數(shù)require_grad設(shè)置為True來創(chuàng)建模型參數(shù)w,b,因為我們必須使用數(shù)據(jù)來學(xué)習(xí)它。
# Define the parameters w, b for y = wx + bw = torch.tensor(-15.0, requires_grad = True)b = torch.tensor(-10.0, requires_grad = True)
Set the learning rate to 0.1 and create an empty list LOSS for storing the loss for each iteration. The LOSS function may be used for plotting and analyzing in the future.
將學(xué)習(xí)速率設(shè)置為0.1,并創(chuàng)建一個空列表LOSS,用于存儲每次迭代的損失。 將來可能會使用LOSS功能進行繪圖和分析。
# Define learning rate and create an empty list for containing the loss for each iteration.lr = 0.1LOSS = []
Time to train our model.
是時候訓(xùn)練我們的模型了。
# The function for training the modeldef train_model(iter):# Loop
for epoch in range(iter):
# make a prediction
Yhat = forward(X)
# calculate the loss
loss = criterion(Yhat, Y)
# store the loss in the list LOSS
LOSS.append(loss)
# backward pass
loss.backward()
# update parameters slope and bias
w.data = w.data - lr * w.grad.data
b.data = b.data - lr * b.grad.data
# zero the gradients before running the backward pass
w.grad.data.zero_()
b.grad.data.zero_()train_model(15)
Here the backward() method computes gradient of the loss with respect to all the learnable parameters. The parameters are updated in each epoch by subtracting from itself the multiple of the gradient and the learning rate . This essentially constitutes the gradient descent method. Feel free to tweak the learning rate, number of epochs and initial value of parameters to get better results.
在這里,backward()方法針對所有可學(xué)習(xí)的參數(shù)計算損耗的梯度。 通過從自身減去梯度和學(xué)習(xí)率的倍數(shù)來更新參數(shù)。 這實質(zhì)上構(gòu)成了梯度下降法。 隨意調(diào)整學(xué)習(xí)率,時期數(shù)和參數(shù)的初始值以獲得更好的結(jié)果。
In this case our results would look like this:
在這種情況下,我們的結(jié)果將如下所示:
The first image shows how cost function decreases with epoch. The second plot shows our final contour plot.第一張圖片顯示了成本函數(shù)如何隨時間減少。 第二個圖顯示了我們的最終輪廓圖。Now let’s see what our model will predict. If we give a value of -3, the output would be y = -3-1 = -4.
現(xiàn)在,讓我們看看我們的模型將預(yù)測什么。 如果給定值-3,則輸出將為y = -3-1 = -4。
x = torch.tensor(-3.)print(forward(x))#Output
tensor(-4.2857, grad_fn=<AddBackward0>)
Okay we are close enough. Try out different values for the hyper-parameters and we may get interesting results.
好吧,我們已經(jīng)足夠親密了。 對超參數(shù)嘗試不同的值,我們可能會得到有趣的結(jié)果。
結(jié)論 (Conclusion)
Hope you have got an overview of how linear regression works. Good luck learning more about linear regression and other Machine Learning algorithms. Stay hungry Stay Foolish.
希望您對線性回歸的工作原理有所了解。 祝您學(xué)習(xí)有關(guān)線性回歸和其他機器學(xué)習(xí)算法的更多信息。 求知若饑,虛懷若愚。
翻譯自: https://medium.com/analytics-vidhya/linear-regression-in-pytorch-86fe93630fd4
pytorch線性回歸
總結(jié)
以上是生活随笔為你收集整理的pytorch线性回归_PyTorch中的线性回归的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深度学习数据集制作工作_创建我的第一个深
- 下一篇: ps怎么看图片尺寸大小