当前位置:网站首页>Element concatenation operations in numpy and pytorch: stack, concatenat, cat
Element concatenation operations in numpy and pytorch: stack, concatenat, cat
2022-07-31 05:31:00 【Cheng-O】
Splicing of ndarray in numpy
numpy.concatenate
Concatenate multiple matrices along an existing dimension
Requirement: The number of dimensions of the two tensors needs to be the same, and the shapes of other dimensions need to be the same except for the spliced dimension.
np.concatenate((,),axis=)'''The first parameter is a tuple of matrices to be concatenatedThe second parameter is the dimension to be spliced'''a = np.arange(3*3).reshape((3,3))b = np.arange(3*4).reshape((3,4))a,b(array([[0, 1, 2],[3, 4, 5],[6, 7, 8]]),array([[ 0, 1, 2, 3],[ 4, 5, 6, 7],[ 8, 9, 10, 11]]))np.concatenate([a,b],axis=1)array([[ 0, 1, 2, 0, 1, 2, 3],[ 3, 4, 5, 4, 5, 6, 7],[ 6, 7, 8, 8, 9, 10, 11]])
numpy.stack
Stack matrices of the same dimension and shape on a new dimension
Requirement: The matrices have exactly the same dimensions
np.stack((,), axis=)'''The parameters are the same as the function above'''x1 = np.arange(9).reshape((3,3))x2 = np.arange(10,19,1).reshape((3,3))y2 = np.stack((x1,x2),axis=0)output:[[[ 0 1 2][ 3 4 5 ][ 6 7 8 ]][[10 11 12][13 14 15][16 17 18]]]'y2.shape': (2,3,3)
PS:
np.hstack(tup) = np.concatenate(tup, axis=1)
np.vstack(tup) = np.concatenate(tup, axis=0)
Splicing of ndarrays in pytorch
There are also two functions in pytorch that are the same as the two operations in numpy, namely:
torch.cat((,), dim=) -> numpy.concatenate((,), axis=)
torch.stack((,), dim=) -> numpy.stack((,), axis=)
Difference:
torch concatenates tensor tensors, the second parameter of the function is dim=
numpy concatenates ndarray, the second parameter of the function is axis=
边栏推荐
猜你喜欢
MYSQL一站式学习,看完即学完
Apache DButils使用注意事项--with modifiers “public“
Simple read operation of EasyExcel
12个MySQL慢查询的原因分析
Sql解析转换之JSqlParse完整介绍
1. 获取数据-requests.get()
Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
CentOS7 —— yum安装mysql
Kubernetes 证书可用年限修改
Distributed transaction processing solution big PK!
随机推荐
What are the advantages and disadvantages of Unity shader forge and the built-in shader graph?
剑指offer专项突击版 --- 第 4 天
CentOS7 - yum install mysql
STM32——DMA
再见了繁琐的Excel,掌握数据分析处理技术就靠它了
MySQL-如何分库分表?一看就懂
12 reasons for MySQL slow query
Centos7 install mysql5.7
datagrip带参sql查询
SQL行列转换
Unity mobile game performance optimization series: performance tuning for the CPU side
torch.normal函数用法
<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍
The interviewer asked me how to divide the database and the table?Fortunately, I summed up a set of eight-part essays
mysql 的简单运用命令
Input length must be multiple of 8 when decrypting with padded cipher
Minio upload file ssl certificate is not trusted
CentOS7 —— yum安装mysql
C语言教程(一)-准备
[mysql improves query efficiency] Mysql database query is slow to solve the problem