How to print a tensor without showing gradients
If I do something like this:

tmp = torch.ones(3, 2, 2, requires_grad=True)
out = tmp ** 2
print("\n{}".format(out))


I get as an output:

tensor([[[1., 1.],
         [1., 1.]],

        [[1., 1.],
         [1., 1.]],

        [[1., 1.],
         [1., 1.]]], grad_fn=&lt;PowBackward0&gt;)


I would like to print out just the values, not the grad_fn part.

However, doing

print("\n{}".format(out[0]))


results in:

tensor([[1., 1.],
        [1., 1.]], grad_fn=&lt;SelectBackward&gt;)


The only way I know is to out.detach() or there is another/better way? Just to clarify, I am happy the gradient is calculated. I just want to show the vector values without additional data.

Using the data should do the job for you:

tmp = torch.ones(3, 2, 2, requires_grad=True)
out = tmp ** 2
print("\n{}".format(out.data))


Output:

tensor([[[1., 1.],
         [1., 1.]],

        [[1., 1.],
         [1., 1.]],

        [[1., 1.],
         [1., 1.]]])

