如何获取 Pytorch 张量的数据类型?
在本文中,我们将创建一个张量并获取数据类型。 Pytorch 用于处理张量。张量是多维数组。 PyTorch 加速了张量的科学计算,因为它具有各种内置功能。
向量:
向量是一个一维张量,它包含多种数据类型的元素。我们可以使用 PyTorch 创建一个向量。 Pytorch 在Python torch 模块中可用,所以我们需要导入它
句法:
import pytorch
一维张量的创建:
使用torch.tensor() 方法创建一维向量。
句法:
torch.tensor([element1,element2,.,element n],dtype)
参数:
- dtype:指定数据类型。
dtype=torch.datatype
示例:用于创建未指定数据类型的张量元素的Python程序。
Python3
# importing torch module
import torch
# create one dimensional tensor with
# integer type elements
a = torch.tensor([10, 20, 30, 40, 50])
print(a)
# create one dimensional tensor with
# float type elements
b = torch.tensor([10.12, 20.56, 30.00, 40.3, 50.4])
print(b)
Python3
# import torch
import torch
# create a tensor with unsigned integer type of 8 bytes size
a = torch.tensor([100, 200, 2, 3, 4], dtype=torch.uint8)
# display tensor
print(a)
# display data type
print(a.dtype)
# create a tensor with integer type of 8 bytes size
a = torch.tensor([1, 2, -6, -8, 0], dtype=torch.int8)
# display tensor
print(a)
# display data type
print(a.dtype)
# create a tensor with integer type of 16 bytes size
a = torch.tensor([1, 2, -6, -8, 0], dtype=torch.int16)
# display tensor
print(a)
# display data type
print(a.dtype)
# create a tensor with integer type of 32 bytes size
a = torch.tensor([1, 2, -6, -8, 0], dtype=torch.int32)
# display tensor
print(a)
# display data type
print(a.dtype)
# create a tensor with integer type of 64 bytes size
a = torch.tensor([1, 2, -6, -8, 0], dtype=torch.int64)
# display tensor
print(a)
# display data type
print(a.dtype)
Python3
# import torch
import torch
# create a tensor with float type
a = torch.tensor([100, 200, 2, 3, 4], dtype=torch.float)
# display tensor
print(a)
# display data type
print(a.dtype)
# create a tensor with double type
a = torch.tensor([1, 2, -6, -8, 0], dtype=torch.double)
# display tensor
print(a)
# display data type
print(a.dtype)
Python3
# import torch
import torch
# create a tensor with bool type
a = torch.tensor([100, 200, 2, 3, 4], dtype=torch.bool)
# display tensor
print(a)
# display data type
print(a.dtype)
# create a tensor with bool type
a = torch.tensor([0, 0, 0, 1, 2], dtype=torch.bool)
# display tensor
print(a)
# display data type
print(a.dtype)
输出:
tensor([10, 20, 30, 40, 50])
tensor([10.1200, 20.5600, 30.0000, 40.3000, 50.4000])
支持的数据类型:
vector 支持以下数据类型:Data Type Description int8 Integer type with 8 bytes uint8 Unsigned integer type with 8 bytes int16 Integer type with 16 bytes int32 Integer type with 32 bytes int64 Integer type with 64 bytes float Data with float type(decimal) double Data with float type (64 bit) decimal bool Boolean type: returns True if the value is greater than 0, otherwise False
我们可以使用dtype 命令获取数据类型:
句法:
tensor_name.dtype
示例 1: Python程序,用于创建具有整数数据类型和显示数据类型的张量
蟒蛇3
# import torch
import torch
# create a tensor with unsigned integer type of 8 bytes size
a = torch.tensor([100, 200, 2, 3, 4], dtype=torch.uint8)
# display tensor
print(a)
# display data type
print(a.dtype)
# create a tensor with integer type of 8 bytes size
a = torch.tensor([1, 2, -6, -8, 0], dtype=torch.int8)
# display tensor
print(a)
# display data type
print(a.dtype)
# create a tensor with integer type of 16 bytes size
a = torch.tensor([1, 2, -6, -8, 0], dtype=torch.int16)
# display tensor
print(a)
# display data type
print(a.dtype)
# create a tensor with integer type of 32 bytes size
a = torch.tensor([1, 2, -6, -8, 0], dtype=torch.int32)
# display tensor
print(a)
# display data type
print(a.dtype)
# create a tensor with integer type of 64 bytes size
a = torch.tensor([1, 2, -6, -8, 0], dtype=torch.int64)
# display tensor
print(a)
# display data type
print(a.dtype)
输出:
tensor([100, 200, 2, 3, 4], dtype=torch.uint8)
torch.uint8
tensor([ 1, 2, -6, -8, 0], dtype=torch.int8)
torch.int8
tensor([ 1, 2, -6, -8, 0], dtype=torch.int16)
torch.int16
tensor([ 1, 2, -6, -8, 0], dtype=torch.int32)
torch.int32
tensor([ 1, 2, -6, -8, 0])
torch.int64
示例 2:创建浮点类型和显示数据类型。
蟒蛇3
# import torch
import torch
# create a tensor with float type
a = torch.tensor([100, 200, 2, 3, 4], dtype=torch.float)
# display tensor
print(a)
# display data type
print(a.dtype)
# create a tensor with double type
a = torch.tensor([1, 2, -6, -8, 0], dtype=torch.double)
# display tensor
print(a)
# display data type
print(a.dtype)
输出:
tensor([100., 200., 2., 3., 4.])
torch.float32
tensor([ 1., 2., -6., -8., 0.], dtype=torch.float64)
torch.float64
示例 3:创建一个布尔类型的张量
蟒蛇3
# import torch
import torch
# create a tensor with bool type
a = torch.tensor([100, 200, 2, 3, 4], dtype=torch.bool)
# display tensor
print(a)
# display data type
print(a.dtype)
# create a tensor with bool type
a = torch.tensor([0, 0, 0, 1, 2], dtype=torch.bool)
# display tensor
print(a)
# display data type
print(a.dtype)
输出:
tensor([True, True, True, True, True])
torch.bool
tensor([False, False, False, True, True])
torch.bool