【发布时间】: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