python - 如何在分层模型中获得注意力权重
问题描述
模型 :
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)
使用的注意力层: https : //gist.github.com/cbaziotis/6428df359af27d58078ca5ed9792bd6d with return_attention=True
训练模型后,如何可视化新输入的注意力权重。
我正在尝试什么:
get_3rd_layer_output = K.function([model.layers[0].input,K.learning_phase()],
[model.layers[1].layer.layers[3].output])
并传递一个新的输入,但它给了我错误。
可能的原因:model.layers() 只给了我最后一层。我想从 Timedistributed 部分获得权重。
解决方案
您可以使用以下内容显示模型中的所有层:
print(model.layers)
一旦你知道你的时间分布层的索引号是多少,比如 3,然后使用以下命令来获取配置和层权重。
g = model_name.layers[3].get_config()
h = model_name.layers[3].get_weights()
print(g)
print(h)
推荐阅读
- android - FirebaseRecyclerAdapter 不会加载
- php - 如何在php中读取无与伦比的标头
- c++ - Qt widget析构函数通过connected信号间接调用widget方法,并崩溃
- c++ - 即使正确使用 make_shared,shared_from_this() 也会导致 std::bad_weak_ptr
- python - 如何在运行其他信息的同时对时钟进行编程
- java - 如何在 Java 中限制输出长度
- php - 如何将一个巨大的科学计数法数字转换为浮点数或双精度数?
- reactjs - 如何使用注入的属性实例化反应组件
- html - Angular Material Schematics 生成的代码
- curl - 不支持的媒体类型,即使没有在标头中传递任何 Content-Type