如何在 Tensorflow 2.0 中计算输出 wrt 输入的梯度
How to compute gradient of output wrt input in Tensorflow 2.0
我有一个训练有素的 Tensorflow 2.0 模型(来自 tf.keras.Sequential()),它采用具有 26 列 (X) 的输入层并生成具有 1 列 (Y) 的输出层。
在 TF 1.x 中,我能够使用以下公式计算 输出相对于输入 的梯度:
model = load_model('mymodel.h5')
sess = K.get_session()
grad_func = tf.gradients(model.output, model.input)
gradients = sess.run(grad_func, feed_dict={model.input: X})[0]
在 TF2 中,当我尝试 运行 tf.gradients() 时,出现错误:
RuntimeError: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead.
在 tf.GradientTape 的问题 In TensorFlow 2.0 with eager-execution, how to compute the gradients of a network output wrt a specific layer?, we see an answer on how to calculate gradients with respect to intermediate layers, but I don't see how to apply this to gradients with respect to the inputs. On the Tensorflow help 中,有计算简单函数梯度的例子,但不是神经网络。
如何使用tf.GradientTape计算输出相对于输入的梯度?
希望这就是您要找的。这将给出输出的梯度 w.r.t。输入。
# Whatever the input you like goes in as the initial_value
x = tf.Variable(np.random.normal(size=(25, 120)), dtype=tf.float32)
y_true = np.random.choice([0,1], size=(25,10))
print(model.output)
print(model.predict(x))
with tf.GradientTape() as tape:
pred = model.predict(x)
grads = tape.gradients(pred, x)
这应该适用于 TF2:
inp = tf.Variable(np.random.normal(size=(25, 120)), dtype=tf.float32)
with tf.GradientTape() as tape:
preds = model(inp)
grads = tape.gradient(preds, inp)
基本上你做的方式和 TF1 一样,但是使用 GradientTape
.
在上面的例子中,我们应该使用tape.watch()
for (x, y) in test_dataset:
with tf.GradientTape() as tape:
tape.watch(x)
pred = model(x)
grads = tape.gradient(pred, x)
但梯度将只有输入的梯度
下面的方法比较好,可以用model预测预测结果,计算loss,再用loss计算所有可训练变量的grad
with tf.GradientTape() as tape:
predictions = model(x, training=True)
loss = loss_function(y, predictions)
grads = tape.gradient(loss, model.trainable_variables)
我有一个训练有素的 Tensorflow 2.0 模型(来自 tf.keras.Sequential()),它采用具有 26 列 (X) 的输入层并生成具有 1 列 (Y) 的输出层。
在 TF 1.x 中,我能够使用以下公式计算 输出相对于输入 的梯度:
model = load_model('mymodel.h5')
sess = K.get_session()
grad_func = tf.gradients(model.output, model.input)
gradients = sess.run(grad_func, feed_dict={model.input: X})[0]
在 TF2 中,当我尝试 运行 tf.gradients() 时,出现错误:
RuntimeError: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead.
在 tf.GradientTape 的问题 In TensorFlow 2.0 with eager-execution, how to compute the gradients of a network output wrt a specific layer?, we see an answer on how to calculate gradients with respect to intermediate layers, but I don't see how to apply this to gradients with respect to the inputs. On the Tensorflow help 中,有计算简单函数梯度的例子,但不是神经网络。
如何使用tf.GradientTape计算输出相对于输入的梯度?
希望这就是您要找的。这将给出输出的梯度 w.r.t。输入。
# Whatever the input you like goes in as the initial_value
x = tf.Variable(np.random.normal(size=(25, 120)), dtype=tf.float32)
y_true = np.random.choice([0,1], size=(25,10))
print(model.output)
print(model.predict(x))
with tf.GradientTape() as tape:
pred = model.predict(x)
grads = tape.gradients(pred, x)
这应该适用于 TF2:
inp = tf.Variable(np.random.normal(size=(25, 120)), dtype=tf.float32)
with tf.GradientTape() as tape:
preds = model(inp)
grads = tape.gradient(preds, inp)
基本上你做的方式和 TF1 一样,但是使用 GradientTape
.
在上面的例子中,我们应该使用tape.watch()
for (x, y) in test_dataset:
with tf.GradientTape() as tape:
tape.watch(x)
pred = model(x)
grads = tape.gradient(pred, x)
但梯度将只有输入的梯度
下面的方法比较好,可以用model预测预测结果,计算loss,再用loss计算所有可训练变量的grad
with tf.GradientTape() as tape:
predictions = model(x, training=True)
loss = loss_function(y, predictions)
grads = tape.gradient(loss, model.trainable_variables)