火炬。我如何扩展张量的维度(从 [[1, 2, 3]] 到 [[1, 2, 3, 4]])?
Pytorch. How I can expand dimension in tensor (from [[1, 2, 3]] to [[1, 2, 3, 4]])?
x = torch.tensor([1, 2, 3])
print(x)
# some operatons?
print(x)
输出:
tensor([[ 1, 2, 3]])
tensor([[ 1, 2, 3, 4]])
我尝试了 tensor.expand() 方法,但没有成功...
我找到了解决方案...
>>> x = torch.tensor([[1, 2, 3]])
>>> print(x)
>>> print(x.size())
tensor([[1, 2, 3]])
torch.Size([1, 3])
>>> x = x.repeat_interleave(torch.tensor([1, 1, 2]))
>>> print(x)
>>> print(x.size())
tensor([1, 2, 3, 3])
torch.Size([4])
>>> x[3] = 4
>>> x = x.unsqueeze(0)
>>> print(x)
>>> print(x.size())
tensor([[1, 2, 3, 4]])
torch.Size([1, 4])
x = torch.tensor([1, 2, 3])
print(x)
# some operatons?
print(x)
输出:
tensor([[ 1, 2, 3]])
tensor([[ 1, 2, 3, 4]])
我尝试了 tensor.expand() 方法,但没有成功...
我找到了解决方案...
>>> x = torch.tensor([[1, 2, 3]])
>>> print(x)
>>> print(x.size())
tensor([[1, 2, 3]])
torch.Size([1, 3])
>>> x = x.repeat_interleave(torch.tensor([1, 1, 2]))
>>> print(x)
>>> print(x.size())
tensor([1, 2, 3, 3])
torch.Size([4])
>>> x[3] = 4
>>> x = x.unsqueeze(0)
>>> print(x)
>>> print(x.size())
tensor([[1, 2, 3, 4]])
torch.Size([1, 4])