获取keras中间层输出、模型保存与加载

1. 获取keras中间层输出

# model summary and plot
import keras
from keras.models import Model
from keras.utils  import plot_model

DocEncoder.summary()
DocEncoder.plot_model()

model.summary()
model.plot_model()

获取keras中间层输出、模型保存与加载_第1张图片

# get mid-output from parent-graph 
from keras import backend as K

attn_layer = K.function(inputs=model.input, outputs=model.layers[-4].output)
attn_layer_output = attn_layer([input_])[0]

# get mid-output from children-graph
att_layer = K.function(inputs=DocEncoder.get_input_at(0), outputs=DocEncoder.layers[-2].output)
att_layer_output = att_layer([input_[0]])[0]

 

2. keras模型的保存与加载

import keras
from keras.models import Model
from keras.models import load_model

model = Model(input, output)

# save
model.save('my_model.h5')

# load
model = load_model('my_model.h5')

 

你可能感兴趣的:(神经网络,深度学习,matlab,机器学习理论与Python实现)