Keras深度学习模型可视化

时间:2022-05-24 22:58:08

一、Example模型:

[1 input] -> [2 neurons] -> [1 output]

from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(2, input_dim=1, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

二、模型概括:

print(model.summary()) # Summarize Model
>>>
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 2) 4
_________________________________________________________________
dense_2 (Dense) (None, 1) 3
=================================================================
Total params: 7
Trainable params: 7
Non-trainable params: 0
_________________________________________________________________
None

三、模型可视化

from keras.utils import plot_model
plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True) # plot my model

Keras深度学习模型可视化

四、输出指定层数

get_1rd_layer_output = K.function([my_model.layers[0].input, K.learning_phase()], [my_model.layers[1].output])

myoutput = K.function([my_model.layers[0].input, K.learning_phase()], [my_model.layers[7].output])
output in test mode = 0
layer_output = myoutput([X_train, 0])[0]
output in train mode = 1
layer_output2 = myoutput([X_train, 1])[0]

参考:https://machinelearningmastery.com/visualize-deep-learning-neural-network-model-keras/