Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why validating model without taking "with torch.no_grad()"? #9

Open
libo-huang opened this issue Dec 25, 2022 · 0 comments
Open

Why validating model without taking "with torch.no_grad()"? #9

libo-huang opened this issue Dec 25, 2022 · 0 comments

Comments

@libo-huang
Copy link

Thanks for your valuable project.

Below I found that you take with torch.no_grad(): on the dataset (i.e., input and target in lines 210-211) rather than on the model in line 213.
As shown in Evaluating pytorch models: with torch.no_grad vs model.eval() and
‘model.eval()’ vs ‘with torch.no_grad()’, it will take too much memory. But I am not sure about taking no with torch.no_grad block on line 213 whether influences the model's outputs.

for i, (input, target, task) in enumerate(dataloader):
if self.gpu:
with torch.no_grad():
input = input.cuda()
target = target.cuda()
if task_in is None:
output = model.forward(input)[:, :self.valid_out_dim]
acc = accumulate_acc(output, target, task, acc, topk=(self.top_k,))
else:
mask = target >= task_in[0]
mask_ind = mask.nonzero().view(-1)
input, target = input[mask_ind], target[mask_ind]
mask = target < task_in[-1]
mask_ind = mask.nonzero().view(-1)
input, target = input[mask_ind], target[mask_ind]
if len(target) > 1:
output = model.forward(input)[:, task_in]
acc = accumulate_acc(output, target-task_in[0], task, acc, topk=(self.top_k,))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant