亞寵展、全球寵物產業風向標——亞洲寵物展覽會深度解析
1389
2022-05-30
學習總結
第一步是導入 Spark 分割好的訓練集和測試集。
第二步是在 TensorFlow 中設置評估指標,再在測試集上調用 model.evaluate 函數計算這些評估指標。這里使用了最常用的 Loss、Accuracy、ROC AUC、PR AUC 四個指標。
第三步是根據四個深度推薦模型的評估結果,進行模型效果的對比。
文章目錄
學習總結
一、訓練集和測試集的生成
二、TensorFlow 評估指標的設置
三、模型的效果對比
3.1 選擇模型
3.2 DeepFM在這里為啥效果差
四、作業
五、課后答疑
Reference
一、訓練集和測試集的生成
第一步是生成訓練集和測試集,這里使用最簡單的Holdout檢驗來劃分訓練集和測試集,調用spark的randomSplit函數。在tensorflow內部直接調用get_dataset方法分別載入訓練集和測試集。
代碼參考:FeatureEngForRecModel 對象中的 splitAndSaveTrainingTestSamples 函數。
按照 8:2 的比例把全量樣本集劃分為訓練集和測試集,再把它們分別存儲在SparrowRecSys/src/main/resources/webroot/sampledata/trainingSamples.csv和SparrowRecSys/src/main/resources/webroot/sampledata/testSamples.csv路徑中。
二、TensorFlow 評估指標的設置
接著是在tensorflow中設置評估指標,通過這些指標觀察模型每輪epoch的效果變化,在模型編譯階段設置metrics指定評估指標:
(1)在 model complie 階段設置準確度(Accuracy)、ROC 曲線 AUC(tf.keras.metrics.AUC(curve='ROC'))、PR 曲線 AUC(tf.keras.metrics.AUC(curve='PR')),這三個在評估推薦模型時最常用的指標。
(2)在訓練和評估過程中,模型還會默認產生損失函數 loss 這一指標。在模型編譯時我們采用了 binary_crossentropy 作為損失函數,所以這里的 Loss 指標就是在上個task中二分類問題的模型損失 Logloss。
(3)模型在每輪 epoch 結束后都會輸出這些評估指標的當前值。在最后的測試集評估階段,我們可以調用 model.evaluate 函數來生成測試集上的評估指標。
# compile the model, set loss function, optimizer and evaluation metrics model.compile( loss='binary_crossentropy', optimizer='adam', metrics=['accuracy', tf.keras.metrics.AUC(curve='ROC'), tf.keras.metrics.AUC(curve='PR')]) # train the model model.fit(train_dataset, epochs=5) # evaluate the model test_loss, test_accuracy, test_roc_auc, test_pr_auc = model.evaluate(test_dataset)
1
2
3
4
5
6
7
8
9
有了上段代碼,就看到如下每一輪訓練的 Loss、Accuracy、ROC AUC、PR AUC 這四個指標的變化,以及最終在測試集上這四個指標的結果:
隨著訓練的進行,模型的 Loss 在降低,而 Accuracy、Roc AUC、Pr AUC 這幾個指標都在升高,這證明模型的效果隨著訓練輪數的增加在逐漸變好。
可以得到測試集上的評估指標:
測試集上的評估結果相比訓練集有所下降,比如 Accuracy 從 0.7524 下降到了 0.7427,ROC AUC 從 0.8256 下降到了 0.8138。這是非常正常的現象,因為模型在訓練集上都會存在著輕微過擬合的情況。
Epoch 1/5 8236/8236 [==============================] - 60s 7ms/step - loss: 3.0724 - accuracy: 0.5778 - auc: 0.5844 - auc_1: 0.6301 Epoch 2/5 8236/8236 [==============================] - 55s 7ms/step - loss: 0.6291 - accuracy: 0.6687 - auc: 0.7158 - auc_1: 0.7365 Epoch 3/5 8236/8236 [==============================] - 56s 7ms/step - loss: 0.5555 - accuracy: 0.7176 - auc: 0.7813 - auc_1: 0.8018 Epoch 4/5 8236/8236 [==============================] - 56s 7ms/step - loss: 0.5263 - accuracy: 0.7399 - auc: 0.8090 - auc_1: 0.8305 Epoch 5/5 8236/8236 [==============================] - 56s 7ms/step - loss: 0.5071 - accuracy: 0.7524 - auc: 0.8256 - auc_1: 0.8481 1000/1000 [==============================] - 5s 5ms/step - loss: 0.5198 - accuracy: 0.7427 - auc: 0.8138 - auc_1: 0.8430 Test Loss 0.5198314250707626, Test Accuracy 0.7426666617393494, Test ROC AUC 0.813848614692688, Test PR AUC 0.8429719805717468
1
2
3
4
5
6
7
8
9
10
11
12
13
14
(1)如果測試集的評估結果相比訓練集出現大幅下降,比如下降幅度超過了 5%,就說明模型產生了非常嚴重的過擬合現象,檢查模型設計過程:
模型結構是否過于復雜;模型的層數或者每層的神經元數量過多;是否需要加入 Dropout,正則化項來減輕過擬合的風險。
(2)除了觀察模型自己的效果,在模型評估階段,更應該重視不同模型之間的效果做橫向對比,這樣才能確定我們最終上線的模型。
三、模型的效果對比
3.1 選擇模型
分析:Embedding MLP 和 Wide&Deep 模型在我們的 MovieLens 這個小規模數據集上的效果最好,它們兩個的指標也非常接近,只不過是在不同指標上有細微的差異,比如模型 Loss 指標上 Wide&Deep 模型好一點,在 Accuracy、ROC AUC、PR AUC 指標上 Embedding MLP 模型好一點。
選擇模型:
(1)做進一步的模型調參,特別是對于復雜一點的 Wide&Deep 模型,我們可以嘗試通過參數的 Fine Tuning(微調)讓模型達到更好的效果;
(2)如果經過多次嘗試兩個模型的效果仍比較接近,我們就通過
線上評選
出最后的勝出者。
3.2 DeepFM在這里為啥效果差
本該DeepFM 的表達能力是最強的,這里可能是過擬合了。看DeepFM 在訓練集上的表現(如下)發現訓練集上的效果很牛逼,但是測試集上效果很拉跨,說明過擬合了。這是因為我們的數據集是規模很小的采樣過的 MovieLens 數據集,難以讓模型收斂。模型中很多參數其實沒有達到穩定的狀態,因此在測試集上的表現往往會呈現出比較大的隨機性。
小結:根據具體業務和數據,因地制宜地調整模型和參數,這才是算法工程師最大的價值所在。
四、作業
(1)除了用到的 Loss、Accuracy、ROC AUC、PR AUC 這四個指標,你在 TensorFlow 的實踐中還會經常用到哪些評估指標呢? 你能把這些常用指標以及它們特點分享出來嗎?(你可以參考 TensorFlow 的官方Metrics 文檔 )
【答】
class BinaryAccuracy: Calculates how often predictions match binary labels.
class BinaryCrossentropy: Computes the crossentropy metric between the labels and predictions.
class CategoricalAccuracy: Calculates how often predictions match one-hot labels.
class CategoricalCrossentropy: Computes the crossentropy metric between the labels and predictions.
class CategoricalHinge: Computes the categorical hinge metric between y_true and y_pred.
class CosineSimilarity: Computes the cosine similarity between the labels and predictions.
class FalseNegatives: Calculates the number of false negatives.
class FalsePositives: Calculates the number of false positives.
class Hinge: Computes the hinge metric between y_true and y_pred.
class KLDivergence: Computes Kullback-Leibler divergence metric between y_true and y_pred.
class LogCoshError: Computes the logarithm of the hyperbolic cosine of the prediction error.
class Mean: Computes the (weighted) mean of the given values.
class MeanAbsoluteError: Computes the mean absolute error between the labels and predictions.
class MeanAbsolutePercentageError: Computes the mean absolute percentage error between y_true and y_pred.
class MeanIoU: Computes the mean Intersection-Over-Union metric.
class MeanMetricWrapper: Wraps a stateless metric function with the Mean metric.
class MeanRelativeError: Computes the mean relative error by normalizing with the given values.
class MeanSquaredError: Computes the mean squared error between y_true and y_pred.
class MeanSquaredLogarithmicError: Computes the mean squared logarithmic error between y_true and y_pred.
class MeanTensor: Computes the element-wise (weighted) mean of the given tensors.
class Metric: Encapsulates metric logic and state.
class Poisson: Computes the Poisson metric between y_true and y_pred.
class Precision: Computes the precision of the predictions with respect to the labels.
class PrecisionAtRecall: Computes best precision where recall is >= specified value.
class RecallAtPrecision: Computes best recall where precision is >= specified value.
class RootMeanSquaredError: Computes root mean squared error metric between y_true and y_pred.
(2)你認為 DeepFM 評估結果這么差的原因,除了過擬合,還有什么更深層次的原因呢?可以嘗試從模型結構的原理上給出一些解釋嗎?
【答】可能是因為
交叉層的數據太稀疏
了,不能夠讓交叉層完全收斂。
另外交叉層大量使用id類特征,測試集的id特征和訓練集的id
特征重疊比較少
的話,很可能無法作出合理的預測。這也是所謂模型泛化性和記憶性的矛盾。
五、課后答疑
Reference
(1)https://github.com/wzhe06/Reco-papers
(2)《深度學習推薦系統實戰》,王喆
TensorFlow 推薦系統 機器學習
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。