How can I print the values of Keras tensors?

后端 未结 7 1425
梦毁少年i
梦毁少年i 2020-12-14 00:43

I am implementing own Keras loss function. How can I access tensor values?

What I\'ve tried

def loss_fn(y_true, y_pred):
    print y_true
         


        
7条回答
  •  攒了一身酷
    2020-12-14 01:18

    If you are using TensorFlow's keras, you can enable Eager Execution:

    import tensorflow as tf 
    tf.enable_eager_execution()
    

    Afterwards you can print the tensors in your loss function.

    In case you get the error message "ValueError: Only TF native optimizers are supported in Eager mode." and you have used 'adam' as an optimizer for example, you can change the model's compile arguments to

    model.compile(optimizer = tf.train.AdamOptimizer(), loss = loss_fn, ...)
    

提交回复
热议问题