理解 tensor, cat, unsquee, stack

假设一个 tensor 如下
[ [ [ 1 , 2 ] , [ 4 , 5 ] ] , [ [ 6 , 7 ] , [ 8 , 9 ] ] ] [ \quad [\quad[1,2], [4,5]\quad] , \quad\quad [\quad[6,7], [8,9]\quad]\quad] [[[1,2],[4,5]],[[6,7],[8,9]]]

这个tensor 一共有 3 3 3维度shape 是 (2,2,2)
想知道有几个维度,数出有几层中括号就行,有几层中括号就有几维

而且,随着中括号由外向里走,维度依次增加:从 0 变为 1 变为 2。

 .
 
那么 shape 的每一个分量是怎么得到的呢?还是以上述 tensor 举例子:
第 0 维
想要看第 0 维的分量,就要先褪去一层括号,得到
[ [ 1 , 2 ] , [ 4 , 5 ] ] , [ [ 6 , 7 ] , [ 8 , 9 ] ] [\quad[1,2], [4,5]\quad] ,\\ \quad\quad [\quad[6,7], [8,9]\quad] [[1,2],[4,5]],[[6,7],[8,9]]
[ [ 1 , 2 ] , [ 4 , 5 ] ] [[1,2],[4,5]] [[1,2],[4,5]] 看成一个元素, [ [ 6 , 7 ] , [ 8 , 9 ] ] [[6,7],[8,9]] [[6,7],[8,9]] 看成是另外一个元素,所以第 0 维一共包含 2 个元素,所以第 0 维的分量是2。

 
第 1 维
想要看第 0 维的分量,随便看 [ [ 1 , 2 ] , [ 4 , 5 ] ] [\quad[1,2], [4,5]\quad] [[1,2],[4,5]] 或者 , [ [ 6 , 7 ] , [ 8 , 9 ] ] ,[\quad[6,7], [8,9]\quad] ,[[6,7],[8,9]]都行,在这里我们看
[ [ 1 , 2 ] , [ 4 , 5 ] ] [\quad[1,2], [4,5]\quad] [[1,2],[4,5]]
把它的括号褪去,得到
[ 1 , 2 ] , [ 4 , 5 ] \quad [1,2], [4,5] \quad [1,2],[4,5]
1 , 2 1,2 1,2是一个元素, 4 , 5 4,5 4,5也是一个元素,所以一共包含两个元素,所以第 1 维也是2.

 
第 2维
[ 1 , 2 ] [1,2] [1,2]举例子,褪去括号是 1 , 2 1,2 1,2,有两个元素 1 1 1 2 2 2,所以第 2 维的大小也是2。
 

举 torch.cat 的例子加以说明:

当torch.cat 的维度参数是 0

在这里插入图片描述
在这里插入图片描述

可以观察 T3 和 T1、T2的 shape,发现在第 0 维上由 2 变成 4了。

当torch.cat 的维度参数是 1,等价于-1,因为-1代表最高维度

在这里插入图片描述
在这里插入图片描述

可以观察 T3 和 T1、T2的 shape,发现在第 1 维上由 3 变成6了。

 

举 unsqueeze 的例子加以说明

在这里插入图片描述

在这里插入图片描述

unsqueeze,为了乘法

在这里插入图片描述

在这里插入图片描述
 

举 troch.stack 的例子

例子1

import torch

# creating tensors
x = torch.tensor([1., 3., 6., 10.])
y = torch.tensor([2., 7., 9., 13.])

# printing above created tensors
print("Tensor x:", x)
print("x's shape: ",x.shape,"\n")

print("Tensor y:", y)
print("y's shape: ",y.shape,"\n")
# join above tensor using "torch.stack()"
print("Default stack:")
t = torch.stack((x, y))

# print final tensor after join
print(t)
print("shape: ",t.shape,"\n")

print("Stack along dimension 0:")
t = torch.stack((x, y), dim=0)
print(t)
print("shape: ",t.shape,"\n")

print("Stack along dimension 1:")
t = torch.stack((x, y), dim=1)
print(t)
print("shape: ",t.shape,"\n")

 
输出如下

Tensor x: tensor([ 1.,  3.,  6., 10.])
x's shape:  torch.Size([4]) 

Tensor y: tensor([ 2.,  7.,  9., 13.])
y's shape:  torch.Size([4]) 

Default stack:
tensor([[ 1.,  3.,  6., 10.],
        [ 2.,  7.,  9., 13.]])
shape:  torch.Size([2, 4]) 

Stack along dimension 0:
tensor([[ 1.,  3.,  6., 10.],
        [ 2.,  7.,  9., 13.]])
shape:  torch.Size([2, 4]) 

Stack along dimension 1:
tensor([[ 1.,  2.],
        [ 3.,  7.],
        [ 6.,  9.],
        [10., 13.]])
shape:  torch.Size([4, 2]) 

例子2

import torch

# creating tensors
x = torch.tensor([[1., 3., 6.], [10., 13., 20.]])
y = torch.tensor([[2., 7., 9.], [14., 21., 34.]])

# printing above created tensors
print("Tensor x:", x)
print("x's shape: ",x.shape,"\n")

print("Tensor y:", y)
print("y's shape: ",y.shape,"\n")
# join above tensor using "torch.stack()"
print("Default stack:")
t = torch.stack((x, y))

# print final tensor after join
print(t)
print("shape: ",t.shape,"\n")

print("Stack along dimension 0:")
t = torch.stack((x, y), dim=0)
print(t)
print("shape: ",t.shape,"\n")

