如何获得相对于输入的keras模型的梯度? [英] How do I get the gradient of a keras model with respect to its inputs?

查看:924
本文介绍了如何获得相对于输入的keras模型的梯度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是问了一个关于同一主题的问题,但针对的是自定义模型(

I just asked a question on the same topic but for custom models (How do I find the derivative of a custom model in Keras?) but realised quickly that this was trying to run before I could walk so that question has been marked as a duplicate of this one.

我已经尝试简化场景,现在有一个(非自定义的)keras模型,该模型由2个Dense层组成:

I've tried to simplify my scenario and now have a (not custom) keras model consisting of 2 Dense layers:

inputs = tf.keras.Input((cols,), name='input')

layer_1 = tf.keras.layers.Dense(
        10,
        name='layer_1',
        input_dim=cols,
        use_bias=True,
        kernel_initializer=tf.constant_initializer(0.5),
        bias_initializer=tf.constant_initializer(0.1))(inputs)

outputs = tf.keras.layers.Dense(
        1,
        name='alpha',
        use_bias=True,
        kernel_initializer=tf.constant_initializer(0.1),
        bias_initializer=tf.constant_initializer(0))(layer_1)

model = tf.keras.Model(inputs=inputs, outputs=outputs)

prediction = model.predict(input_data)
# gradients = ...

现在,我想知道对于inputs = input_dataoutputs关于inputs的导数.

Now I would like to know the derivative of outputs with respect to inputs for inputs = input_data.

到目前为止,我已经尝试过:

对另一个问题的答案建议运行grads = K.gradients(model.output, model.input).但是,如果我运行该命令,则会收到此错误;

This answer to a different question suggests running grads = K.gradients(model.output, model.input). However, if I run that I get this error;

启用急切执行时,不支持

tf.gradients.使用 改用tf.GradientTape.

tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead.

我只能假设这与急切执行有关,因为它已成为默认设置.

I can only assume this is something to do with eager execution now being the default.

另一种方法是回答我关于定制keras模型的问题,其中涉及添加以下内容:

Another approach was in the answer to my question on custom keras models, which involved adding this:

with tf.GradientTape() as tape:
    x = tf.Variable(np.random.normal(size=(10, rows, cols)), dtype=tf.float32)
    out = model(x)

对于这种方法,我不了解的是我应该如何加载数据.它要求xvariable,但是我的xtf.keras.Input对象.我也不明白with语句在做什么,某种魔术,但我不明白.

What I don't understand about this approach is how I'm supposed to load the data. It requires x to be a variable, but my x is a tf.keras.Input object. I also don't understand what that with statement is doing, some kind of magic but I don't understand it.

这里有一个听起来很相似的问题:使用Keras Tensorflow 2.0 ,尽管应用程序和场景有很大的不同,但我很难将答案应用于此场景.它的确使我在代码中添加了以下内容:

There's a very similar-sounding question to this one here: Get Gradients with Keras Tensorflow 2.0 although the application and scenario are sufficiently different for me to have difficulty applying the answer to this scenario. It did lead me to add the following to my code:

with tf.GradientTape() as t:
    t.watch(outputs)

那行得通,但是现在呢?我运行model.predict(...),但是如何获得渐变呢?答案是说我应该运行t.gradient(outputs, x_tensor).numpy(),但是我要为x_tensor输入什么?我没有输入变量.我尝试在运行predict之后运行t.gradient(outputs, model.inputs),但是结果是这样:

That does work, but now what? I run model.predict(...), but then how do I get my gradients? The answer says I should run t.gradient(outputs, x_tensor).numpy(), but what do I put in for x_tensor? I don't have an input variable. I tried running t.gradient(outputs, model.inputs) after running predict, but that resulted in this:

推荐答案

我最终使它与以下问题的答案一起使用:

I ended up getting this to work with a variant of the answer to this question: Get Gradients with Keras Tensorflow 2.0

x_tensor = tf.convert_to_tensor(input_data, dtype=tf.float32)
with tf.GradientTape() as t:
    t.watch(x_tensor)
    output = model(x_tensor)

result = output
gradients = t.gradient(output, x_tensor)

这使我无需冗余计算即可获得输出和梯度.

This allows me to obtain both the output and the gradient without redundant computation.

这篇关于如何获得相对于输入的keras模型的梯度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