PyTorch简单入门实战--一个简单的回归模型

一、用PyTorch做一个简单的神经网络回归模型

import torch
from torch.autograd import Variable
import torch.nn.functional as F
import matplotlib.pyplot as plt
#实现简单回归模型
x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1)
y = x.pow(2) + 0.2*torch.rand(x.size())

x, y = Variable(x), Variable(y)

class Net(torch.nn.Module):
    def __init__(self, n_feature, n_hidden, n_output):
        super(Net, self).__init__()
        self.hidden = torch.nn.Linear(n_feature, n_hidden)
        self.predict = torch.nn.Linear(n_hidden, n_output)
    
    def forward(self, x):
        x = F.relu(self.hidden(x))
        x = self.predict(x)
        return x
    
net = Net(1, 10, 1)
optimizer = torch.optim.SGD(net.parameters(), lr=0.5)

for i in range(3000):
    #这里分别是清空上一步的更新参数值、进行误差的反向传播、计算新的更新参数值、将计算得到的更新值赋给net.parameters()
    prediction = net(x)
    loss = torch.nn.MSELoss()(prediction, y)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    if (i+1)%100 == 0:
        print(loss.data)

你可能感兴趣的:(Pytorch)