【问题标题】:Get the validation/train loss from Skorch fit从 Skorch 拟合中获取验证/训练损失
【发布时间】:2020-08-04 20:21:46
【问题描述】:

有没有办法从 Skorch 中获取训练/验证损失,例如列表(如果你想做一些绘图、统计)?

【问题讨论】:

    标签: python-3.x skorch


    【解决方案1】:

    您可以使用历史记录(允许随着时间的推移进行切片)来获取此信息。例如:

    train_loss = net.history[:, 'train_loss']
    

    它为每个记录的纪元返回train_loss

    这是一个基于following的示例。

    import numpy as np
    import torch
    import torch.nn.functional as F
    from matplotlib import pyplot as plt
    from sklearn.datasets import make_classification
    from skorch import NeuralNetClassifier
    from torch import nn
    
    torch.manual_seed(0)
    
    class ClassifierModule(nn.Module):
        def __init__(
                self,
                num_units=10,
                nonlin=F.relu,
                dropout=0.5,
        ):
            super(ClassifierModule, self).__init__()
            self.num_units = num_units
            self.nonlin = nonlin
            self.dropout = dropout
    
            self.dense0 = nn.Linear(20, num_units)
            self.nonlin = nonlin
            self.dropout = nn.Dropout(dropout)
            self.dense1 = nn.Linear(num_units, 10)
            self.output = nn.Linear(10, 2)
    
        def forward(self, X, **kwargs):
            X = self.nonlin(self.dense0(X))
            X = self.dropout(X)
            X = F.relu(self.dense1(X))
            X = F.softmax(self.output(X), dim=-1)
            return X
    
    net = NeuralNetClassifier(
        ClassifierModule,
        max_epochs=20,
        lr=0.1,
        # device='cuda',  # uncomment this to train with CUDA
    )
    
    X, y = make_classification(1000, 20, n_informative=10, random_state=0)
    X, y = X.astype(np.float32), y.astype(np.int64)
    
    net.fit(X, y)
    
    train_loss = net.history[:, 'train_loss']
    valid_loss = net.history[:, 'valid_loss']
    
    plt.plot(train_loss, 'o-', label='training')
    plt.plot(valid_loss, 'o-', label='validation')
    plt.legend()
    plt.show()
    

    结果:

    【讨论】:

    • 好答案!我冒昧地通过在历史对象上使用切片符号来缩短代码。
    猜你喜欢
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 2019-11-29
    • 2018-04-05
    • 1970-01-01
    相关资源
    最近更新 更多