I have tried your idea, the validation phase will arise CUDA Error while removing ‘with no_grad()’.
Code as follows:
for i in range(1,epochs + 1):
    valid_loss = 0.0
    for batch,(data,labels) in enumerate(validloader):
        model.eval()
        if is_cuda:
            data,labels = data.cuda(),labels.cuda()
        
        outs = model(data)[1]
        loss = criterion(outs,labels)
        valid_loss = valid_loss + loss * data.size(0)

    valid_loss = valid_loss/len(validloader.dataset)

    history.log(i,valid_loss = valid_loss)
    with canvas:
        canvas.draw_plot([history['valid_loss']])