注:安装pytorch时,首先选择用anaconda的方法安装,但是因为pycharm在工程设置的时候总会遇到一点问题,所以就没有安装anaconda,而是安装python3.5.4+TensorFlow-gpu+cuda8.0之后再根据pytorch官网提供的方法安装pytorch。安装完pytorch之后,使用import torch 发现numpy的版本过低,这时候需要关闭python。使用pip install --upgrade numpy 进行升级,如果遇到权限问题,需在管理员cmd下安装。
import numpy as np
import torch
inputs = np.array([[73, 67, 43],
[91, 88, 64],
[87, 134, 58],
[102, 43, 37],
[69, 96, 70]], dtype='float32')
targets = np.array([[56, 70],
[81, 101],
[119, 133],
[22, 37],
[103, 119]], dtype='float32')
inputs = torch.from_numpy(inputs)
targets = torch.from_numpy(targets)
print(inputs)
print(targets)
w = torch.randn(2, 3, requires_grad=True)
b = torch.randn(2, requires_grad=True)
print(w)
print(b)
def model(x):
return x @ w.t() + b
preds = model(inputs)
print(preds)
def mse(t1, t2):
diff = t1 - t2
return torch.sum(diff*diff)/diff.numel()
loss = mse(preds, targets)
print(loss)
loss.backward()
print(w)
print(w.grad)
w.grad.zero_()
b.grad.zero_()
print(w.grad)
print(b.grad)
preds = model(inputs)
print(preds)
loss = mse(preds, targets)
print(loss)
loss.backward()
print(w.grad)
print(b.grad)
with torch.no_grad():
w -= w.grad*1e-5
b -= b.grad*1e-5
w.grad.zero_()
b.grad.zero_()
print(w)
print(b)
preds = model(inputs)
loss = mse(preds, targets)
print(loss)
for i in range(1000):
preds = model(inputs)
loss = mse(preds, targets)
loss.backward()
with torch.no_grad():
w -= w.grad * 1e-5
b -= b.grad * 1e-5
w.grad.zero_()
b.grad.zero_()
preds = model(inputs)
loss = mse(preds, targets)
print(loss)
print(preds)