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
相关问题 更多 >
编程相关推荐