gpt4 book ai didi

python-3.x - 凯拉斯 : Attention Mechanism For Text Summarization

转载 作者:行者123 更新时间:2023-12-04 16:04:41 25 4
gpt4 key购买 nike

我正在尝试实现 Attention使用 Keras 生成抽象文本摘要的机制从中得到了很多帮助 GitHub线程,其中有很多关于实现的信息性讨论。我正在努力理解代码的某些非常基本的部分,以及我需要修改什么才能成功获得输出。我知道attention是通过所有先前时间戳的所有隐藏状态生成的上下文向量的加权和,这就是我们在下面尝试做的。

数据:

我得到的 BBC 新闻数据集由新闻文本和各种类别的标题组成,例如政治、娱乐和体育。

参数:

n_embeddings = 64
vocab_size = len(voabulary)+1
max_len = 200
rnn_size = 64

代码:
_input = Input(shape=(max_len,), dtype='int32')

embedding = Embedding(input_dim=vocab_size, output_dim=n_embeddings, input_length=max_len)(_input)
activations = LSTM(rnn_size, return_sequences=True)(embedding)

# 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)

# apply the attention
sent_representation = merge([activations, attention], mode='mul')
sent_representation = Lambda(lambda xin: K.sum(xin, axis=1))(sent_representation)

probabilities = Dense(max_len, activation='softmax')(sent_representation)

model = Model(input=_input, output=probabilities)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=[])
print(model.summary())

我的问题:
  • 链接的线程正在尝试使用 Attention用于分类,而我想生成一个文本序列(摘要),那么我应该如何利用 sent_probabilites并解码以生成摘要?
  • 什么是RepeatVector这里用来做什么?是为了获取activation和每个词在时间戳的注意力概率 T ?
  • 我没有找到关于什么的太多解释 Permute层呢?
  • 什么是Lambda(lambda xin: K.sum(xin, axis=1))(sent_representation)为了?
  • 怎么样model.fit()看起来像?我创建了固定长度的填充序列 Xy .

  • 我真的很感激你能提供的任何帮助。非常感谢。

    最佳答案

    我发现大多数研究倾向于使用 tensorflow 来完成这项任务,因为使用 tensorflow 实现 seq2seq 模型比使用 keras 模型要容易得多

    blog详细介绍了如何以极其优化的方式构建文本摘要器,它解释了一个 dongjun-Lee ,这是最简单高效的实现之一

    Code可以在这里找到,在 google colab 上实现,

    如果您喜欢这个想法,您可以查看所有 blog series .

    希望这可以帮助

    关于python-3.x - 凯拉斯 : Attention Mechanism For Text Summarization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50893465/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com