在 PyTorch 中使用 GD 查找全局最小值



我已经训练了一个带有向量输入和标量输出(回归(的NN。 现在我想使用 GD 和 pytorch 找到 NN 的全局最小值。

我是一般编程的新手,特别是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-respect-to-input/9482 更新了这个

您应该注意这样一个事实,即您的网络可能会产生非常嘈杂的"输入",因此您应该考虑您的初始输入应该是什么。谷歌以前做过类似的事情,例如 https://www.networkworld.com/article/2974718/software/deep-dream-artificial-intelligence-meets-hallucinations.html

最新更新