定义要在函数外部保存的图层并命名它们.然后创建两个函数foo()和bar(). foo()将拥有包含嵌入层的原始管道. bar()将只包含管道AFTER嵌入层的一部分.相反,您将在bar()中定义具有嵌入尺寸的新Input()图层:
lstm1 = LSTM(256, return_sequences=True, name='lstm1')
lstm2 = LSTM(256, return_sequences=False, name='lstm2')
dense = Dense(NUM_OF_LABELS, name='Susie Dense')
def foo(...):
sentence_indices = Input(input_shape, dtype="int32")
embedding_layer = pretrained_embedding_layer(word_to_vec_map, word_to_index)
embeddings = embedding_layer(sentence_indices)
X = lstm1(embeddings)
X = Dropout(0.5)(X)
X = lstm2(X)
X = Dropout(0.5)(X)
X = dense(X)
X = Activation("softmax")(X)
return Model(inputs=sentence_indices, outputs=X)
def bar(...):
embeddings = Input(embedding_shape, dtype="float32")
X = lstm1(embeddings)
X = Dropout(0.5)(X)
X = lstm2(X)
X = Dropout(0.5)(X)
X = dense(X)
X = Activation("softmax")(X)
return Model(inputs=sentence_indices, outputs=X)
foo_model = foo(...)
bar_model = bar(...)
foo_model.fit(...)
bar_model.save_weights(...)
现在,您将训练原始的foo()模型.然后,您可以保存缩小的bar()模型的权重.加载模型时,不要忘记指定by_name = True参数:
foo_model.load_weights('bar_model.h5', by_name=True)