問題:一個數據又多個標簽,一個樣本數據多個類別中的某幾類;比如一個病人的數據有多個疾病,一個文本有多種題材,所以標簽就是: [1,0,0,0,1,0,1] 這種高維稀疏類型,如何計算分類准確率?
分類問題:
二分類
多分類
多標簽
Keras metrics (性能度量)
介紹的比較好的一個博客:
https://machinelearningmastery.com/custom-metrics-deep-learning-keras-python/
還有一個介紹loss的博客:
metrics:在訓練的每個batch結束的時候計算訓練集acc,如果提供驗證集(一個epoch結束計算驗證集acc),也同時計算驗證集的性能度量,分為回歸任務和分類任務,有不同的acc計算辦法;metrics 里面可以放 loss (回歸問題)或者acc(分類問題);
A metric is a function that is used to judge the performance of your model.
A metric function is similar to a loss function, except that the results from evaluating a metric are not used when training the model. You may use any of the loss functions as a metric function.
metrics其實和loss類似,只是不用來指導網絡的訓練;一般根據具體問題具體要求采用不同的 metric 函數,衡量性能;
分類問題的不同acc計算方法:
- Binary Accuracy: binary_accuracy, acc
- Categorical Accuracy: categorical_accuracy, acc
- Sparse Categorical Accuracy: sparse_categorical_accuracy
- Top k Categorical Accuracy: top_k_categorical_accuracy (requires you specify a k parameter)
- Sparse Top k Categorical Accuracy: sparse_top_k_categorical_accuracy (requires you specify a k parameter)
keras metrics 默認的accuracy:
metrics["accuracy"] : == categorical_accuracy; 最快的驗證方法,訓練一個簡單網絡,同時輸出默認accuracy,categorical_accuracy,,binaray_accuracy, 對比就可以知道;
或者看keras源碼,找到metrics默認設置:
多標簽分類問題:
[1,0,0,1,0] , [1,0,0,0,0] 分別是 y_pred, y_true:
如果使用 binary_accuracy : acc = 0.8;
if the prediction would be [0, 0, 0, 0, 0, 1]
. And if the actual labels were [0, 0, 0, 0, 0, 0]
, the accuracy would be 5/6
.;
訓練過程常見坑:
1.自定義loss:
自定義loss寫成函數的時候,keras compile() 里面,要調用自定義的loss函數而不是只給函數名: