我在和火把的张量一起工作。如何将对应于列向量的张量转换为对应于其转置的张量?
import numpy as np
coef = torch.from_numpy(np.arange(1.0, 5.0)).float()
print(coef)
print(coef.size())
目前,coef
的大小是[4]
,但我希望它是具有相同内容的[4, 1]
。
发布于 2018-04-16 06:55:19
它很容易在PyTorch中实现。您可以使用view()
方法。
coef = coef.view(4, 1)
print(coef.size()) # now the shape will be [4, 1]
发布于 2018-04-16 13:32:03
虽然总的来说,使用.view
肯定是一个很好的选择,但为了完整性起见,我想补充的是,还有一个.unsqueeze()
方法,它在指定的索引处添加一个额外的维度(与删除单位维的.squeeze()
方法相反):
>>> coef = coef.unsqueeze(-1) # add extra dimension at the end
>>> coef.shape
torch.Size([4, 1])
一般情况下,您可以使用.t()
方法。
https://stackoverflow.com/questions/49847984
复制相似问题