I have the following model for classification. I have train set and test set. I trained it on train set ,the Input is 3400 vector and output it is a class between 3 classes (0,1,2). I saved the model as the following code . Now I want to apply 10 fold cross validation to evaluate the saved model on test set. Could you please tell how i can do it since I never used 10 cross validation before.
training_set = Dataset("train_data.txt","train_target.txt")
training_generator = torch.utils.data.DataLoader(training_set, **params)
testing_set = Dataset("test_data.txt","testtarget.txt")
testing_generator = torch.utils.data.DataLoader(testing_set, **params)
for i, (seq_batch, stat_batch) in enumerate(training_generator):
seq_batch, stat_batch = seq_batch.to(device), stat_batch.to(device)
optimizer.zero_grad()
#print(seq_batch.shape,stat_batch.shape)
# Model computation
seq_batch = seq_batch.unsqueeze(-1)
outputs = model(seq_batch)
if CUDA:
loss = criterion(outputs, stat_batch)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
epoch_loss += loss.item()*outputs.shape[0]
if i % 2000 == 1999: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000),"acc",(outputs.argmax(1) == stat_batch).float().mean())
running_loss = 0.0
sum_acc += (outputs.argmax(1) == stat_batch).float().sum()
print("epoch" , epoch+1, "acc", sum_acc/len(training_set),"loss", epoch_loss/len(training_set))
loss_values.append(epoch_loss/len(training_set))
if epoch % 20 == 0:
torch.save(model.state_dict(), path + name_file + "model_epoch_i_" + str(epoch) + ".cnn")