设立计算图并自动计算
任务
- numpy和pytorch实现梯度下降法
- 设定初始值
- 求取梯度
- 在梯度方向上进行参数的更新
- numpy和pytorch实现线性回归
- pytorch实现一个简单的神经网络
参考资料
新手必备 | 史上最全的PyTorch学习资源汇总
快速上手笔记,PyTorch模型训练实用教程(附代码)
PyTorch学习笔记
《深度学习框架PyTorch:入门与实践》的对应代码
PyTorch 中文文档
作业
- numpy和pytorch实现梯度下降法
x = 1
learning_rate = 0.0001
epochs = 10
y = lambda x : x**2 + 2*x + 1
for epoch in range(epochs):
dx = 2*x + 2
x = x - learning_rate *dx
print(x)
0.9960035980806718
import torch
from torch.autograd import Variable
x = torch.Tensor([1])
print("grad",x.grad,"data",x.data)
learning_rate = 0.00001
epochs = 10
for epoch in range(epochs):
y = x**2 + 2*x + 1
y.requires_grad = True
y.backward()
print("grad",y.grad)
x.data = x.data - learning_rate * y.grad
y.grad.data.zero_()
print(x.data)
numpy和pytorch实现线性回归
import numpy as np
x_data = np.array([1,2,3])
y_data = np.array([2,4,6])
epochs = 1000
lr = 0.001
w = 0
cost = []
for epoch in range(epochs):
y_hat = x_data * w
loss = np.average((y_hat - y_data) ** 2)
cost.append(loss)
dw = -2 * (y_data - y_hat) @ x_data.T/(x_data.shape[0])
w = w - lr * dw
print(w)
1.9998307298556592
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
input_size = 1
output_size = 1
num_epochs = 60
learning_rate = 0.001
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)
model = nn.Linear(input_size,output_size)
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(),lr =learning_rate)
for epoch in range(num_epochs):
inputs = torch.from_numpy(x_train)
targets = torch.from_numpy(y_train)
outputs = model(inputs)
loss = criterion(outputs,targets)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch + 1 ) % 5 == 0:
print("Epoch[{}/{}],loss:{:.4f}".format(epoch + 1, num_epochs,loss.item()))
predicted = model(torch.from_numpy(x_train)).detach().numpy()
plt.plot(x_train, y_train, 'ro', label='Original data')
plt.plot(x_train, predicted, label='Fitted line')
plt.legend()
plt.show()
torch.save(model.state_dict(), 'linear.ckpt')
Epoch[5/60],loss:0.2663
Epoch[10/60],loss:0.2087
Epoch[15/60],loss:0.1854
Epoch[20/60],loss:0.1759
Epoch[25/60],loss:0.1721
Epoch[30/60],loss:0.1705
Epoch[35/60],loss:0.1699
Epoch[40/60],loss:0.1696
Epoch[45/60],loss:0.1695
Epoch[50/60],loss:0.1695
Epoch[55/60],loss:0.1695
Epoch[60/60],loss:0.1695

import torch
dtype = torch.float
device = torch.device("cpu")
N, D_in, H, D_out = 64, 1000, 100, 10
x = torch.randn(N, D_in, device=device, dtype=dtype)
y = torch.randn(N, D_out, device=device, dtype=dtype)
w1 = torch.randn(D_in, H, device=device, dtype=dtype, requires_grad=True)
w2 = torch.randn(H, D_out, device=device, dtype=dtype, requires_grad=True)
learning_rate = 1e-6
for t in range(500):
y_pred = x.mm(w1).clamp(min=0).mm(w2)
loss = (y_pred - y).pow(2).sum()
print(t, loss.item())
loss.backward()
with torch.no_grad():
w1 -= learning_rate * w1.grad
w2 -= learning_rate * w2.grad
w1.grad.zero_()
w2.grad.zero_()
0 30793478.0
1 24614226.0
2 19491514.0
3 14447308.0
4 10014916.0
...