Issue
The code below appears in this tutorial.
total = 0
# since we're not training, we don't need to calculate the gradients for our outputs
with torch.no_grad():
for data in testloader:
images, labels = data
# calculate outputs by running images through the network
outputs = net(images)
# the class with the highest energy is what we choose as prediction
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the 10000 test images: %d %%' % (
100 * correct / total))
Why do you write outputs.data
here?
I want to know the difference from using outputs
only.
Solution
TLDR; Tensor
and Tensor.data
are not the same! Please refer to this answer.
While Tensor
and Tensor.data
do share the same memory they are not the same interface to accessing it. Also, notice how Tensor.data
is a Tensor
, which means the data
attribute is recursive... However, there is a difference between the two: operations performed on the data attribute will bypass Autograd's check. This means any computation performed from Tensor.data
won't be tracked for backpropagation. In practice, this means that using data
for computing is identical to detaching the tensor from its computational graph if any.
Answered By - Ivan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.