如何在 Pytorch 模型中添加验证和测试集?

How to add a Validation and Test Set in Pytorch Model?

我有非线性回归 Model ANN( X = [1000,3] , Y = [1000,8] ),其中一个隐藏 Layer(Nh = 6)

如何在此模型中添加验证(10% 数据集)和测试集(10% 数据集)?

型号:

N, D_in, H, D_out = x.shape[0], x.shape[1], 6, y.shape[1]

model = nn.Sequential(OrderedDict([ ('fc1', nn.Linear(D_in, H)), 
                                    #('Sig', nn.Sigmoid()),
                                    ('ISRU', ISRU()), # Add ISRU
                                    ('fc2', nn.Linear(H, D_out))]))

# Error -----
loss_fn = torch.nn.L1Loss(reduction='mean')

# Train -----
optimizer = torch.optim.Adam(model.parameters(), lr=1,eps=2**(-EPS))
epoch = 250
for t in range(epoch):
    # Forward pass: compute predicted y by passing x to the model.
    clear_output(wait=True)
    y_pred = model(X)

    # Compute and print loss.
    loss = loss_fn(y_pred, Y)
    if t % 100 == 99:
        print(t, loss.item())

    optimizer.zero_grad() ;
    loss.backward() ;
    optimizer.step() ;
if loss.item() < diff : lista = np.vstack((lista, [loss.item(),2,EPS])) ; diff = loss.item()

Train/validation/test 数据拆分为模型 "orthogonal"。

要管理 training/testing 的数据,您可能需要使用 pytorch 的 TensorDataset. Then you might find 将数据集拆分为 train/validation/test 个子集。

有很多方法可以做到这一点。你可以使用 , I want to add what I would like to do. I ofen use train_test_split to split my data into train and test and then move forward to convert my train and test data to TensorDataset

如果您喜欢更简单的解决方案,可以查看 skorch 它是 pytorch 的 scikit 学习包装器。我发现它易于使用,更像 keras API,skorch 会在你开始训练时自动执行训练测试拆分,但你也可以将自定义训练和测试集传递给它。