在PyTorch中使用GD查找全球最小零件

时间:2018-08-25 22:37:20

标签: python regression pytorch gradient-descent non-linear-regression

我用矢量输入和标量输出(回归)训练了神经网络。 现在,我想使用带有pytorch的GD查找NN的全局最小值。

我是一般编程的新手,特别是python,甚至更特别的是pytorch。

我相信我要尝试做的事情必须已经完成了一千次,即使不是一万次。如果有人能将我指向某些代码的地方(也许在github中),我将非常高兴和感激,那里有我正在尝试做的一个示例,我可以根据自己的需要进行调整。

1 个答案:

答案 0 :(得分:0)

您已经完成了训练网络的工作,但没有更新权重,而是更新了输入:

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

更新了此内容

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