基本类型
| Data type | CPU tensor | GPU tensor |
|---|---|---|
| 32-bit floating point | torch.FloatTensor | torch.cuda.FloatTensor |
| 64-bit floating point | torch.DoubleTensor | torch.cuda.DoubleTensor |
| 16-bit floating point | N/A | torch.cuda.HalfTensor |
| 8-bit integer(unsigned) | torch.ByteTensor | torch.cuda.ByteTensor |
| 8-bit integer(signed) | torch.CharTensor | torch.cuda.CharTensor |
| 16-bit integer(signed) | torch.ShortTensor | torch.cuda.ShortTensor |
| 32-bit integer(signed) | torch.IntTensor | torch.cuda.IntTensor |
| 64-bit integer(signed) | torch.LongTensor | torch.cuda.LongTensor |
torch.Tensor是默认的tensor类型torch.FloatTensor的简称。
类型检测
Pytorch数据类型的检测可以通过三个方式:
1)Python的内置函数type()
2)Tensor的成员函数Tensor.type()
3)Pytorch的工具函数isinstance()
示例代码 test.py
import torch
a = torch.randn(2, 3)
print(a)
print(type(a))
print(a.type())
print(isinstance(a, torch.FloatTensor))
终端命令行及运行结果
<user>python test.py
tensor([[-0.0454, 0.7095, -1.4267],
[-0.5969, 0.7630, 1.2615]])
<class 'torch.Tensor'>
torch.FloatTensor
True
从运行结果来看:
(1)Python的内置函数type()只能检测到Tensor类型。
(2)Tensor的成员函数Tensor.type()能检测到Tensor类型的具体类型。
(3)Pytorch的工具函数isinstance()能判断某数据是否属于某个数据类型。
本文详细介绍了PyTorch中的各种数据类型,包括基本类型如CPUtensor和GPUtensor,以及不同精度的浮点数和整数类型。同时,文章提供了类型检测的方法,包括使用Python的内置函数type(),Tensor的成员函数Tensor.type(),以及Pytorch的工具函数isinstance()。
77

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



