我想从新闻文章中提取可能的句子,这些句子可以作为文章摘要的一部分。
花了一些时间,我发现可以通过两种方式实现这一点,
- 提取摘要(从文本中提取句子并将其合并)
- 抽象摘要(内部语言表示以生成更接近人类的摘要)
我关注了abigailsee's Get To The Point: Summarization with Pointer-Generator Networks用于摘要,它使用预训练模型产生了良好的结果,但它是抽象的。
问题: 到目前为止,我见过的大多数提取摘要器(PyTeaser、PyTextRank 和 Gensim)都不是基于监督学习,而是基于朴素贝叶斯分类器、tf-idf、POS 标记、基于关键字频率、位置等的句子排名,不需要任何培训。
到目前为止,我尝试过一些事情来提取潜在的摘要句子。
- 获取文章的所有句子,并将所有其他句子的摘要句子标记为 1 和 0
- 清理文本并应用停用词过滤器
- 使用 Tokenizer
from keras.preprocessing.text import Tokenizer
对词汇量大小为 20000 的文本语料库进行向量化,并将所有序列填充到所有句子的平均长度。 - 构建 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
我认为这是因为上述模型更适合肯定/否定句子而不是摘要/非摘要句子分类。
任何有关解决此问题的方法的指导将不胜感激。
最佳答案
I think this is because the above model is more suitable for positive/negative sentences rather than summary/non-summary sentences classification.
没错。上述模型用于二元分类,而不是文本摘要。如果您注意到,输出 (Dense(1,activation='sigmoid')
) 只会给您 0-1 之间的分数,而在文本摘要中,我们需要一个生成标记序列的模型。
我应该做什么?
解决这个问题的主导思想是 encoder-decoder (也称为 seq2seq)模型。有一个nice tutorial在 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
领先一个标记。 .
This is giving very low accuracy ~0.2
I think this is because the above model is more suitable for positive/negative sentences rather than summary/non-summary sentences classification.
训练规模小、过拟合、欠拟合等多种原因导致准确率较低
关于python - 有监督的文本抽取摘要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54287822/