評價此頁

torch.Tensor.tolist#

Tensor.tolist() list number#

將張量返回為(巢狀)列表。對於標量,將返回標準的 Python 數字,就像使用 item() 一樣。如果需要,張量會自動先移動到 CPU。

此操作不可微分。

示例

>>> a = torch.randn(2, 2)
>>> a.tolist()
[[0.012766935862600803, 0.5415473580360413],
 [-0.08909505605697632, 0.7729271650314331]]
>>> a[0,0].tolist()
0.012766935862600803