How to visualize the model structure in Keras?
In order to visualize the structure of a Keras model, you can use the plot_model function from the keras.utils.vis_utils module. This function allows you to save the model’s structure as an image or display it in a Jupyter Notebook.
Below is an example code for visualizing the structure of a simple Keras model:
from keras.models import Sequential
from keras.layers import Dense
from keras.utils.vis_utils import plot_model
# 创建一个简单的Sequential模型
model = Sequential()
model.add(Dense(32, input_shape=(784,), activation='relu'))
model.add(Dense(10, activation='softmax'))
# 可视化模型结构并保存为图片
plot_model(model, to_file='model.png', show_shapes=True)
# 在Jupyter Notebook中显示模型结构
plot_model(model, show_shapes=True)
Running the above code will generate an image file named model.png displaying the structure of the model; at the same time, the model structure will be displayed in the Jupyter Notebook. You can adjust the model structure based on actual needs and modify the parameters in the plot_model function as necessary.