先贴个笔记:
均方损失函数,注意
SGD更新参数
注意
1.构建模型
设计网络的结构,pytorch有方便的模块和函数可供使用,例如nn.Module
,nn.Sequential
,nn.Linear
:
# 使用类构建
class LinearNet(nn.Module):
def __init__(self, n_feature):
super(LinearNet, self).__init__() # call father function to init
self.linear = nn.Linear(n_feature, 1) # function prototype: `torch.nn.Linear(in_features, out_features, bias=True)`
'''
# method 1
self.linear = nn.Sequential(
nn.Linear(n_feature, 1)
# nn.Linear(...)
)
# method 2
self.linear = nn.Sequential()
self.linear.add_model('linear', nn.Linear(n_feature, 1))
# self.linear.add_model(...)
# method3
from collections import OrderedDict # 生成有序词典
self.linear = nn.Sequential(OrderedDict([
('linear', nn.Linear(n_feature, 1)),
# (...)
]))
'''
def forward(self, x):
y = self.linear(x)
return y
其中的__init__
实现是可变的,可以通过nn.Sequential
加入多层神经元,例如以上注释掉的部分
1.1初始化模型参数
from torch.nn import init
init.normal_(net[0].weight, mean=0.0, std=0.01)
init.constant_(net[0].bias, val=0.0)
1.2定义损失函数和优化函数
loss = nn.MSELoss()
import torch.optim as optim
optimizer = optim.SGD(net.parameters(), lr=0.03)
2.训练
大体步骤是{预测、评判、清空前一步梯度、计算本步梯度、修正权重}固定步骤
num_epochs = 3
for epoch in range(1, num_epochs + 1):
for X, y in data_iter:
output = net(X)
# l/o/l/o
l = loss(output, y.view(-1, 1))
optimizer.zero_grad() # reset gradient, equal to net.zero_grad()
l.backward()
optimizer.step()
print('epoch %d, loss: %f' % (epoch, l.item()))
查看权重
# result comparision
dense = net[0]
print(true_w, dense.weight.data)
print(true_b, dense.bias.data)
网友评论