pytorch张量操作基础

张量是pytorch的基本数据类型,因此有必要重新快速的学习下怎么使用操作张量。

1:使用tensor直接创建

b = torch.tensor([[11, 22, 33, 66], [77, 44, 88, 99]])
print(b)
b = torch.tensor([11, 22, 33, 66, 77, 44, 88, 99])
print(b)

结果输出是:
tensor([[11, 22, 33, 66],
[77, 44, 88, 99]])
tensor([11, 22, 33, 66, 77, 44, 88, 99])

2:从numpy中导入

a = np.array([2, 5.5])
print(a)
b = torch.from_numpy(a)
print(b)

a = np.array([[2, 5.5], [1, 3]])
print(a)
b = torch.from_numpy(a)
print(b)

输出结果是:
[2. 5.5]
tensor([2.0000, 5.5000], dtype=torch.float64)
[[2. 5.5]
[1. 3. ]]
tensor([[2.0000, 5.5000],
[1.0000, 3.0000]], dtype=torch.float64)

3:从一些分布函数中得到

a = torch.randn(3, 3)  # [0,1]之间正太分布
print(a)
a = torch.rand(3, 3)  # [0,1]之间均匀分布
print(a)
b = torch.rand_like(a)  # 创建一个与a shape相同的数组
print(b)
c = torch.randint(1, 10, (3, 3))  # [0, 10)之间,3*3形状的整数
print(c)

结果输出是:
tensor([[-0.1965, -1.2576, 0.9081],
[ 0.2345, 1.3528, 1.5679],
[-1.2760, -0.9808, 0.5914]])
tensor([[0.6441, 0.5676, 0.7393],
[0.0952, 0.9967, 0.8745],
[0.1752, 0.0379, 0.0169]])
tensor([[0.7620, 0.1802, 0.1150],
[0.5347, 0.6680, 0.5420],
[0.3381, 0.3097, 0.4028]])
tensor([[2, 8, 3],
[9, 1, 9],
[7, 4, 4]])

4:同一个数值

a = torch.full([2, 3], 7)  # 2行3列,全是7
print(a)

结果是:
tensor([[7, 7, 7],
[7, 7, 7]])

5:在一定范围带有步长的值

a = torch.arange(0, 10, 2)  # 0到10,步长为2
print(a)
a = torch.linspace(0, 10, steps=5)  # 0到10,分5份
print(a)

结果是:
tensor([0, 2, 4, 6, 8])
tensor([ 0.0000, 2.5000, 5.0000, 7.5000, 10.0000])

6:全0,全1,对角阵

a = torch.ones(3, 3)  # 3*3,全1矩阵
print(a)
a = torch.zeros(3, 3)  # 3*3,全0矩阵
print(a)
a = torch.eye(3, 3)  # 3*3,对角矩阵
print(a)

结果是:
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])

7:对张量进行下标选择,其中… 表示所有

a = torch.rand(5, 3, 28, 28)
print(a.shape)
b = a[:, :, 0:28:2, 0:28:2]  # 步长为2,对2,3维度进行索引
print(b.shape)
c = a.index_select(2, torch.arange(8))  # 对第2维度进行索引,范围为0-7
print(c.shape)
d = a[..., 0:28:2]  # 步长为2,对2,3维度进行索引
print(d.shape)
e = a[2:5, ..., 0:28:2]  # 步长为2,对2,3维度进行索引
print(e.shape)

结果是:
torch.Size([5, 3, 28, 28])
torch.Size([5, 3, 14, 14])
torch.Size([5, 3, 8, 28])
torch.Size([5, 3, 28, 14])
torch.Size([3, 3, 28, 14])

8:改变张量的维度信息

a = torch.rand(5, 3, 4, 4)
print(a.shape)
b = a.view(-1)
print(b.shape)
b = a.view(5, -1)
print(b.shape)
b = a.view(5, 3, -1)
print(b.shape)
b = a.view(5, -1, 4)
print(b.shape)

