Keras深度学习模型可视化

来源:互联网 发布:sql select语句查询器 编辑:程序博客网 时间:2024/05/21 06:36

一、Example模型:

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

from keras.models import Sequentialfrom keras.layers import Densemodel = 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_modelplot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True) # plot my model

model

四、输出指定层数

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 = 0layer_output = myoutput([X_train, 0])[0]output in train mode = 1layer_output2 = myoutput([X_train, 1])[0]

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