本期作業包括:
1.numpy和pytorch實現梯度下降法
2.設定初始值
3.求取梯度
4.在梯度方向上進行參數的更新
5.numpy和pytorch實現線性迴歸
6.pytorch實現一個簡單的神經網絡
7.參考資料:PyTorch 中文文檔 https://pytorch.apachecn.org/docs/1.0/
作業如下:
1.numpy和pytorch實現梯度下降法
2.設定初始值
3.求取梯度
4.在梯度方向上進行參數的更新
import torch
from torch.autograd import Variable
x = 1
learning_rate = 0.1
epochs = 100
y = lambda x : x ** 2 + 2 * x + 1
for epoch in range(epochs):
dx = 2 * x + 2
x = x - learning_rate * dx
print(x)
print(y(x))
x=torch.Tensor([1])
#建立一個張量 tensor([1.], requires_grad=True)
x=Variable(x,requires_grad=True)
print('grad',x.grad,'data',x.data)
learning_rate=0.1
epochs=10
for epoch in range(epochs):
y = x**2 + 2*x +1
y.backward()
print('grad',x.grad.data)
x.data=x.data-learning_rate*x.grad.data
#在PyTorch中梯度會積累假如不及時清零
x.grad.data.zero_()
print(x.data)
print(y)
5.numpy和pytorch實現線性迴歸
numpy方法:
import numpy as np
x_data=np.array([1,2,3])
y_data=np.array([2,4,6])
epochs=10
lr=0.1
w=0
cost=[]
for epoch in range(epochs):
yhat=x_data*w
loss=np.average((y_data-yhat)**2)
cost.append(loss)
dw=-2*(y_data-yhat)@x_data.T/(x_data.shape[0])
w=w-lr*dw
print(w)
print(w)
pytorch實現線性迴歸:
torch.manual_seed(2)
x_data=Variable(torch.Tensor([[1.0],[2.0],[3.0]]))
y_data=Variable(torch.Tensor([[2.0],[4.0],[6.0]]))
epochs=10
lr=0.1
w=Variable(torch.FloatTensor([0]),requires_grad=True)
cost=[]
for epoch in range(epochs):
yhat=x_data*w
loss=torch.mean((yhat-y_data)**2)
cost.append(loss.data.numpy())
loss.backward()
w.data=w.data-lr*w.grad.data
print(w.data)
w.grad.data.zero_()
print("結果爲:\n", w.data)
7.實現一個神經網絡
詳見作業1的鏈接