0
$\begingroup$

First of all, I'm new in this field and it's my first this kind of work. I'm trying to train EfficientNet (CNN), the code below is working fine, but I can't succeed to add also validation set to the code below. My validation data is val_X and val_y. I really will appreciate your help.

   def train(net):
        BATCH_SIZE = 64
        EPOCHS = 10
        for epoch in range(EPOCHS):
            for i in tqdm(range(0, len(train_X), BATCH_SIZE)):
                batch_X = train_X[i:i+BATCH_SIZE].view(-1,3,224,224)
                batch_y = train_y[i:i+BATCH_SIZE]
    
                batch_X, batch_y = batch_X.to(device), batch_y.to(device)
    
                net.zero_grad()
                outputs = net(batch_X)
                matches  = [torch.argmax(i)==torch.argmax(j) for i, j in zip(outputs, batch_y)]
                acc = matches.count(True)/len(matches)            
                loss = loss_function(outputs, batch_y)
                loss.backward()
                optimizer.step()
            print(f"Epoch: {epoch}. Acc: {round(float(acc),2)}  Loss: {round(float(loss),4)}")
    
    train(net)
$\endgroup$
2
$\begingroup$

The loop for the validation data would look very similar to your training loop, but for your validation data you only have to calculate the loss and not backpropagate the error. It would look something like this:

def train(net):
        BATCH_SIZE = 64
        EPOCHS = 10
        for epoch in range(EPOCHS):
            # training loop
            model.train()
            for i in tqdm(range(0, len(train_X), BATCH_SIZE)):
                batch_X = train_X[i:i+BATCH_SIZE].view(-1,3,224,224)
                batch_y = train_y[i:i+BATCH_SIZE]
    
                batch_X, batch_y = batch_X.to(device), batch_y.to(device)
    
                net.zero_grad()
                outputs = net(batch_X)
                matches = [torch.argmax(i)==torch.argmax(j) for i, j in zip(outputs, batch_y)]
                acc = matches.count(True)/len(matches)            
                loss = loss_function(outputs, batch_y)
                loss.backward()
                optimizer.step()
            
            print(f"Epoch: {epoch}. Training acc: {round(float(acc),2)} Training loss: {round(float(loss),4)}")
            
            # validation loop
            model.eval()
            for i in tqdm(range(0, len(val_X), BATCH_SIZE)):
                batch_X = val_X[i:i+BATCH_SIZE].view(-1,3,224,224)
                batch_y = val_y[i:i+BATCH_SIZE]
    
                batch_X, batch_y = batch_X.to(device), batch_y.to(device)
                
                outputs = net(batch_X)
                matches = [torch.argmax(i)==torch.argmax(j) for i, j in zip(outputs, batch_y)]
                acc = matches.count(True)/len(matches)            
                loss = loss_function(outputs, batch_y)
                
            print(f"Epoch: {epoch}. Validation acc: {round(float(acc),2)} Validation loss: {round(float(loss),4)}")
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.