我想通过以下方式在PyTorch中扩展张量:
我们C
是一个3x4
张量requires_grad = True
。我想要一个新C
的3x5
张量,C = [C, ones(3,1)]
(最后一列是一个向量,其他列是旧的C
),而且,我需要requires_grad = True
new C
。
什么是有效的方法?
你可以做这样的事情-
c = torch.rand((3,4), requires_grad=True) # a random matrix c of shape 3x4
ones = torch.ones(c.shape[0], 1) # creating a vector of 1's using shape of c
# merging vector of ones with 'c' along dimension 1 (columns)
c = torch.cat((c, ones), 1)
# c.requires_grad will still return True...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句