Based on a linear fit python and pytorch (to be parsed and doubts)

Achieve linear fit with a neural network, the code from the "entry-depth study of pytorch".

"Getting the depth learning pytorch" experience

Good is not good about this book, I am not a good evaluation, but the overall experience is not very good.
Some of the code book was incomplete, no novice own operating results, is not conducive to the novice to understand the code.
Another source on GitHub older version. Pytorch version I used was 1.0.1, when running the sample code will complain, the details need to be changed to run properly.

Linear fit Code

code show as below

import torch
import numpy as np
from torch.autograd import Variable
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt

# 原始数据为 numpy.array
x_train = np.array([[3.3], [4.4], [5.5], [6.71], [6.93], [4.168],
                    [9.779], [6.182], [7.59], [2.167], [7.042],
                    [10.791], [5.313], [7.997], [3.1]], dtype=np.float32)

y_train = np.array([[1.7], [2.76], [2.09], [3.19], [1.694], [1.573],
                    [3.366], [2.596], [2.53], [1.221], [2.827],
                    [3.465], [1.65], [2.904], [1.3]], dtype=np.float32)

# array 变 Tensor
x_train = torch.from_numpy(x_train)
y_train = torch.from_numpy(y_train)

# ----------------定义模型------------------
class LinearRegression(nn.Module):
    def __init__(self):
        super(LinearRegression, self).__init__()
        self.linear = nn.Linear(1, 1)   # 输入输出都是一维
    def forward(self, x):
        out = self.linear(x)
        return out

# 判断cuda加速是否可用
if torch.cuda.is_available():
    model = LinearRegression().cuda()
else:
    model = LinearRegression()
# ----------------定义模型完毕--------------

# ------------定义损失函数和优化函数--------------
criterion = nn.MSELoss()    # 损失函数
optimizer = optim.SGD(model.parameters(), lr=1e-3)

# 循环100次
num_epochs = 100
for epoch in range(num_epochs):
    # 判断cuda加速是否可用
    if torch.cuda.is_available():
        inputs = Variable(x_train).cuda()
        target = Variable(y_train).cuda()
    else:
        inputs = Variable(x_train)
        target = Variable(y_train)
#     forward
    out = model(inputs)
    loss = criterion(out, target)
#     backward
    optimizer.zero_grad()       # 梯度归零
    loss.backward()
    optimizer.step()
    # 输出循环结果
    if(epoch+1) % 20 == 0:
        print('Epoch[{}/{}], loss:{:.6f}'
              .format(epoch+1, num_epochs, loss.data))

model.eval()    # 将模型转变为测试模式(有一些层操作在训练和测试的时候不一样,所以要转换
model.cpu()
predict = model(Variable(x_train))
print('predict:\n')
print(predict)
predict = predict.data.numpy()
plt.plot(x_train.numpy(), y_train.numpy(), 'ro', label='Original data')
plt.plot(x_train.numpy(), predict, label='Fitting line')
plt.show()

doubt

  1. How simply tensor from the CPU, simply transferred to the GPU? (Error will be reported on discrepancies CUDA and CPU)
  2. About tensor and Variable, the new version of pytorch have to combine the two. This time, the reference code is relatively old, so see if there areas for improvement?
  3. Carrying on 2:00, tensor, Variable, CUDA, CPU, numpy, data types too complicated, there is no method to reduce data types?
  4. "Model.eval () # model into a test mode (some layer operations are not the same in training and testing time, so you want to convert." How to understand this sentence?

Guess you like

Origin blog.csdn.net/qq_41683065/article/details/91393582