问题

用 pytorch 中的 gd 查找全局最小值


我已经训练了具有矢量输入和标量输出的 NN(回归)。现在我想使用与 Pytorch 一起使用 GD 的 NN 的全局 minimun。

我是一般而言,特别是 Python 和 Pytorch 的新手。

我相信我要做的事情必须已经完成了一千次,即使不是一千次。如果有人可以将我指向某个地方(也许在 Github)的某个代码,我将非常高兴和感激,那里有一个我想做的示例,我可以适应我的需求。

推荐答案

您正在做您所做的工作来训练网络,但您没有更新权重,而是更新输入:

input = torch.zeros([1,3,32,32], requires_grad=True) # Whatever the expected input size of your network is
output = model(input)
target = 0.0 # What kind of target should your network reach?
loss = ((target - output) ** 2) # Replace this with the loss you are using
grad = torch.autograd.grad(loss, input)

您可以将梯度(可能乘以学习率乘以学习率)并多次重复此步骤。我已经从 https://discuss.pytorch.org/t/gradient-of-loss-of-neural-network-with-with-with-recept-to-input/9482更新

您应该注意以下事实:您的网络可能会产生一个非常嘈杂的“输入”,因此您应该考虑一下初始输入的内容。Google 以前做过类似的事情,请参阅例如 https://www.networkworld.com/article/2974718/software/deep-dream-aream-arteram-artermater-intercer-intelligence-meets-hallucinations.html