在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

你可以给你的图层命名。你的代码看起来像这样:

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

这样你就可以打印出图层的权重了

最新更新