最全面的官网说明:https://pytorch.org/docs/stable/tensors.html?highlight=item#
本文就平时用到的简单写一下,做备忘。
1. torch.Tensor就是一个多维矩阵,其元素均为一种数据类型。torch定义了9中CPU tensor类型和9中GPU tensor类型。具体看官网。
2. python 列表或数组->tensor 转换函数torch.tensor()
3. tensor的item()可以将tensor->python number
item() -> number
Returns the value of this tensor as a standard Python number. This only works for tensors with one element. For other cases, see tolist()
.
This operation is not differentiable.
Example:
>>> x = torch.tensor([1.0]) >>> x.item() 1.0
tolist() -> list or number
Returns the tensor as a (nested) list. For scalars, a standard Python number is returned, just