dmandair:

I’ve looked at like the code snippet below. Note in it the line out = torch.argmax(outputs.detach(),dim=1) used during training. I was confused why this was used here but not in the training loop I referenced in the pytorch tutorial (wasn’t sure if I wasn’t understanding something).


torch.argmax(outputs.detach(), dim=1) is used to get the predicted class indices in a multi-class classification use case.  Detaching the tensor is needed if you are storing the calculated accuracy e.g. in a list or are accumulating it as otherwise you would store the entire computation graph with it, which would increase the memory usage (and could then yield an out of memory error). Alternatively, you could also wrap the accuracy calculation into a with torch.no_grad() block, but it depends on your coding style I guess.



 dmandair:

Similarly note at the bottom of this training block the line out = model(inputs.cuda()).cpu(), which is done during validation.


This isn’t needed since the acc value is created via item(), which will create a Python literal on the CPU.