我正在嘗試在連體神經網路上使用預訓練的 BERT 模型。但是,我在將 BERT 模型傳遞到共享 LSTM 層時遇到問題。我遇到以下錯誤:
ValueError: Exception encountered when calling layer "reshape_4" (type Reshape).
total size of new array must be unchanged, input_shape = [768], output_shape = [64, 768, 1]
Call arguments received:
? inputs=tf.Tensor(shape=(None, 768), dtype=float32)
我在其他幾篇文章中讀到,我輸入 LSTM 的維度應該是[batch_size, 768, 1]. 但是,當我嘗試重塑時,我遇到了錯誤。如何解決此錯誤?
input_1 = Input(shape=(), dtype=tf.string, name='text')
preprocessed_text_1 = bert_preprocess(input_1)
outputs_1 = bert_encoder(preprocessed_text_1)
e1 = tf.keras.layers.Reshape((64, 768, 1))(outputs_1['pooled_output'])
input_2 = Input(shape=(), dtype=tf.string, name='text')
preprocessed_text_2 = bert_preprocess(input_2)
outputs_2 = bert_encoder(preprocessed_text_2)
e2 = Reshape((64, 768, 1))(outputs_2['pooled_output'])
lstm_layer = Bidirectional(LSTM(50, dropout=0.2, recurrent_dropout=0.2)) # Won't work on GPU
x1 = lstm_layer(e1)
x2 = lstm_layer(e2)
mhd = lambda x: exponent_neg_cosine_distance(x[0], x[1])
merged = Lambda(function=mhd, output_shape=lambda x: x[0], name='cosine_distance')([x1, x2])
preds = Dense(1, activation='sigmoid')(merged)
model = Model(inputs=[input_1, input_2], outputs=preds)
uj5u.com熱心網友回復:
您必須從 Reshape 圖層中洗掉批量大小 (=64)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/459066.html
上一篇:模型是用形狀(None,50)構建的輸入KerasTensor(type_spec=TensorSpec(shape=(None,50),
