我正在制作一个预测3个班级的CNN。如果我将批次大小设置得足够小(有时甚至会更大),则会收到类似自定义ROC指标的错误(请参见下面的功能和错误)。我看过另一个关于我的错误的stackoverflow问题,但没有解决在keras度量中使用它的问题。我的问题是有没有办法使用try,除了通过绕过错误并进入下一个时期而不终止训练?我知道解决此问题的实际方法是拥有更平衡的数据,但是我仍然想知道当前设置中的问题是否有解决方案。谢谢。顺便说一句,即使我强制数据生成器在数据集中至少包含2个类,我仍然似乎会收到此错误。
AUC指标
def sk_auroc(y_true, y_pred):
import tensorflow as tf
from sklearn.metrics import roc_auc_score
return tf.py_func(roc_auc_score, (y_true, y_pred), tf.double)
错误
Epoch 12/300
88/103 [========================>.....] - ETA: 3s - loss: 0.5235 - acc: 0.7960 - sk_auroc: 0.9289 - auc: 0.9058
ValueError: Only one class present in y_true. ROC AUC score is not defined in that case.
Traceback (most recent call last):
我尝试过
def sk_auroc(y_true, y_pred):
import tensorflow as tf
from sklearn.metrics import roc_auc_score
try:
roc_auc_score(y_true, y_pred)
except:
pass
但是当我尝试编译模型时出现以下错误。据我所知,我需要return
AttributeError Traceback (most recent call last)
<ipython-input-152-12b92143dd11> in <module>()
4
5 if t_space['which_model']=='mike':
----> 6 model = build_compile_opo_ai_model_mike(t_space)
7
8 elif t_space['which_model']=='liyuan':
<ipython-input-150-3e0af212444a> in build_compile_opo_ai_model_mike(space)
79 model.compile(optimizer=Adam(lr=space['learning_rate']*space['lr_rate_mult']),
80 loss=space['loss'],
---> 81 metrics=[space['metrics'], sk_auroc])
82 print('Plotting model...')
83 plot_model(model, to_file='mike_model.png')
~/anaconda3/lib/python3.6/site-packages/keras/engine/training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs)
449 output_metrics = nested_metrics[i]
450 output_weighted_metrics = nested_weighted_metrics[i]
--> 451 handle_metrics(output_metrics)
452 handle_metrics(output_weighted_metrics, weights=weights)
453
~/anaconda3/lib/python3.6/site-packages/keras/engine/training.py in handle_metrics(metrics, weights)
418 metric_result = weighted_metric_fn(y_true, y_pred,
419 weights=weights,
--> 420 mask=masks[i])
421
422 # Append to self.metrics_names, self.metric_tensors,
~/anaconda3/lib/python3.6/site-packages/keras/engine/training_utils.py in weighted(y_true, y_pred, weights, mask)
421 score_array *= weights
422 score_array /= K.mean(K.cast(K.not_equal(weights, 0), K.floatx()))
--> 423 return K.mean(score_array)
424 return weighted
425
~/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in mean(x, axis, keepdims)
1394 A tensor with the mean of elements of `x`.
1395 """
-> 1396 if x.dtype.base_dtype == tf.bool:
1397 x = tf.cast(x, floatx())
1398 return tf.reduce_mean(x, axis, keepdims)
AttributeError: 'NoneType' object has no attribute 'dtype'
如果我像这样加回去,就不会收到上面的错误,但是我仍然会收到原来的错误。我认为除了代码的不同部分外,我需要使用try和,但是我不确定现在在哪里(每历元的度量计算完成了)
def sk_auroc(y_true, y_pred):
import tensorflow as tf
from sklearn.metrics import roc_auc_score
try:
return tf.py_func(roc_auc_score, (y_true, y_pred), tf.double)
except:
pass