import torch
input=torch.Tensor([[1,-1],
[-1,3]])
print(input.type())
代码运行得到的结果:
torch.FloatTensor
如果将Tensor更改成tensor,则代码运行的结果:
torch.LongTensor
根据查阅的资料,以及自己实践运行的结果,可知:Tensor得到的张量的数据类型默认是FloatTensor;而tensor得到的张量的数据类型默认是LongTensor。
如果在代码中,分别指定数据类型会有怎样的结果呢?
import torch
input=torch.tensor([[1,-1],
[-1,3]],dtype=torch.float64)
print(input.type())
运行结果:
torch.DoubleTensor
但是对于Tensor函数,则出现报错:
new() received an invalid combination of arguments - got (list, dtype=torch.dtype), but expected one of:
* (*, torch.device device)
didn't match because some of the keywords were incorrect: dtype
* (torch.Storage storage)
* (Tensor other)
* (tuple of ints size, *, torch.device device)
* (object data, *, torch.device device)
Tensor输出的数据类型不能被改变,tensor的数据类型可以根据需要变化。
在PyTorch中,`Tensor`创建的张量默认为`FloatTensor`,而`tensor`创建的张量默认为`LongTensor`。通过指定`dtype`参数,可以改变`tensor`创建的张量数据类型,如`torch.DoubleTensor`。然而,尝试用`dtype`参数改变`Tensor`创建的数据类型会导致错误,因为`Tensor`不接受这个参数。
2728

被折叠的 条评论
为什么被折叠?



