首页 > 解决方案 > 在 tensorflow 中查看神经网络中的各个组件

问题描述

实际上有没有办法查看神经网络中的各个组件?假设下面的代码在 tensorflow 中。如何查看每一层的内容、神经元和权重?

# Create a `Sequential` model and add a Dense layer as the first layer.
model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(16,)))
model.add(tf.keras.layers.Dense(32, activation='relu'))
# Now the model will take as input arrays of shape (None, 16)
# and output arrays of shape (None, 32).
# Note that after the first layer, you don't need to specify
# the size of the input anymore:
model.add(tf.keras.layers.Dense(32))
model.output_shape

标签: tensorflow

解决方案


你可以给你的图层命名。您的代码如下所示:

model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(16,)))
model.add(tf.keras.layers.Dense(32, activation='relu'), name="layer_1")
# Now the model will take as input arrays of shape (None, 16)
# and output arrays of shape (None, 32).
# Note that after the first layer, you don't need to specify
# the size of the input anymore:
model.add(tf.keras.layers.Dense(32), name="layer_1")
model.output_shape

如果你这样做,你可以通过做访问层权重

model.get_layer("layer_1").weights

因此,您可以通过这种方式打印图层的权重。


推荐阅读