How can I print the intermediate variables in the loss function in TensorFlow and Keras?

前端 未结 3 1292
囚心锁ツ
囚心锁ツ 2021-02-19 10:13

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:

         


        
3条回答
  •  太阳男子
    2021-02-19 10:37

    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_true
    diff = theano.printing.Print('shape of diff', attrs=['shape'])(diff)
    return K.square(diff)
    

    Example for TensorFlow:

    diff = y_pred - y_true
    diff = 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.

提交回复
热议问题