My model can finish its training phase, while validation phase will throw an exception: Runtime Error: CUDA out of memory.
After using ‘with torch.no_grad()’, this model can works well, but I wonder why it’ll cause cuda out of memory without ‘with torch.no_grad()’ and what does ‘with torch.no_grad()’ change.
My function is defined as follows:
for i in range(1,epochs + 1):
    train_loss,valid_loss = 0.0,0.0

    for batch,(data,labels) in enumerate(trainloader):
        model.train()
        if is_cuda:
            data,labels = data.cuda(),labels.cuda()
        outs = model(data)
        loss = criterion(outs,labels)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        train_loss = train_loss + loss * data.size(0)

    with torch.no_grad():
        for batch,(data,labels) in enumerate(validloader):
            model.eval()
            if is_cuda:
                data,labels = data.cuda(),labels.cuda()
        
            outs = model(data)
            loss = criterion(outs,labels)
            valid_loss = valid_loss + loss * data.size(0)

    train_loss = train_loss/len(trainloader.dataset)
    valid_loss = valid_loss/len(validloader.dataset)