在训练期间在Keras中打印层输出

时间:2018-03-25 18:00:46

标签: python tensorflow keras

我是Keras的新手。如何在训练阶段打印中间或最终图层的输出?

我正在尝试调试我的神经网络,并希望了解这些层在训练期间的行为方式。为此,我试图在训练期间为每一步确定一层的输入和输出。

FAQ(https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer)有一种方法来提取中间层的输出以构建另一个模型,但这不是我想要的。我不需要使用中间层输出作为其他层的输入,我只需要打印它们的值,也许图形/图表/可视化它。

我正在使用Keras 2.1.4

2 个答案:

答案 0 :(得分:3)

我想我自己找到了一个答案,虽然没有Keras严格完成。

基本上,要在训练期间访问图层输出,需要通过添加打印节点来修改计算图。

可以在此stackoverflow问题中找到更详细的描述:
Debugging keras intermediate layer or objective variables with tensorflow

我会在这里引用一个例子,说你想让你的损失按步骤打印,你需要将自定义损失函数设置为:

对于Theano后端:

diff = y_pred - y_true
diff = theano.printing.Print('shape of diff', attrs=['shape'])(diff)
return K.square(diff)

for Tensorflow后端:

diff = y_pred - y_true
diff = tf.Print(diff, [tf.shape(diff)])
return K.square(diff)

可以类似地访问其他图层的输出。

还有一个很好的副教程,关于使用谷歌的tf.Print() Using tf.Print() in TensorFlow

答案 1 :(得分:2)

If you want to know more info on each neuron, you need to use the following to get their bias and weights.

weights = model.layers[0].get_weights()[0]
biases = model.layers[0].get_weights()[1]

0 index defines weights and 1 defines the bias.

You can also get per layer too,

for layer in model.layers:
    weights = layer.get_weights() # list of numpy arrays

After each training, if you can access each layer with its dimension and obtain the weights and bias to a numpy array, you should be able to visualize how the neuron after each training.

Hope it helps.