【问题标题】:BERT embeddings for abstractive text summarisation in Keras using encoder-decoder modelBERT 嵌入在 Keras 中使用编码器-解码器模型进行抽象文本摘要
【发布时间】:2020-09-11 11:31:59
【问题描述】:

我正在使用 Keras 中的编码器-解码器架构处理文本摘要任务。我想使用 GloVe 和 BERT 等不同的词嵌入来测试模型的性能。我已经使用 GloVe 嵌入对其进行了测试,但在使用 Keras 的 seq2seq 模型中找不到合适的 BERT 嵌入示例。这是我的代码的摘录:

<...>
# splitting the data

from sklearn.model_selection import train_test_split
Xtrain, Xtest, ytrain, ytest = train_test_split(data['clean_texts'], data['clean_summaries'], 
                                            test_size=0.2,shuffle=True,random_state=0)
# prepare a tokenizer for inputs

tokenizer = Tokenizer()
tokenizer.fit_on_texts(Xtrain) 

X_train = tokenizer.texts_to_sequences(Xtrain)
X_test = tokenizer.texts_to_sequences(Xtest)

X_train = pad_sequences(X_train, maxlen= MAX_TEXT_LENGTH, padding='post')
X_test = pad_sequences(X_test, maxlen= MAX_TEXT_LENGTH, padding='post')

# prepare a tokenizer for outputs

y_tokenizer = Tokenizer()
y_tokenizer.fit_on_texts(ytrain) 

y_train = y_tokenizer.texts_to_sequences(ytrain)
y_test = y_tokenizer.texts_to_sequences(ytest)

y_train = pad_sequences(y_train, maxlen= MAX_SUM_LENGTH, padding='post')
y_test = pad_sequences(y_test, maxlen= MAX_SUM_LENGTH, padding='post')

Textvocab_size   =  len(tokenizer.word_index) + 1
Sumvocab_size  =   len(y_tokenizer.word_index) + 1 

# Encoder 

encoder_inputs = Input(shape=(MAX_TEXT,))
encoder_embedding = Embedding(Textvocab_size, LATENT_DIMENSION,trainable=True)(encoder_inputs) 

encoderlstm1 = Bidirectional(LSTM(LATENT_DIMENSION,return_sequences=True, return_state=True))
encoder_output1, forward_h1, forward_c1, backward_h1, backward_c1 = encoderlstm1(encoder_embedding)
state_h1 = Concatenate()([forward_h1, backward_h1])
state_c1 = Concatenate()([forward_c1, backward_c1])
encoder_states1 = [state_h1, state_c1]

<...>

如何将 BERT 词嵌入添加到这样的模型中?我在标记化之前在我的数据框上尝试了this implementation,但我遇到了一个错误:

AttributeError: 'str' object has no attribute 'device_typeid'

我找不到解决办法。有没有其他方法可以简单地将这些词嵌入添加为 GloVe?

【问题讨论】:

    标签: python keras nlp seq2seq bert-language-model


    【解决方案1】:

    错误说明你的系统处理器类型是GPU还是CPU机器。

    对我来说,我使用的是 bert 嵌入是使用 bert_embedding 库

    from bert_embedding import BertEmbedding
    embedding = BertEmbedding('man')
    

    错误代码:未初始化机器类型

    将代码改为

    embedding = BertEmbedding()# for cpu
    embed = embedding('man')
    #or for gpu
    import mxnet as mx
    ctx = mx.gpu(0)
    embedding = BertEmbedding(ctx=ctx)
    embed = embedding('man')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-14
      • 2021-11-26
      • 2021-05-18
      • 2019-05-19
      • 2019-09-13
      • 2019-01-05
      • 2021-07-24
      • 1970-01-01
      相关资源
      最近更新 更多