How to add Validation and Test set in Non Linear Regression ANN

I have Non Linear Regression Model ANN( X = [1000,3] , Y = [1000,8] ) with One hidden Layer(Nh = 6) .

How add Validation and Test analisys in my Non Linear Regression ANN with Pytorch in my case?

  • Add code for split

  • Add code for test and validation in “epoch” loop

Model :

` ```
x = torch.from_numpy(x).float()
y = torch.from_numpy(y).float()

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.
    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()

Since you are using the all data samples at the moment, I think the easiest way would be to split your numpy arrays via sklean.model_selection.train_test_split and transform each split into a tensor with torch.from_numpy.

For the evaluation loop, I would refer to the MNIST example.
Note that a Dataset and DataLoader was used in the linked example, but you could simply use the tensors directly as is done in your current code snippet.

1 Like

For split Dataset i dont have problem, im doing what you saying.
In the MNIST example i can see test loop, but not Validation set, loop with train set and relative loss calculation !?

You could use the test loop for your validation use case. :wink:

1 Like