如何使用Tensorflow 2从Keras模型中获得评估的梯度? [英] How to get evaluated gradients from keras model using tensorflow 2?

查看:271
本文介绍了如何使用Tensorflow 2从Keras模型中获得评估的梯度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从keras模型中获取渐变.后端函数keras.backend.gradients创建一个符号函数,该符号函数需要在某些特定输入上进行评估.以下代码确实可以解决此问题,但它利用了旧的tensorflow会话,尤其是feed_dict.

I'm trying to obtain the gradients from a keras model. The backend function keras.backend.gradients creates a symbolic function which needs to be evaluated on some specific input. The following code does work for this problem but it makes use of the old tensorflow sessions and in particular of feed_dict.

import numpy as np
import keras
from keras import backend as K
import tensorflow as tf

model = keras.Sequential()
model.add(keras.layers.Dense(16, activation='relu', input_shape = (49, )))
model.add(keras.layers.Dense(11, activation='softmax'))
model.compile(optimizer='rmsprop', loss='mse')

trainingExample = np.random.random((1, 49))


gradients = K.gradients(model.output, model.trainable_weights)

sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())

evaluated_gradients = sess.run(gradients,\
                               feed_dict={model.input:trainingExample})
sess.close()

我该如何以tensorflow 2样式重写此代码,即没有会话? 此处有另一种方法.但是我不明白为什么必须给出一些明确的输出来评估梯度,以及在没有这些输出的情况下如何使解决方案起作用.

How can I rewrite this in tensorflow 2 style, i.e. without the sessions? There is an alternative method described here. However I don't understand why it should be necessary to give some explicit output to evaluate the gradients and how to make the solution work without these outputs.

推荐答案

在tensorflow-2中,您可以使用渐变tf.GradientTape()非常容易地获得渐变.

In tensorflow-2 you can get gradients very easily using gradient tf.GradientTape().

我在这里引用了官方教程代码-

I am citing the official tutorial code here -

@tf.function
def train_step(images, labels):
  with tf.GradientTape() as tape:
    predictions = model(images)
    loss = loss_object(labels, predictions)
  gradients = tape.gradient(loss, model.trainable_variables)
  optimizer.apply_gradients(zip(gradients, model.trainable_variables))

  train_loss(loss)
  train_accuracy(labels, predictions) 

您可以在tensorflow官方网站上找到完整的教程- https://www.tensorflow. org/tutorials/quickstart/advanced

you can find the complete tutorial in tensorflow official website - https://www.tensorflow.org/tutorials/quickstart/advanced

这篇关于如何使用Tensorflow 2从Keras模型中获得评估的梯度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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