Python 层次模型中注意力权重的确定
型号: 使用的注意层: 带return\u attention=True 训练模型后,我如何可视化新输入的注意力权重 我正在尝试的是:Python 层次模型中注意力权重的确定,python,keras,attention-model,Python,Keras,Attention Model,型号: 使用的注意层: 带return\u attention=True 训练模型后,我如何可视化新输入的注意力权重 我正在尝试的是: sequence_input = Input(shape=(MAX_SENT_LENGTH,), dtype='int32') words = embedding_layer(sequence_input) h_words = Bidirectional(GRU(200, return_sequences=True,dropout=0.2,recurrent_d
sequence_input = Input(shape=(MAX_SENT_LENGTH,), dtype='int32')
words = embedding_layer(sequence_input)
h_words = Bidirectional(GRU(200, return_sequences=True,dropout=0.2,recurrent_dropout=0.2))(words)
sentence = Attention()(h_words) #with return true
#sentence = Dropout(0.2)(sentence)
sent_encoder = Model(sequence_input, sentence[0])
print(sent_encoder.summary())
document_input = Input(shape=(None, MAX_SENT_LENGTH), dtype='int32')
document_enc = TimeDistributed(sent_encoder)(document_input)
h_sentences = Bidirectional(GRU(100, return_sequences=True))(document_enc)
preds = Dense(7, activation='softmax')(h_sentences)
model = Model(document_input, preds)
并传递一个新的输入,但它给了我错误
可能的原因:
model.layers()只提供最后一层。我想从时间分布部分获取权重。您可以使用以下内容显示模型中的所有层:
get_3rd_layer_output = K.function([model.layers[0].input,K.learning_phase()],
[model.layers[1].layer.layers[3].output])
一旦你知道你的时间分布层是什么索引号,比如说3,然后使用下面的方法来获得配置和层权重
print(model.layers)
你好@Rohit Saxena,问题很好,你能找到解决办法吗?
g = model_name.layers[3].get_config()
h = model_name.layers[3].get_weights()
print(g)
print(h)