簡體   English   中英

如何使用特征來評估未用於訓練模型的自定義 TensorFlow 指標

[英]How to use feature to evaluate custom TensorFlow metric that is not used to train model

我正在訓練一個進行序列預測的模型。 例如,給定某人寫的前 10 個單詞,我正在訓練一個 LSTM 來預測他們將寫的下一個單詞。 我有一個有時會起作用的模型,所以我想創建一個指標來跟蹤模型通過詞性標簽預測下一個單詞的能力(即預測動詞時的一個損失指標,一個單獨的指標預測名詞時的損失,然后是預測所有其他詞性時損失的最后一個指標)。 因此,我擁有的數據如下所示:

|----------------------------------------------------------|----------|----------------| 
| X                                                        | Y        | part-of-speech |
|==========================================================|==========|================|  
| When the sunlight strikes raindrops in the air, they act | as       | preposition    |
|----------------------------------------------------------|----------|----------------| 
| the sunlight strikes raindrops in the air, they act as   | a        | determiner     |
|----------------------------------------------------------|----------|----------------| 
| sunlight strikes raindrops in the air, they act as a     | prism    | noun           |
|----------------------------------------------------------|----------|----------------| 
| strikes raindrops in the air, they act as prism          | and      | conjunction    |
|----------------------------------------------------------|----------|----------------| 
| raindrops in the air, they act as a prism and            | form     | verb           |
|----------------------------------------------------------|----------|----------------| 
| in the air, they act as a prism and form                 | a        | determiner     |
|----------------------------------------------------------|----------|----------------| 
| the air, they act as a prism and form a                  | rainbow. | noun           |
|----------------------------------------------------------|----------|----------------| 
| ...                                                      | ...      | ...            | 
|----------------------------------------------------------|----------|----------------| 

我在訓練/驗證/測試集中有每個詞的詞性,但我不想給模型任何詞性信息,我希望它從原始詞中推斷出所有內容(他們確實通過模型結構中的編碼)。 但是,我不知道如何在不將詞性作為 data_x 或 data_y 的一部分傳遞的情況下獲取度量計算函數中的詞性。 換句話說,現在,我的訓練數據是 X 列,我的標簽是 Y 列,而且我沒有將詞性列傳遞給模型。

有什么方法可以將與每個輸出樣本相關的詞性傳遞給模型,而無需在訓練中使用該數據? 例如,要么將詞性附加到 X,然后在訓練中忽略它,要么將詞性附加到 Y,然后告訴模型不要在訓練中預測它? 或者將它附加到任何一個,並以某種方式將其與數據關聯,以便度量函數可以看到它,但不能在模型訓練中使用?

我使用的是 tensorflow 1.15,所以我一直在使用 tf.keras 庫來構建我的模型和指標。

編輯 12-20:我想在訓練循環期間通過詞性來評估准確性,而不是在后處理步驟中。 我正在尋找如何將詞性傳遞給訓練循環,而不用它來訓練模型。 tf.keras.metric.update_weight的參數是(self, y_true, y_pred, sample_weight=None) [link] ,我想不出如何傳入另一個參數,例如y_part_of_speech

一個簡單的解決方案是將您的數據拆分為語音包的部分進行評估。 訓練仍然可以在沒有詞性的完整數據集上進行,但對每個詞性分別進行評估。

假設您在 Python 中執行此操作,它可能如下所示:

# assume we have X_eval, Y_eval, Y_pos 
# these are evaluation features, evaluation truth and evaluation part of speech
# model is your model and score a function that returns the metric given the input 
# feature matrix and ground truth vector

pos_metrics = {}
for pos in Y_pos.unique():
    pos_mask = (Y_pos == pos)
    pos_metrics[pos] = model.score(X_eval[pos_mask], Y_pos[pos_mask])

這將為您提供一個字典,其中包含單獨評估的每個詞性的度量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM