user12189069
user12189069

Reputation:

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

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

How to add a Validation(10% Dataset) and Test Set(10% Dataset) in this model ?

Model :

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

Upvotes: 1

Views: 3532

Answers (2)

basilisk
basilisk

Reputation: 1277

there are many ways to do this. you can use what @Shai suggested, 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

if you like an easier solution, you can take a look at skorch it is a scikit learn wrapper for pytorch. I find it easy to use and more like the keras API, skorch will implement train test split automatically when you start training but you can also pass your custom train and test set to it.

Upvotes: 0

Shai
Shai

Reputation: 114936

Train/validation/test splits of data are "orthogonal" to the model.

To manage your data for training/testing you might want to use pytorch's TensorDataset. Then you might find Subset to be useful for splitting the dataset into train/validation/test subsets.

Upvotes: 2

Related Questions