PyTorch使用笔记

  1. 将data, target, loss层,神经网络模型放到GPU上!
  2. 在构造模型时,可以使用这种方法,直接使用nn.Sequential来构造!
    这样在forward时可以直接用一个self.model来做!
class MLP(nn.Module):

    def __init__(self):
        super(MLP, self).__init__()

        self.model = nn.Sequential(
            nn.Linear(784, 200),
            nn.ReLU(inplace=True),
            nn.Linear(200, 200),
            nn.ReLU(inplace=True),
            nn.Linear(200, 10),
            nn.ReLU(inplace=True),
        )

    def forward(self, x):
        x = self.model(x)

        return x
  1. 神经网络在训练时可以每经过一个epoch就用训练得到的模型在validation set上进行一下测试,看模型实际效果,从而有效避免过拟合的出现!
  2. 训练时可以每个epoch(或者每隔n个)学习到的模型以及其loss保存下来,然后用val set验证其准确率,最后所有的epoch完成后,从中选择一个效果最好的check point模型进行加载,对test set进行测试看其实际泛化能力,但一定注意test set的数据不参与backward,只是forward

你可能感兴趣的:(算法,细节)