Torch - 如何更改张量类型?

Torch - How to change tensor type?

我创建了从 1 到 3 的数字排列。

th> y = torch.randperm(3 );
th> y
 3
 2
 1
[torch.DoubleTensor of size 3]

现在,我想将 y 转换为 Torch.LongTensor。我该怎么做?

y = y.long() 完成任务。其他数据类型也有类似的方法,如intcharfloatbyte.

您可以检查不同的 dtype here

对于pytorch用户,由于在google中搜索在pytorch中更改张量类型会导致此页面,您可以这样做:

y = y.type(torch.LongTensor)

使用.to手电筒方法如下:

y = y.to(torch.long)

有关 torch 张量的更多详细信息 type/ops 可在此处找到

https://pytorch.org/docs/stable/tensors.html