我遇到了this PyTorch教程(位于Neuro_networks_tutorial.py中),他们在其中构造了一个简单的神经网络并进行了推理。我想打印整个输入张量的内容以进行调试。当我尝试打印张量时,我得到的是这样的东西,而不是整个张量:
我对于numpy看到了类似的link,但不确定PyTorch的工作方式。我可以将其转换为numpy并可以查看它,但想避免额外的开销。我有办法打印整个张量吗?
答案 0 :(得分:5)
为避免截断并控制打印多少张量数据,请使用与numpy的numpy.set_printoptions(threshold=10_000)
相同的API。
示例:
x = torch.rand(1000, 2, 2)
print(x) # prints the truncated tensor
torch.set_printoptions(threshold=10_000)
print(x) # prints the whole tensor
如果张量很大,请将threshold
的值调整为更大的数字。
另一个选择是:
torch.set_printoptions(profile="full")
print(x) # prints the whole tensor
torch.set_printoptions(profile="default") # reset
print(x) # prints the truncated tensor
所有可用的set_printoptions
自变量已记录在here中。
答案 1 :(得分:3)
尽管我不建议这样做,但是
RecyclerView
答案 2 :(得分:0)
我来到这里实际上是为了寻找如何在控制台的一行中打印整行张量的答案,所以我想我会添加这个。
tensor([[1.1573e+04, 6.0693e+02, 1.2436e+03, 2.7277e+04, 1.6673e+08, 2.0462e+00, 9.8891e-01],
[2.0237e+04, 5.9074e+02, 1.7208e+03, 2.7449e+04, 2.1301e+08, 2.0678e+00, 1.0011e+00],
[2.7456e+04, 6.1106e+02, 1.4897e+03, 2.7332e+04, 1.7310e+08, 2.0448e+00, 9.6041e-01],
[1.7732e+04, 6.0232e+02, 1.2608e+03, 2.7371e+04, 1.8106e+08, 1.9594e+00, 1.0040e+00],
...,
[1.1167e+04, 5.9867e+02, 1.3440e+03, 2.7263e+04, 2.3160e+08, 2.0190e+00, 1.0075e+00],
[1.6003e+04, 5.9590e+02, 1.2319e+03, 2.7368e+04, 1.7155e+08, 2.0171e+00, 1.0202e+00],
[1.5499e+04, 6.1471e+02, 9.4877e+02, 2.7395e+04, 1.8146e+08, 1.9016e+00, 9.5884e-01],
[3.3886e+04, 6.0689e+02, 1.0777e+03, 2.7259e+04, 2.1599e+08, 2.0179e+00, 1.0201e+00]], dtype=torch.float64)
我这样做了
torch.set_printoptions(linewidth=200)