pytorch中的tensor
程序员文章站
2022-07-13 10:07:50
...
1、什么是tensor?
tensor中文意为张量,提到张量最先能想到物理中的张量。有基础物理知识的人都知道,没有方向只有大小的量称为标量,例如普通数字1, 2, 3等,可以比较大小;既有方向又有大小的量称为矢量,例如物理概念中的作用力,不能够简单的比较大小;在物理中学习单元体应力应变状态的时候提到了张量,张量是一组能够表示某一个微元体应力应变状态的分量的集合。张量(tensor)是pytorch中最基础的一种数结构,熟悉numpy数组的话会发现tensor和numpy数组的操作很相似,但不同之处在于tensor具有更多的属性,相当于表示数组数据在运算过程中的状态,tensor中不仅仅包含了数组数据本身,还包含了dtype,grad,device等属性。
2、tensor的操作
熟悉numpy的话对tensor的操作会感觉很熟悉。导入torch和numpy,从数据创建tensor:
import torch
import numpy as np
# initialize tensor
# # from data
data = [[1, 2], [3, 4]]
tensor_data = torch.tensor(data, dtype=torch.int32)
print(tensor_data)
print(type(tensor_data))
print(tensor_data.shape)
从numpy创建tensor
# from numpy
arr1 = np.array([[4, 5, 6], [7, 8, 9]])
arr_tensor = torch.from_numpy(arr1)
print(arr_tensor)
print(type(arr_tensor))
print(arr_tensor.shape)
其它方式创建tensor:
shape = (2, 3)
rand_tensor = torch.rand(size=shape)
one_tensor = torch.ones(size=shape)
zero_tensor = torch.ones(size=shape)
print(rand_tensor.shape)
print(rand_tensor)
tensor的shape,dtype,device属性等,判断cuda,将tensor由cpu移到gpu:
# attributes of tensor
print(f'shape: {rand_tensor.shape}; dtype: {rand_tensor.dtype}; device: {rand_tensor.device}')
if torch.cuda.is_available():
print(torch.device)
tensor = rand_tensor.to('cuda')
print(tensor.device)
tensor的聚合:cat聚合方式,dim维度从要聚合的tensor的size中选择,0按行聚合,1按列聚合
# cat and stack tensor
print(rand_tensor)
# dim choose from tensor size , 0 for row, 1 for columns
cat_tensor = torch.cat([rand_tensor, rand_tensor], dim=1)
print(cat_tensor)
原:
tensor([[0.9003, 0.7300, 0.3107],
[0.8845, 0.6043, 0.0443]])
dim取0聚合:
tensor([[0.9003, 0.7300, 0.3107],
[0.8845, 0.6043, 0.0443],
[0.9003, 0.7300, 0.3107],
[0.8845, 0.6043, 0.0443]])
dim取1聚合:
tensor([[0.8397, 0.2339, 0.8829, 0.8397, 0.2339, 0.8829],
[0.7830, 0.4890, 0.0135, 0.7830, 0.4890, 0.0135]])
stack方式:与cat不同,stack以堆叠的方式进行,dim可取到size+1,此例中可取0,1,2。
stack_tensor = torch.stack([rand_tensor, rand_tensor], dim=2)
print(stack_tensor)
原:
tensor([[0.9003, 0.7300, 0.3107],
[0.8845, 0.6043, 0.0443]])
dim取0:
tensor([[[0.6554, 0.0863, 0.1121],
[0.6554, 0.0863, 0.1121]],
[[0.6638, 0.2260, 0.1568],
[0.6638, 0.2260, 0.1568]]])
dim取1:
tensor([[0.1256, 0.7978, 0.3251, 0.1256, 0.7978, 0.3251],
[0.6428, 0.6606, 0.1968, 0.6428, 0.6606, 0.1968]])
dim取2:
tensor([[[0.1256, 0.1256],
[0.7978, 0.7978],
[0.3251, 0.3251]],
[[0.6428, 0.6428],
[0.6606, 0.6606],
[0.1968, 0.1968]]])
tensor的基本运算举例
元素相乘、矩阵相乘、自加运算:
# tensor multiply
print(one_tensor)
one_tensor_mul = one_tensor.mul(one_tensor)
print(one_tensor_mul)
one_tensor_matmul = one_tensor.matmul(one_tensor.T)
print(one_tensor_matmul)
# self operations
one_tensor.add_(5)
print(one_tensor)
输出:
one_tensor:
tensor([[1., 1., 1.],
[1., 1., 1.]])
one_tensor_mul:
tensor([[1., 1., 1.],
[1., 1., 1.]])
one_tensor_matmul:
tensor([[3., 3.],
[3., 3.]])
one_tensor自加运算:
tensor([[6., 6., 6.],
[6., 6., 6.]])
上一篇: pytorch:tensor的运算
下一篇: PyTorch中的Tensor