结果是:
torch.Size([5, 3, 4, 4])
torch.Size([240])
torch.Size([5, 48])
torch.Size([5, 3, 16])
torch.Size([5, 12, 4])

9:张量乘法和加法

a = torch.rand(2, 3)
print(a)
b = torch.rand(2, 3)
print(b)
print(a.add(b))  # 矩阵加法
print(a + b)  # 矩阵加法
print(a.mm(b.T))  # 矩阵乘法

结果是:
tensor([[1., 2., 3.],
[6., 5., 4.]])
tensor([[4., 1., 3.],
[9., 7., 2.]])
tensor([[ 5., 3., 6.],
[15., 12., 6.]])
tensor([[ 5., 3., 6.],
[15., 12., 6.]])
tensor([[15., 29.],
[41., 97.]])

类似可以得到类似的Div和Sub函数的操作。

10:聚合运算
Max/min:沿行/列取最大/最小值,dim=0表示列,dim=1表示行。返回值和索引。
Sum:沿行/列取求和,dim=0表示列,dim=1表示行

a = torch.Tensor([[1, 2, 3], [6, 5, 4]])
print(a.shape)
min_val, min_idx = torch.min(a, dim=0)  # 按列求最小值
print(min_val)
print(min_idx)
max_val, max_idx = torch.max(a, dim=1)  # 按行求最大值
print(max_val)
print(max_idx)
sum_idx = torch.sum(a, dim=0)  # 按列求和
print(sum_idx)
sum_idx = torch.sum(a, dim=1)  # 按行求和
print(sum_idx)

结果是
torch.Size([2, 3])
tensor([1., 2., 3.])
tensor([0, 0, 0])
tensor([3., 6.])
tensor([2, 0])
tensor([7., 7., 7.])
tensor([ 6., 15.])

11:张量的操作函数
1):cat。功能:将张量按维度dim进行拼接
• tensors: 张量序列 • dim : 要拼接的维度

low_layer_features = torch.rand(2, 3, 3, 2)
deep_layer_features = torch.rand(2, 3, 3, 2)
y = torch.cat([low_layer_features, deep_layer_features], dim=0)
print(y.shape)
y = torch.cat([low_layer_features, deep_layer_features], dim=1)
print(y.shape)
y = torch.cat([low_layer_features, deep_layer_features], dim=2)
print(y.shape)
y = torch.cat([low_layer_features, deep_layer_features], dim=3)
print(y.shape)

结果是:
torch.Size([4, 3, 3, 2])
torch.Size([2, 6, 3, 2])
torch.Size([2, 3, 6, 2])
torch.Size([2, 3, 3, 4])

2):chunk。 功能:将张量按维度dim进行平均切分
返回值:张量列表
注意事项:若不能整除,最后一份张量小于 其他张量
• input: 要切分的张量
• chunks : 要切分的份数,向上取整
• dim : 要切分的维度

a = torch.ones((2, 7))
list_of_tensors = torch.chunk(a, dim=1, chunks=3)  # 在维度1切分3份
for idx, t in enumerate(list_of_tensors):
    print("第{}个张量 shape is {}".format(idx + 1, t.shape))

结果是:
第1个张量 shape is torch.Size([2, 3])
第2个张量 shape is torch.Size([2, 3])
第3个张量 shape is torch.Size([2, 1])

3):split。功能:将张量按维度dim进行切分,和chunk类似
返回值:张量列表
• tensor: 要切分的张量
• split_size_or_sections : 为int时,表示 每一份的长度;为list时,按list元素切分
• dim : 要切分的维度

t = torch.ones((2, 5))
list_of_tensors = torch.split(t, split_size_or_sections=2, dim=1)  # int
for idx, t in enumerate(list_of_tensors):
    print("第{}个张量:shape is {}".format(idx + 1, t.shape))

结果是:
第1个张量:shape is torch.Size([2, 2])
第2个张量:shape is torch.Size([2, 2])
第3个张量:shape is torch.Size([2, 1])

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值