如何可视化含输入输出层数值的DNN/CNN模型?现有可视化仅显示Dense层
Hey there! Let's work through your two technical requests one by one.
1. How to Visualize DNN/CNN Models with Input & Output Layer Values
To visualize your model with clear display of input/output layer details (like shape, feature counts, and output dimensions), you'll want to leverage the built-in tf.keras.utils.plot_model function with specific parameters that enable these details. Here's what you need to know:
- Key Parameters to Enable Layer Details:
show_shapes=True: This will display the input and output tensor shapes for every layer (including your input and output layers, showing values like(None, n_features)for the input and(None, n_outputs)for the output).show_layer_names=True: Makes layer names visible, which helps map each component in the plot to your code.show_layer_activations=True: Adds the activation function name next to each Dense/CNN layer for clarity.expand_nested=False: Keeps the plot clean if you don't have nested sub-models (adjust toTrueif you do).
For CNN models, the approach is identical—just make sure your Input layer specifies the full spatial shape (e.g., Input(shape=(28,28,1)) for MNIST images), and the show_shapes parameter will display those dimensions in the visualization.
2. Fixing Missing Input/Output Layer Values in Your Current Visualization
The reason your generated image only shows Dense layers (and no input/output layer values) is that you're using the default plot_model settings, which don't display shapes by default. Here's how to modify your code to fix this:
Modified Code Snippet
import pandas as pd import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Input, BatchNormalization, Dense, Dropout def create_model(n_features, n_outputs): model = Sequential() model.add(Input(n_features)) model.add(BatchNormalization()) model.add(Dense(51, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(68, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(85, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(85, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(68, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(51, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(n_outputs, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='Adam', metrics=['accuracy']) # Updated plot_model call with shape/details enabled tf.keras.utils.plot_model( model, to_file='model_combined.png', show_shapes=True, show_layer_names=True, show_layer_activations=True ) # model.summary() return model
Why This Works:
- Adding
show_shapes=Trueforces the plot to display the input/output dimensions for every layer. You'll now see your input layer's(None, n_features)shape and output layer's(None, n_outputs)shape clearly in the image. - The other parameters (
show_layer_names,show_layer_activations) add extra context to make the visualization more informative.
Note on the Earlier keras.utils.vis_utils.plot_model Error
The TypeError: 'InputLayer' object is not iterable you encountered is likely due to version mismatches between standalone Keras and TensorFlow's Keras implementation. Since you're building your model with tf.keras.Sequential, sticking with tf.keras.utils.plot_model is the correct and compatible choice (as you've already done).
内容的提问来源于stack exchange,提问作者bashar

