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

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]]]

你可能感兴趣的:(torch.Tensor和numpy.array增加一维并复制)