首页 > 其他 > 详细

pytorch 之 torch.stack

时间:2021-03-03 14:55:04      阅读:22      评论:0      收藏:0      [点我收藏+]

torch.stack 堆叠一个高维的tensor

首先需要明白:
    1、标量(数)
    2、向量(一维数组)
    3、矩阵(二位数组)
    4、张量(高维数组)

下面是实验部分:

 1 import torch as t
 2 a=t.as_tensor((11,12,13)) #tuple初始化一个tensor,(11,12,13) ,采用torch.as_tensor方法
 3 b=t.tensor([21,22,23])    #list初始化一个tensor,(21,22,23),采用torch.tensor方法
 4 print(a)
 5 print(b)
 6 ab = t.stack((a,b))    #torch.stack 堆叠一个高维的tensor,dim缺省时,默认在dim=0维度上进行堆叠
 7 ab0 = t.stack((a,b),dim=0) #在dim = 0(第一维度上进行堆叠)
 8 ab1 = t.stack((a,b),dim=1) #在dim = 1(在第二维度上进行堆叠)
 9 # ab2 = t.stack((a,b),dim=2) #在dim = 2 (在第三维度上进行堆叠),此时,维度越界,报错
10 print(ab.shape)
11 print(ab0.shape)
12 print(ab1.shape)
13 print(ab0)
14 print(ab1)

在jupyter notebook中的输出结果如下:

tensor([11, 12, 13])
tensor([21, 22, 23])
torch.Size([2, 3])
torch.Size([2, 3])
torch.Size([3, 2])
tensor([[11, 12, 13],
        [21, 22, 23]])
tensor([[11, 21],
        [12, 22],
        [13, 23]])

 

 

 

pytorch 之 torch.stack

原文:https://www.cnblogs.com/regain/p/14474284.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!