美文网首页
torch.Tensor和numpy.array增加一维并复制

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

作者: SwordIng | 来源:发表于2020-10-26 10:38 被阅读0次
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增加一维并复制

      本文链接:https://www.haomeiwen.com/subject/jecamktx.html