简体   繁体   中英

how to expand the dimensions of a tensor in pytorch

i'm a newcomer for pytorch. if i have a tensor like that:

A = torch.tensor([[1, 2, 3], [ 4, 5, 6]]) ,

but my question is how to get a 2 dimensions tensor like:

B =  Tensor([[[1, 2, 3],
                           [4, 5, 6]], 

                          [[1, 2, 3], 
                           [4, 5, 6]]])

You can concatenate ...

A
tensor([[[1., 2., 3.],
         [4., 5., 6.]]])
B = torch.cat((a, a))

B
tensor([[[1., 2., 3.],
         [4., 5., 6.]],

        [[1., 2., 3.],
         [4., 5., 6.]]])

只需像这样使用重复功能

B = A.repeat(2, 1, 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM