如何在 Torch 中扩展 dims(向张量添加维度)
How to expand dims in Torch (add dimension to tensor)
我想给张量增加维度,如numpy.expand_dims
。我该怎么做?
我可以用下面的代码来完成:
a = torch.Tensor({{1,2}, {3,4}, {5,6}}) --array with shape (3,2)
a_size_table = a:size():totable()
table.insert(a_size_table, 1, 1) -- adding `1` before first dimension
a:reshape(torch.LongStorage(a_size_table)) -- returning array with shape (1,3,2)
我觉得太复杂了(比较一下np.expand_dims(a,0)
)。可能有更好的方法吗?
有nn.utils.addSingletonDimension
个。我在这里找到了答案
https://www.bountysource.com/issues/38773074-convenience-function-to-add-a-singleton-dimension-to-a-torch-tensor
我想给张量增加维度,如numpy.expand_dims
。我该怎么做?
我可以用下面的代码来完成:
a = torch.Tensor({{1,2}, {3,4}, {5,6}}) --array with shape (3,2)
a_size_table = a:size():totable()
table.insert(a_size_table, 1, 1) -- adding `1` before first dimension
a:reshape(torch.LongStorage(a_size_table)) -- returning array with shape (1,3,2)
我觉得太复杂了(比较一下np.expand_dims(a,0)
)。可能有更好的方法吗?
有nn.utils.addSingletonDimension
个。我在这里找到了答案
https://www.bountysource.com/issues/38773074-convenience-function-to-add-a-singleton-dimension-to-a-torch-tensor