第1关:求导原理
import torch
Tensor = torch.Tensor
def task(x: Tensor) -> Tensor:
y = 2*x + 6
z = y * y * 3
out = torch.mean(3*z)
########## Begin ##########
x.requires_grad = True
out.backward()
########## End ##########
result = x.grad
return result
第2关:静态图动态图设计
import torch
def task():
w = torch.tensor([1.], requires_grad=True)
x = torch.tensor([2.], requires_grad=True)
########## Begin ##########
a = w+x
b = w+0
y = a+b
########## End ##########
y.backward()
print(y)
print(w.is_leaf, x.is_leaf, a.is_leaf, b.is_leaf, y.is_leaf)
第3关:正向反向传播
import torch
from torch import nn, optim
from torch.autograd import Variable
import numpy as np
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)
x_train = torch.from_numpy(x_train)
y_train = torch.from_numpy(y_train)
# Linear Regression Model
class LinearRegression(nn.Module):
def __init__(self):
super(LinearRegression, self).__init__()
self.linear = nn.Linear(1, 1) # input and output is 1 dimension
def forward(self, x):
out = self.linear(x)
return out
model = LinearRegression()
# 定义loss和优化函数
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=1e-4)
def train():
# 开始训练
num_epochs = 2000
for epoch in range(num_epochs):
inputs = Variable(x_train)
target = Variable(y_train)
# 正向传播
########## Begin ##########
out = model(inputs)
########## End ##########
loss = criterion(out, target)
# 反向传播
optimizer.zero_grad()
########## Begin ##########
loss.backward()
########## End ##########
optimizer.step()
if (epoch+1) % 20 == 0:
print('Epoch[{}/{}], loss: {:.6f}'
.format(epoch+1, num_epochs, loss.data))
return loss.data
第4关:损失函数
import torch
def task(y1, y2):
'''
任务要求:使用pytorch中的均方误差(MSE)求y1与y2之间的均方误差
'''
########## Begin ##########
loss = torch.nn.MSELoss()
########## End ##########
output = loss(y1, y2)
return output