我瀏覽了 Kaggle 中的一些筆記本,只是為了更深入地了解 NLP 的作業原理。我偶然發現了一個筆記本,用于預測給定前提和假設之間的關系的自然語言推理任務。它使用預訓練的 BERT 模型來完成這項任務
我有一個關于build_model()函式的問題:
max_len = 50
def build_model():
bert_encoder = TFBertModel.from_pretrained("bert-base-multilingual-cased")
input_word_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
input_mask = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
input_type_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_type_ids")
embedding = bert_encoder([input_word_ids, input_mask, input_type_ids])[0] # confused about this line
output = tf.keras.layers.Dense(3, activation='softmax')(embedding[:,0,:])
model = tf.keras.Model(inputs=[input_word_ids, input_mask, input_type_ids], outputs=output)
model.compile(tf.keras.optimizers.Adam(lr=1e-5), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
return model
我對這條線感到困惑: embedding = bert_encoder([input_word_ids, input_mask, input_type_ids])[0]
這個“嵌入”代表什么,為什么函式呼叫前面有 [0]?為什么要使用 bert_encoder 來實體化這個“嵌入”?
提前致謝!
uj5u.com熱心網友回復:
邏輯


你必須把[0]為了有torch.Tensor計算。您也可以嘗試output.logits代替output[0]
附言。我用過AutoModelForMaskedLM,沒有TFBertModel。可能略有不同,但請嘗試列印出您的embedding第一個 = ]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/411601.html
標籤:
上一篇:擬合LSTM模型
下一篇:在TPU上訓練RNN時獲得
