Pytorch,如何扩展张量

FA *_* mn 3 python pytorch

我想通过以下方式扩展 PyTorch 中的张量:

C3x4的张量requires_grad = True。我想要一个新的张量(最后一列是单向C量 ,其他是旧的)此外,我需要新的。3x5C = [C, ones(3,1)]Crequires_grad = TrueC

什么是有效的方法来做到这一点?

小智 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)