python - 监督提取文本摘要
问题描述
我想从新闻文章中提取潜在的句子,这些句子可以作为文章摘要的一部分。
花了一些时间,我发现这可以通过两种方式实现,
- 提取摘要(从文本中提取句子并将它们组合起来)
- 抽象摘要(内部语言表示以生成更多类似人类的摘要)
我按照abigailsee 的 Get To The Point: Summarization with Pointer-Generator Networks进行总结,使用预训练模型产生了很好的结果,但它是抽象的。
问题: 到目前为止,我看到的大多数提取式摘要器(PyTeaser、PyTextRank 和 Gensim)都不是基于监督学习,而是基于朴素贝叶斯分类器、tf–idf、POS 标记、基于关键字频率的句子排名、位置等等,不需要任何培训。
到目前为止,我几乎没有尝试过提取潜在的摘要句子。
- 获取文章的所有句子并将摘要句子标记为 1 和所有其他句子的 0
- 清理文本并应用停用词过滤器
- 使用词汇量为 20000 的Tokenizer 对文本语料库进行矢量化
from keras.preprocessing.text import Tokenizer
,并将所有序列填充到所有句子的平均长度。 - 构建一个 Sqequential keras 模型并对其进行训练。
model_lstm = Sequential()
model_lstm.add(Embedding(20000, 100, input_length=sentence_avg_length))
model_lstm.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
model_lstm.add(Dense(1, activation='sigmoid'))
model_lstm.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
这给出了非常低的准确度~0.2
我认为这是因为上述模型更适合正/负句子而不是摘要/非摘要句子分类。
任何有关解决此问题的方法的指导将不胜感激。
解决方案
我认为这是因为上述模型更适合正/负句子而不是摘要/非摘要句子分类。
这是正确的。上述模型用于二进制分类,而不是文本摘要。如果你注意到了,输出Dense(1, activation='sigmoid')
(
我应该怎么办?
解决这个问题的主要思想是编码器-解码器(也称为 seq2seq)模型。Keras 存储库上有一个很好的教程,它用于机器翻译,但它很容易适应文本摘要。
代码的主要部分是:
from keras.models import Model
from keras.layers import Input, LSTM, Dense
# Define an input sequence and process it.
encoder_inputs = Input(shape=(None, num_encoder_tokens))
encoder = LSTM(latent_dim, return_state=True)
encoder_outputs, state_h, state_c = encoder(encoder_inputs)
# We discard `encoder_outputs` and only keep the states.
encoder_states = [state_h, state_c]
# Set up the decoder, using `encoder_states` as initial state.
decoder_inputs = Input(shape=(None, num_decoder_tokens))
# We set up our decoder to return full output sequences,
# and to return internal states as well. We don't use the
# return states in the training model, but we will use them in inference.
decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_inputs,
initial_state=encoder_states)
decoder_dense = Dense(num_decoder_tokens, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
# Define the model that will turn
# `encoder_input_data` & `decoder_input_data` into `decoder_target_data`
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
# Run training
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
model.fit([encoder_input_data, decoder_input_data], decoder_target_data,
batch_size=batch_size,
epochs=epochs,
validation_split=0.2)
基于上述实现,需要将encoder_input_data
,decoder_input_data
和分别传递给输入文本和文本的摘要版本decoder_target_data
。model.fit()
请注意,decoder_input_data
和decoder_target_data
是相同的东西,只是在 .decoder_target_data
前面有一个标记decoder_input_data
。
这给出了非常低的准确度~0.2
我认为这是因为上述模型更适合正/负句子而不是摘要/非摘要句子分类。
训练规模小、过拟合、欠拟合等各种原因导致的准确率低。
推荐阅读
- python - Spyder 没有从 anaconda 启动
- javascript - CRUD 在 React 与 Firestore 中用于数组中的数据
- javascript - 使用 Playwright 将 GOJS 节点从调色板拖放到 HTML div 中
- r - 在 ggplot 上查找某个 y 值处的 x 值
- xml - Matlab生成的xml文件中的xC9代替É
- python - 将零移动到字符串的末尾
- python - Python Pandas 用最后一篇文章的体积计算总体积
- c# - 在 C# 中循环所有创建的按钮
- python - Python包加固:捕获新文件的创建
- python - 无法根据“performance_rnn_compact”模型配置从 *.mag 包中生成 midi 音符