0

我有一个非常简单的 Deep Dream 算法实现,灵感来自 kadenze 示例https://github.com/pkmital/CADL/blob/master/session-4/lecture-4.ipynb

layer = graph.get_tensor_by_name('inception/output2:0')
layer_size = layer.eval({x: img_4d}).shape

neuron_i = 110

layer_activation = np.zeros(layer_size)
layer_activation[..., neuron_i] = 1

grad = tf.gradients(layer[..., neuron_i], x)[0]

img_noise_4d = img_noise.copy()[np.newaxis]
img_noise_4d /=  255.0

grad_step = 4.0
n_steps = 100
for step in range(n_steps):
    print(step, end=', ')
    res = grad.eval({layer: layer_activation, x: img_noise_4d})
    res /= (np.max(np.abs(res)) + 1e-8)
    img_noise_4d += res * grad_step

plt.imshow(normalize(img_noise_4d[0]))

我不明白它是如何工作的——我的意思是我们如何用我们生成的 ( layer_activation) 替换实际的层激活并获得正确的梯度?

我做了一个简单的实验:

x = tf.Variable(3.0)
y = x**2

session = tf.InteractiveSession()
session.run(tf.global_variables_initializer())

session.run(tf.gradients(y, x)[0], {y: 100})

无论我用什么替代y- 我总是得到正确的梯度x3.06.0. 我知道我遗漏了一些东西,但究竟是什么?

4

1 回答 1

0

我想我现在可以回答我的问题了——事实证明我有一个不好的例子。

这个答案更好地展示了它是如何工作的:

x = tf.Variable(3.0)
w = tf.Variable([6.0, 2.0])
y = x * w

session = tf.InteractiveSession()
session.run(tf.global_variables_initializer())

session.run(
    tf.gradients(tf.reduce_max(y), x)[0], 
    {y: [3, 9],
})

因此,基本上通过将自定义传递ysession.run我们可以向反向传播算法建议我们期望哪个“神经元”是最大的 - 因此它将计算梯度而不是实际的一个(y[0]),而是自定义一个(y[1])。

如果我们知道我们感兴趣的特定神经元,我们可以做得更简单:

session.run(
    tf.gradients(y[1], x)[0], 
)
于 2017-04-02T22:08:35.127 回答