output.data在pytorch中是什么意思?
What does output.data mean in pytorch?
下面的代码出现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))
这里为什么要写outputs.data
?
我想知道仅使用 outputs
的区别。
TLDR; Tensor
和Tensor.data
不一样!请参考。
虽然 Tensor
和 Tensor.data
确实共享相同的内存,但它们访问它的接口不同。另外,请注意 Tensor.data
是 一个 Tensor
,这意味着 data
属性是递归的...但是,两者之间存在差异:对数据属性执行的操作将绕过 Autograd 的检查。这意味着从 Tensor.data
执行的任何计算都不会被反向传播跟踪。实际上,这意味着使用 data
进行计算等同于从其计算图中分离张量 if any.
下面的代码出现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))
这里为什么要写outputs.data
?
我想知道仅使用 outputs
的区别。
TLDR; Tensor
和Tensor.data
不一样!请参考
虽然 Tensor
和 Tensor.data
确实共享相同的内存,但它们访问它的接口不同。另外,请注意 Tensor.data
是 一个 Tensor
,这意味着 data
属性是递归的...但是,两者之间存在差异:对数据属性执行的操作将绕过 Autograd 的检查。这意味着从 Tensor.data
执行的任何计算都不会被反向传播跟踪。实际上,这意味着使用 data
进行计算等同于从其计算图中分离张量 if any.