如何在keras程序中调用TimeDistributed包装器中的层

时间:2018-04-19 06:49:23

标签: keras

我想从" sentEncoder'中调用图层。在具有给定体系结构的训练有素的Keras模型中,在第二个TimeDistributed包装器中建模,以创建另一个用于进行中间预测的Keras模型。从' docEncoder'中调用图层非常简单。 model,但是如何在训练后访问sentEncoder中的图层' docEncoder'?

l2_reg = regularizers.l2(reg_param)
sentence_input = Input(shape=(MAX_SENT_LENGTH,), dtype='int32')
embedding_layer = get_embedding_layer(embedding_dim=embedding_dim, embedding_matrix=embedding_matrix,
                                      max_num_words=max_num_words, max_sent_length=max_sent_length,
                                      reg_param=reg_param)
embedded_sequences = embedding_layer(sentence_input)
l_lstm = Bidirectional(GRU(gru_units, return_sequences=True))(embedded_sequences)
l_dense = TimeDistributed(Dense(2*gru_units, use_bias=True,
                                bias_initializer='zero',
                                kernel_initializer='glorot_uniform',
                                activation='tanh'))(l_lstm)
l_att = AttLayer()(l_dense)
sentEncoder = Model(sentence_input, l_att)
print(sentEncoder.summary())

doc_input = Input(shape=(max_sents, max_sent_length), dtype='int32')
review_encoder = TimeDistributed(sentEncoder)(doc_input)
l_lstm_sent = Bidirectional(GRU(gru_units, return_sequences=True))(review_encoder)
l_dense_sent = TimeDistributed(Dense(2*gru_units, use_bias=True,
                                     bias_initializer='zero',
                                     kernel_initializer='glorot_uniform',
                                     activation='tanh'))(l_lstm_sent)
l_att_sent = AttLayer()(l_dense_sent)
preds = Dense(n_classes, activation='sigmoid', kernel_regularizer=l2_reg)(l_att_sent)
docEncoder = Model(doc_input, preds)

2 个答案:

答案 0 :(得分:1)

TimeDistributed包装器中图层的权重可以像这样提取:

sent_att_u_weights = model.layers[4].get_weights()[0]

答案 1 :(得分:0)

您可以使用以下类似的方法将其重新设置为可训练:

for layer in docEncoder.layers[0].layer.layers:
layer.trainable=True

首先,您在主模型中访问TimeDistributed包装的sentEncoder作为一个图层,然后访问其中的各个图层。