Usage of torch.cat ( ) and np.concatenate()

1. torch.cat()

1. Usage:

        Splice the two terson data together according to the specified dimensions;

2. Example:

improt torch

A = torch.tensor(np.array([[1,2,3],[4,5,6]])) #2×3的张量
tensor([[1, 2, 3],
        [4, 5, 6]])
B = torch.tensor(np.array([[7,8],[10,11]])) #2×2的张量
tensor([[ 7,  8],
        [10, 11]])
C = torch.cat([A, B],dim=1) #按照张量维度1(列)进行拼接,维度变为2×5
tensor([[ 1,  2,  3,  7,  8], 
        [ 4,  5,  6, 10, 11]])
D = torch.tensor(np.array([[2,3,4],[10,11,12],[5,6,7]])) #3×3的张量
tensor([[ 2,  3,  4],
        [10, 11, 12],
        [ 5,  6,  7]])
E = torch.cat([A,D], dim=0) #按照张量维度0(行)进行拼接,维度变为5×3
tensor([[ 1,  2,  3],
        [ 4,  5,  6],
        [ 2,  3,  4],
        [10, 11, 12],
        [ 5,  6,  7]])


The two tensors A and B are 2 rows and 3 columns, and 2 rows and 2 columns respectively. That is, they are all 2-dimensional tensors. Because there are only two dimensions, there are two splicing methods when using torch.cat: splicing by rows and splicing by columns. The so-called dimension 0 and dimension 1. 

C=torch.cat((A,B),1) means splicing A and B according to dimension 1 (column), that is, splicing them horizontally, with A on the left and B on the right. At this time, you need to pay attention: the number of rows must be consistent, that is, the value of dimension 0 must be the same, here they are all 2 rows, so that the rows can be aligned. The first dimension of C after splicing is the numerical sum of the two dimensions 1, that is, 2+3=5.

E=torch.cat((A,D),0) means splicing A and B according to dimension 0 (row), that is, splicing them vertically, with A above and B below. At this time, you need to pay attention: the number of columns must be consistent, that is, the value of dimension 0 must be the same. Here, there are 3 columns, so that the columns can be aligned. The 0th dimension of C after splicing is the sum of the 0 values ​​of the two dimensions, that is, 2+3=5.

Note: When using torch.cat((A,B),dim), except that the dim value of the splicing dimension can be different, the other dimension values ​​must be the same in order to be aligned.

2. np.concatenate() 

1. Usage:

        np.concatenate is a function in numpy that concatenates arrays.

2. Example:

import numpy as np

#创建2×3的数组A,B
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])

A:[[1 2 3]
   [4 5 6]]
B:[[ 7  8  9]
   [10 11 12]]
con = np.concatenate([A,B],axis=0)  #按照0维度拼接,得到4×3的数组
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
con1 = np.concattenate([A,B],AXIS=1) #按照1维度拼接,得到2×6的数组
[[ 1  2  3  7  8  9]
 [ 4  5  6 10 11 12]]

The axis parameter specifies which dimension to splice. In the above example, A is [2,3] and B is [2,3]. Setting axis=0 means splicing according to the first dimension. The size after splicing is [4 ,3] Except for the change in the size of the first dimension, the other dimensions remain unchanged. It also means that the size of other dimensions must be ensured to be correct. Suppose A is [5,4] and B is [5,3] , if axis=1 is also set here, an error will be reported because the second dimensions of x1 and x2 are not equal and cannot be spliced.
 

Guess you like

Origin blog.csdn.net/m0_62278731/article/details/131172463