首页 > 解决方案 > 监督提取文本摘要

问题描述

我想从新闻文章中提取潜在的句子,这些句子可以作为文章摘要的一部分。

花了一些时间,我发现这可以通过两种方式实现,

  1. 提取摘要(从文本中提取句子并将它们组合起来)
  2. 抽象摘要(内部语言表示以生成更多类似人类的摘要)

参考:rare-technologies.com

我按照abigailsee 的 Get To The Point: Summarization with Pointer-Generator Networks进行总结,使用预训练模型产生了很好的结果,但它是抽象的。

问题: 到目前为止,我看到的大多数提取式摘要器(PyTeaser、PyTextRank 和 Gensim)都不是基于监督学习,而是基于朴素贝叶斯分类器、tf–idf、POS 标记、基于关键字频率的句子排名、位置等等,不需要任何培训。

到目前为止,我几乎没有尝试过提取潜在的摘要句子。

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

我认为这是因为上述模型更适合正/负句子而不是摘要/非摘要句子分类。

任何有关解决此问题的方法的指导将不胜感激。

标签: pythonkerasnlpnltktext-extraction

解决方案


我认为这是因为上述模型更适合正/负句子而不是摘要/非摘要句子分类。

这是正确的。上述模型用于二进制分类,而不是文本摘要。如果你注意到了,输出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_datamodel.fit()

请注意,decoder_input_datadecoder_target_data是相同的东西,只是在 .decoder_target_data前面有一个标记decoder_input_data

这给出了非常低的准确度~0.2

我认为这是因为上述模型更适合正/负句子而不是摘要/非摘要句子分类。

训练规模小、过拟合、欠拟合等各种原因导致的准确率低。


推荐阅读