学习
刘二大人《PyTorch深度学习实践》
B站地址:B站链接
问题1:通过损失的计算对权重进行更新,对于复杂的网络应当怎么去做呢?
通过前馈和反向传播
问题2:在每一层结束引入非线性变换函数的意义是?
为了提高模型的复杂度和泛化程度
import torch
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
w = torch.Tensor([1.0])
w.requires_grad = True
def forward(x):
return x * w
def loss(x, y):
y_pred = forward(x)
return (y_pred - y) ** 2
print('predict (before training)', 4, forward(4).item())
for epoch in range(100):
for x, y in zip(x_data, y_data):
l = loss(x, y)
l.backward()
print('\tgrad:', x, y, w.grad.item())
w.data = w.data - 0.01 * w.grad.data
w.grad.data.zero_()
print('progress:', epoch, l.item())
print('predict (after training)', 4, forward(4).item())