pytorch --- torch.cat()

torch.cat((tensor1,tensor2), dim)

Connecting the two tensor, the following specific examples of how to connect see

x = torch.rand((2,2,3))
y = torch.rand((2,2,3))
print("x:",x)
print("y:",y)
print("dim=0:", torch.cat((x,y),dim=0))
print("dim=1:", torch.cat((x,y), dim=1))
print("dim=2:", torch.cat((x, y), dim=2))

Output:

x: tensor([[[0.2571, 0.9011, 0.7935],
         [0.9308, 0.3267, 0.3290]],

        [[0.6155, 0.4739, 0.7251],
         [0.8025, 0.0424, 0.8101]]])

y: tensor([[[0.8813, 0.1149, 0.7757],
         [0.4733, 0.9003, 0.3300]],

        [[0.2597, 0.5810, 0.2507],
         [0.1220, 0.2260, 0.5620]]])

dim=0: tensor([[[0.2571, 0.9011, 0.7935],
         [0.9308, 0.3267, 0.3290]],

        [[0.6155, 0.4739, 0.7251],
         [0.8025, 0.0424, 0.8101]],

        [[0.8813, 0.1149, 0.7757],
         [0.4733, 0.9003, 0.3300]],

        [[0.2597, 0.5810, 0.2507],
         [0.1220, 0.2260, 0.5620]]])

dim=1: tensor([[[0.2571, 0.9011, 0.7935],
         [0.9308, 0.3267, 0.3290],
         [0.8813, 0.1149, 0.7757],
         [0.4733, 0.9003, 0.3300]],

        [[0.6155, 0.4739, 0.7251],
         [0.8025, 0.0424, 0.8101],
         [0.2597, 0.5810, 0.2507],
         [0.1220, 0.2260, 0.5620]]])
         
dim=2: tensor([[[0.2571, 0.9011, 0.7935, 0.8813, 0.1149, 0.7757],
        		[0.9308, 0.3267, 0.3290, 0.4733, 0.9003, 0.3300]],

        [[0.6155, 0.4739, 0.7251, 0.2597, 0.5810, 0.2507],
         [0.8025, 0.0424, 0.8101, 0.1220, 0.2260, 0.5620]]])
[Finished in 2.1s]

torch.stack((tensor1, tensor2), dim)

x = torch.rand((2,2,3))
y = torch.rand((2,2,3))
print("x:",x)
print("y:",y)
print("dim=0:", torch.stack((x,y),dim=0))
print("dim=1:", torch.stack((x,y), dim=1))
print("dim=2:", torch.stack((x, y), dim=2))
print("dim=3", torch.stack((x, y), dim=3))

Output:

x: tensor([[[0.5099, 0.3434, 0.3731],
         [0.8523, 0.4672, 0.4163]],

        [[0.3364, 0.4910, 0.2302],
         [0.7896, 0.8119, 0.3978]]])

y: tensor([[[0.3843, 0.7627, 0.9757],
         [0.0065, 0.5462, 0.2765]],

        [[0.1890, 0.1698, 0.4486],
         [0.3459, 0.5552, 0.1908]]])

dim=0: tensor([[[[0.5099, 0.3434, 0.3731],
          [0.8523, 0.4672, 0.4163]],

         [[0.3364, 0.4910, 0.2302],
          [0.7896, 0.8119, 0.3978]]],


        [[[0.3843, 0.7627, 0.9757],
          [0.0065, 0.5462, 0.2765]],

         [[0.1890, 0.1698, 0.4486],
          [0.3459, 0.5552, 0.1908]]]])

dim=1: tensor([[[[0.5099, 0.3434, 0.3731],
          [0.8523, 0.4672, 0.4163]],

         [[0.3843, 0.7627, 0.9757],
          [0.0065, 0.5462, 0.2765]]],


        [[[0.3364, 0.4910, 0.2302],
          [0.7896, 0.8119, 0.3978]],

         [[0.1890, 0.1698, 0.4486],
          [0.3459, 0.5552, 0.1908]]]])
          
dim=2: tensor([[[[0.5099, 0.3434, 0.3731],
          [0.3843, 0.7627, 0.9757]],

         [[0.8523, 0.4672, 0.4163],
          [0.0065, 0.5462, 0.2765]]],


        [[[0.3364, 0.4910, 0.2302],
          [0.1890, 0.1698, 0.4486]],

         [[0.7896, 0.8119, 0.3978],
          [0.3459, 0.5552, 0.1908]]]])

dim=3 tensor([[[[0.5099, 0.3843],
          [0.3434, 0.7627],
          [0.3731, 0.9757]],

         [[0.8523, 0.0065],
          [0.4672, 0.5462],
          [0.4163, 0.2765]]],


        [[[0.3364, 0.1890],
          [0.4910, 0.1698],
          [0.2302, 0.4486]],

         [[0.7896, 0.3459],
          [0.8119, 0.5552],
          [0.3978, 0.1908]]]])
[Finished in 2.2s]

Note the difference between stack and the cat

  • After the stack operation will further increase the basis of the original one-dimensional, such as the original two-dimensional tensor dimensions are 3, through the stack as the result of 4-dimensional tensor; cat operation result and the original consistency tensor
  • How to connect two particular stack and cat see example above tensor
Published 33 original articles · won praise 1 · views 2612

Guess you like

Origin blog.csdn.net/orangerfun/article/details/104012365