训练
1.利用dataset构建DataLoader
2.定义优化器和损失函数
3.将模型转到device上
4.训练来啦
(1)先把梯度清零。数据转到device上
(2)反向传播并计算梯度
(3)更新参数
dataser=MyDataset(file)
train_set=DataLoader(dataset,batch_size=16,shuffle=True)
model=MyModel().to(device)
criterion=nn.MSELoss()
optimizer=torch.optim.SGD(model.parameters(),lr,momentum=0)
epochs=100
for epoch in range(epochs):
model.train()
for x,y in train_set:
optimizer.zero_grad()
x,y=x.to(device),y.to(device)
pred=model(x)
loss=criterion(pred,y)
loss.backward()
optimizer.step()
验证
#valid
在验证和测试先把Model转到device上,不需要更新参数,不需要进行梯度计算
model.eval()
total_loss=0
for x,y in train_set:
x,y=x.to(device),y.to(device)
with torch.no_grad():
pred=model(x)
loss=criterion(pred,y)
total_loss+=loss.cpu().item()*len(x)
avg_loss=total_loss/len(train_set.dataset)