我想通过以下方式扩展 PyTorch 中的张量:
令C
为3x4
的张量requires_grad = True
。我想要一个新的张量(最后一列是单向C
量
,其他是旧的)此外,我需要新的。3x5
C = [C, ones(3,1)]
C
requires_grad = True
C
什么是有效的方法来做到这一点?
小智 5
你可以做这样的事情 -
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...
Run Code Online (Sandbox Code Playgroud)