【问题标题】:How do I mutate the input using gradient descent in PyTorch?如何在 PyTorch 中使用梯度下降来改变输入?
【发布时间】:2020-12-30 23:43:43
【问题描述】:

我是 PyTorch 的新手。我了解到它使用autograd 自动计算梯度下降函数的梯度。

我不想调整权重,而是想使用梯度下降来改变输入以实现所需的输出。因此,我不想改变神经元的权重,而是想保持所有的权重相同,只改变输入以最小化损失。

例如。该网络是经过训练的图像分类器,数字为 0-9。我输入随机噪声,我想对其进行变形,以便网络以 60% 的置信度将其视为3。我想利用梯度下降来调整输入的值(最初是噪声),直到网络认为输入是3,置信度为 60%。

有没有办法做到这一点?

【问题讨论】:

  • 只有一个网络是不可能的。你需要像 GAN(生成对抗网络)这样的东西,它是 2 个网络的组合,才能完成这项工作。
  • 是的,你可以。看看这个official tutorial

标签: python pytorch


【解决方案1】:

我假设您知道如何使用梯度下降进行常规训练。您只需要更改要由优化器优化的参数。类似的东西

# ... Setup your network, load the input
# ...

# Set proper requires_grad -> We train the input, not the parameters
input.requires_grad = True
for p in network.parameters():
    p.requires_grad = False

# Setup the optimizer
# Previously we should have SomeOptimizer(net.parameters())
optim = SomeOptimizer([input])

output_that_you_want = ...
actual_output = net(input)
some_loss = SomeLossFunction(output_that_you_want, actual_output)
# ...
# Back-prop and optim.step() as usual

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 2015-05-08
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    相关资源
    最近更新 更多