【发布时间】:2019-04-18 15:24:44
【问题描述】:
我是 PyTorch 的新手,想做一件我认为很简单但遇到很多困难的事情。
我有函数sin(x) * cos(x) + x^2,我想在任何时候得到该函数的导数。
如果我做到这一点,它可以完美地发挥作用
x = torch.autograd.Variable(torch.Tensor([4]),requires_grad=True)
y = torch.sin(x)*torch.cos(x)+torch.pow(x,2)
y.backward()
print(x.grad) # outputs tensor([7.8545])
但是,我希望能够将向量作为 x 传递,并让它按元素计算导数。例如:
Input: [4., 4., 4.,]
Output: tensor([7.8545, 7.8545, 7.8545])
但我似乎无法让它工作。
我试着简单地做
x = torch.tensor([4., 4., 4., 4.], requires_grad=True)
out = torch.sin(x)*torch.cos(x)+x.pow(2)
out.backward()
print(x.grad)
但我收到错误“RuntimeError: grad can be implicitly created only for scalar outputs”
如何为向量调整此代码?
提前致谢,
【问题讨论】:
标签: python pytorch derivative autodiff