Tensor
约 847 字大约 3 分钟
2025-10-27
import torch# 可以是array,list,tuple
# 会根据传入的数据自动推断类型
a = torch.tensor([1,2,3], dtype=torch.int32)常见的Tensor数据类型:
整数型:torch.int8、torch.int16、torch.int32、torch.int64
浮点型:torch.float16、torch.float32(默认)、torch.float64
布尔型:torch.bool
x = torch.tensor([1,2,3,4,5])
mask = x > 2
mask运行结果
tensor([False, False, True, True, True])
# 使用布尔掩码选出>2的元素
x[mask]运行结果
tensor([3, 4, 5])
# 当然也可以设置
x[x > 2] = 0
x运行结果
tensor([1, 2, 0, 0, 0])
使用随机值/固定值创建Tensor
shape = (2,3)
rand_tensor = torch.rand(shape) # 0-1之间的随机值
randn_tensor = torch.randn(shape) # 标准正态分布的随机值
ones_tensor = torch.ones(shape) # 全1
zeros_tensor = torch.zeros(shape) # 全0
two_tensor = torch.full(shape, 2) # 全2
(rand_tensor, randn_tensor, ones_tensor, zeros_tensor, two_tensor)运行结果
(tensor([[0.9226, 0.9674, 0.7888], [0.0174, 0.2961, 0.8842]]), tensor([[-0.6291, -0.0087, 0.1825], [-1.6540, -1.3331, 1.1764]]), tensor([[1., 1., 1.], [1., 1., 1.]]), tensor([[0., 0., 0.], [0., 0., 0.]]), tensor([[2, 2, 2], [2, 2, 2]]))
形状变换
x = torch.randn(4,4).reshape(2,8)
x运行结果
tensor([[ 0.2714, 0.4808, 0.8307, 1.2320, -1.2803, -0.5090, -0.5330, -0.3757], [-0.2069, -0.8447, 0.9988, -1.0224, 0.1018, 0.7757, -0.2246, -0.7806]])
x.t()运行结果
tensor([[ 0.2714, -0.2069], [ 0.4808, -0.8447], [ 0.8307, 0.9988], [ 1.2320, -1.0224], [-1.2803, 0.1018], [-0.5090, 0.7757], [-0.5330, -0.2246], [-0.3757, -0.7806]])
数学运算
a = torch.ones((2,3))
b= torch.ones((2,3))
(a + b, a - b, a * b, a / b, a @ b.t())运行结果
(tensor([[2., 2., 2.], [2., 2., 2.]]), tensor([[0., 0., 0.], [0., 0., 0.]]), tensor([[1., 1., 1.], [1., 1., 1.]]), tensor([[1., 1., 1.], [1., 1., 1.]]), tensor([[3., 3.], [3., 3.]]))
# 统计函数
torch.sum(a), torch.mean(a), torch.std(a), torch.var(a)运行结果
(tensor(6.), tensor(1.), tensor(0.), tensor(0.))
这里要注意一个反直觉的地方,让我们看一个例子:
t=111333
t = torch.tensor([[1,3],[1,3],[1,3]], dtype=torch.float32)
t.mean(dim=0) # dim=0意味着行,但并不是,torch认为你指定哪一个就要消灭哪一个维度,所以它是对列求均值运行结果
tensor([1., 3.])
原来的shape为[3,2],t.mean(dim=0)得到的shape为[2],消灭了第一个维度
# 如果你需要保持原来的维度,可以使用`keepdim=True`参数
t.mean(dim=0, keepdim=True) # 这样得到的shape为`[1,2]`运行结果
tensor([[1., 3.]])
广播机制
原则来说需要保持形状一致。 但是他会进行一个广播,比如tensorA+1它就可以对每一个元素加1
广播机制一般原则
- 维数对齐
如果两个张量的维数不一样,那么会在维数较小的张量的前面补1,直到两个张量的维数相同。
2. 扩展维度 在__维度值为1__的维度上,通过虚拟复制,让tensor的维度值相同。使用CUDA计算
在GPU上计算的速度会快几十倍
- tensor放在cuda上
- model放在cuda上
- 计算设备的一致性
- 计算结果会存放在参与计算的tensor设备上
