您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在keras中添加注意力机制?

如何在keras中添加注意力机制?

如果您想在时间维度上关注,那么这段代码对我来说似乎是正确的:

activations = LSTM(units, return_sequences=True)(embedded)

# compute importance for each step
attention = Dense(1, activation='tanh')(activations)
attention = Flatten()(attention)
attention = Activation('softmax')(attention)
attention = RepeatVector(units)(attention)
attention = Permute([2, 1])(attention)

sent_representation = merge([activations, attention], mode='mul')

您已经计算出shape的注意力向量(batch_size, max_length)

attention = Activation('softmax')(attention)

我以前从未看过这段代码,所以我不能说这段代码是否正确:

K.sum(xin, axis=-2)
其他 2022/1/1 18:27:09 有531人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