Eri*_*ric 5 python keras tensorflow
我是Keras的新手。在培训阶段,如何打印中间层或最终层的输出?
我正在尝试调试我的神经网络,并想知道训练过程中各层的行为。为此,我尝试在训练过程中为每一步精确输入和输出一层。
FAQ(https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer)具有提取中间层输出以构建另一层的方法模型,但这不是我想要的。我不需要将中间层的输出用作其他层的输入,我只需要打印出它们的值,并可能对其进行图形化/图表化/可视化。
我正在使用Keras 2.1.4
我想我自己已经找到了答案,虽然不是 Keras 严格完成的。
基本上,要在训练期间访问层输出,需要通过添加打印节点来修改计算图。
更详细的描述可以在这个 StackOverflow 问题中找到:
如何在 TensorFlow 和 Keras 中打印损失函数中的中间变量?
我将在此处引用一个示例,假设您希望每步打印损失,您需要将自定义损失函数设置为:
对于 Theano 后端:
diff = y_pred - y_true
diff = theano.printing.Print('shape of diff', attrs=['shape'])(diff)
return K.square(diff)
Run Code Online (Sandbox Code Playgroud)
对于 Tensorflow 后端:
diff = y_pred - y_true
diff = tf.Print(diff, [tf.shape(diff)])
return K.square(diff)
Run Code Online (Sandbox Code Playgroud)
可以类似地访问其他层的输出。
还有一个很好的关于使用tf.Print()Google Using tf.Print() in TensorFlow 的副教程
| 归档时间: |
|
| 查看次数: |
5710 次 |
| 最近记录: |