pytorch Tensor和tensor的区别

参考博客:https://blog.csdn.net/tfcy694/article/details/85338745

在PyTorch中,Tensor和tensor都能用于生成新的张量:

import torch

if __name__ == '__main__':
    Tensor_long = torch.Tensor([1, 2])
    Tensor_float = torch.Tensor([1., 2.])
    tensor_long = torch.tensor([1, 2])
    tensor_float = torch.tensor([1., 2.])
    print(Tensor_long)
    print(Tensor_float)
    print(Tensor_long.dtype)
    print(Tensor_float.dtype)
    print(tensor_long)
    print(tensor_float)
    print(tensor_long.dtype)
    print(tensor_float.dtype)

结果:

tensor([1., 2.])
tensor([1., 2.])
torch.float32
torch.float32
tensor([1, 2])
tensor([1., 2.])
torch.int64
torch.float32

可以看出Tensor会强制生成float类型的张量。

tensor会根据你的输入值,选则使用int64还是float

torch.Tensor (Python class, in torch.Tensor):https://pytorch.org/docs/stable/tensors.html?highlight=torch%20tensor#torch.Tensor

torch.tensor (Python function, in torch.tensor):https://pytorch.org/docs/stable/generated/torch.tensor.html?highlight=torch%20tensor#torch.tensor

文章目录