将LSTM / GRU添加到keras张量流中的BERT嵌入中

问题描述 投票:2回答:1

我正在按照此代码https://github.com/strongio/keras-bert/blob/master/keras-bert.py尝试BERT嵌入

这些是代码的重要位(265-267行):

bert_output = BertLayer(n_fine_tune_layers=3)(bert_inputs)
dense = tf.keras.layers.Dense(256, activation="relu")(bert_output)
pred = tf.keras.layers.Dense(1, activation="sigmoid")(dense)

我想在BertLayer和Dense层之间添加GRU

bert_output = BertLayer(n_fine_tune_layers=3)(bert_inputs)
gru_out = tf.keras.layers.GRU(100, activation='sigmoid')(bert_output)
dense = tf.keras.layers.Dense(256, activation="relu")(gru_out)
pred = tf.keras.layers.Dense(1, activation="sigmoid")(dense)

但是我收到此错误TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

我不确定如何解决此问题。我需要重塑bert_output还是创建Embedding可以处理的GRU图层?

lstm word-embedding tf.keras
1个回答
0
投票

我有相同的错误,解决方法是

embedding_size = 768

bert_output = BertLayer(n_fine_tune_layers=3)(bert_inputs)
# Reshape bert_output before passing it the GRU
bert_output_ = tf.keras.layers.Reshape((max_seq_length, embedding_size))(bert_output)

gru_out = tf.keras.layers.GRU(100, activation='sigmoid')(bert_output_)
dense = tf.keras.layers.Dense(256, activation="relu")(gru_out)
pred = tf.keras.layers.Dense(1, activation="sigmoid")(dense)

我希望它能起作用,如果需要,您可以参考my question

© www.soinside.com 2019 - 2024. All rights reserved.