I'm writing a custom objective to train a Keras (with TensorFlow backend) model but I need to debug some intermediate computation. For simplicity, let's say I have:def custom_loss(y_pred, y_true): diff = y_pred - y_true return K.square(diff)I could not find an easy way to access, for example, the intermediate variable diff or its shape during training. In this simple example, I know that I could return diff to print its values, but my actual loss is more complex and I can't return intermediate values without getting compiling errors.Is there an easy way to debug intermediate variables in Keras? 解决方案 This is not something that is solved in Keras as far as I know, so you have to resort to backend-specific functionality. Both Theano and TensorFlow have Print nodes that are identity nodes (i.e., they return the input node) and have the side-effect of printing the input (or some tensor of the input).Example for Theano:diff = y_pred - y_truediff = theano.printing.Print('shape of diff', attrs=['shape'])(diff)return K.square(diff)Example for TensorFlow:diff = y_pred - y_truediff = tf.Print(diff, [tf.shape(diff)])return K.square(diff)Note that this only works for intermediate values. Keras expects tensors that are passed to other layers to have specific attributes such as _keras_shape. Values processed by the backend, i.e. through Print, usually do not have that attribute. To solve this, you can wrap debug statements in a Lambda layer for example. 这篇关于如何在TensorFlow和Keras的损失函数中打印中间变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-08 05:11