Datawhale_Task2 设立计算图并自动计算
- numpy和pytorch实现梯度下降法
- 设定初始值
- 求取梯度
- 在梯度方向上进行参数的更新
- numpy和pytorch实现线性回归
- pytorch实现一个简单的神经网络
- 参考资料:PyTorch 中文文档 https://pytorch.apachecn.org/docs/1.0/
1.numpy和pytorch实现梯度下降法
(1)numpy实现梯度下降
x = 1
lr= 0.1 #学习率
epochs = 2
y = lambda x : x**2 + 5*x + 1
for epoch in range(epochs):
dx = 2*x + 5
x = x - lr*dx
print(x)
(2)pytorch实现梯度下降
import torch
from torch.autograd import Variable
x = torch.FloatTensor([1])
print('grad',x.grad,'data',x.data)
learning_rate = 0.1
epoches = 10
for epoch in range(epoches):
x = Variable(x, requires_grad=True)
y = x**2+5*x+1
y.backward()
x.data = x.data - learning_rate*x.grad.data
print('grad',x.grad.data, 'data',x.data) #打印梯度值,更新后的x
x.grad.data.zero_() #梯度归零
print(x.data)
(3)numpy实现线性回归
import torch
import torch.nn as nn
import numpy as np
from torch.autograd import Variable
import matplotlib.pyplot as plt
# Linear regression model
def model(x):
return [email protected]()+b # @ 代表pytorhc中矩阵乘法,.t()返回一个张量的转置
def mse(t1,t2):
diff = t1-t2
return torch.sum(diff*diff)/diff.numel()
# Hyper-parameters
epoches=1000
lr = 1e-4
# Toy dataset
x_train = np.array([[73,67,43], [91,88,64],[87,134,37],[102,43,37],[69,96,70]],dtype=np.float32)
y_train = np.array([[56,70],[81,101],[119,133],[22,37],[103,119]], dtype=np.float32)
# x_train = torch.from_numpy(x_train)
# y_train = torch.from_numpy(y_train)
# Weights and bias
w = torch.randn(2 ,3) # , equires_gard=True
b = torch.randn(2)
for epoch in range(epoches):
x_train = torch.from_numpy(x_train)
y_train = torch.from_numpy(y_train)
w = Variable(w, requires_grad=True)
b = Variable(b, requires_grad=True)
preds = model(x_train)
loss = mse(preds, y_train)
loss.backward()
w.data -= w.grad.data * lr
b.data -= b.grad.data * lr
w.grad.zero_()
b.grad.zero_()
if (epoch + 1) % 5 == 0:
print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch + 1, epoches, loss.item()))
(4)pytorch实现线性回归
import torch
from torch.autograd import Variable
epoches = 100
lr = 1e-4
# train data
x_data = Variable(torch.Tensor([[1.0], [2.0], [3.0]]))
y_data = Variable(torch.Tensor([[2.0], [4.0], [6.0]]))
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear = torch.nn.Linear(1, 1) # One in and one out
def forward(self, x):
y_pred = self.linear(x)
return y_pred
# our model
model = Model()
criterion = torch.nn.MSELoss(size_average=False) # Defined loss function
optimizer