torch.Tensor和numpy.array增加一维并复制

2020-10-26  本文已影响0人  SwordIng
import numpy as np
import torch

x = torch.Tensor([[1,3], [2,3], [3,4]])
x=x.unsqueeze(1)
y= x.expand(3,3,2)
torch
print(y)
tensor([[[1., 3.],
         [1., 3.],
         [1., 3.]],
        [[2., 3.],
         [2., 3.],
         [2., 3.]],
        [[3., 4.],
         [3., 4.],
         [3., 4.]]])

a=np.array([[1,0,1],[1,1,0],[1,0,1]])
b= np.expand_dims(a,1).repeat(5,axis=1)
print(b)
[[[1 0 1]
  [1 0 1]
  [1 0 1]
  [1 0 1]
  [1 0 1]]
 [[1 1 0]
  [1 1 0]
  [1 1 0]
  [1 1 0]
  [1 1 0]]
 [[1 0 1]
  [1 0 1]
  [1 0 1]
  [1 0 1]
  [1 0 1]]]
上一篇 下一篇

猜你喜欢

热点阅读