print("Stack along dimension 1:")
t = torch.stack((x, y), dim=1)
print(t)
print("shape: ",t.shape,"\n")

print("Stack along dimension 2:")
t = torch.stack((x, y), dim=2)
print(t)
print("shape: ",t.shape,"\n")

 
输出如下:

Tensor x: tensor([[ 1.,  3.,  6.],
        [10., 13., 20.]])
x's shape:  torch.Size([2, 3]) 

Tensor y: tensor([[ 2.,  7.,  9.],
        [14., 21., 34.]])
y's shape:  torch.Size([2, 3]) 

Default stack:
tensor([[[ 1.,  3.,  6.],
         [10., 13., 20.]],

        [[ 2.,  7.,  9.],
         [14., 21., 34.]]])
shape:  torch.Size([2, 2, 3]) 

Stack along dimension 0:
tensor([[[ 1.,  3.,  6.],
         [10., 13., 20.]],

        [[ 2.,  7.,  9.],
         [14., 21., 34.]]])
shape:  torch.Size([2, 2, 3]) 

Stack along dimension 1:
tensor([[[ 1.,  3.,  6.],
         [ 2.,  7.,  9.]],

        [[10., 13., 20.],
         [14., 21., 34.]]])
shape:  torch.Size([2, 2, 3]) 

Stack along dimension 2:
tensor([[[ 1.,  2.],
         [ 3.,  7.],
         [ 6.,  9.]],

        [[10., 14.],
         [13., 21.],
         [20., 34.]]])
shape:  torch.Size([2, 3, 2]) 


Process finished with exit code 0

 

例子3

import torch

# creating tensors
x = torch.tensor([1., 3., 6., 10.])
y = torch.tensor([2., 7., 9., 13.])
z = torch.tensor([4., 5., 8., 11.])

# printing above created tensors
print("Tensor x:", x)
print("x's shape: ",x.shape,"\n")
print("Tensor y:", y)
print("y's shape: ",y.shape,"\n")
print("Tensor z:", z)
print("z's shape: ",z.shape,"\n")

print("Default stack:")
t = torch.stack((x, y, z))
# print final tensor after join
print(t)
print("shape: ",t.shape,"\n")

print("Stack along dimension 0:")
t = torch.stack((x, y, z), dim=0)
print(t)
print("shape: ",t.shape,"\n")

print("Stack along dimension 1:")
t = torch.stack((x, y, z), dim=1)
print(t)
print("shape: ",t.shape,"\n")

输出如下:

Tensor x: tensor([ 1.,  3.,  6., 10.])
x's shape:  torch.Size([4]) 

Tensor y: tensor([ 2.,  7.,  9., 13.])
y's shape:  torch.Size([4]) 

Tensor z: tensor([ 4.,  5.,  8., 11.])
z's shape:  torch.Size([4]) 

Default stack:
tensor([[ 1.,  3.,  6., 10.],
        [ 2.,  7.,  9., 13.],
        [ 4.,  5.,  8., 11.]])
shape:  torch.Size([3, 4]) 

Stack along dimension 0:
tensor([[ 1.,  3.,  6., 10.],
        [ 2.,  7.,  9., 13.],
        [ 4.,  5.,  8., 11.]])
shape:  torch.Size([3, 4]) 

Stack along dimension 1:
tensor([[ 1.,  2.,  4.],
        [ 3.,  7.,  5.],
        [ 6.,  9.,  8.],
        [10., 13., 11.]])
shape:  torch.Size([4, 3]) 

例子4

注意,被执行 stack 的两个 tensor 的shape 必须完全一致,不然会报错!

import torch

# creating tensors
x = torch.tensor([1., 3., 6., 10.])
y = torch.tensor([2., 7., 9.])

# printing above created tensors
print("Tensor x:", x)
print("Tensor y:", y)

# join above tensor using "torch.stack()"
print("Default stack")
t = torch.stack((x, y))
# print final tensor after join
print(t)

print("Stack along dimension 0:")
t = torch.stack((x, y), dim=0)
print(t)

print("Stack along dimension 1:")
t = torch.stack((x, y), dim=1)
print(t)

输出如下:

Tensor x: tensor([ 1.,  3.,  6., 10.])
Tensor y: tensor([2., 7., 9.])
Default stack
Traceback (most recent call last):
  File "", line 13, in <module>
    t = torch.stack((x, y))
RuntimeError: stack expects each tensor to be equal size, but got [4] at entry 0 and [3] at entry 1

cat和stack不同

  • 2个 tensor 执行cat,新生成 tensor 相比原始 tesnsor 维度不会增加,但是 shape 的分量值会改变,比如两个 shape 都是 (2,3) 的tensor 沿着 0 维 cat,那么得到的新的 tensor 的shape将会是 (4,3)
  • 两个 tensor 执行 stack,得到的新的 tensor相比元素 tensor维度会增加。比如比如 2 个 shape 都是 (2,3) 的tensor 沿着 0 维 stack,那新生成的 tensor 的shape将会是 (2,2,3)。新生成的 tensor 的 shape 的第 0 维的值是 2 是因为 参与 stack 的 tensor 数目是 2。如果有 3 个 tensor 参与 stack,沿着 0 维,那么新 tensor 的shape将会是 (3,2,3)。
  • 注意 tensor 执行 stack 新生成的 tensor 的 shape 某一个维度的值会等于参与 stack 的tensor 的数目。有 2 个tensor 参与 stack,那么就会是 2,有 3个,就会是 3.
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

培之

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值