TypeError: ‘Tensor’ 类型对象无法使用 len() 函数,当在 Tensorflow 中使用自定义度量时

我正在使用 Keras 和 Tensorflow 后端开发一个多类分类模型(4个类别)。y_test 的值格式为2D:

0 1 0 00 0 1 00 0 1 0

这是我用来计算平衡准确率的函数:

def my_metric(targ, predict):    val_predict = predict    val_targ = tf.math.argmax(targ, axis=1)    return metrics.balanced_accuracy_score(val_targ, val_predict)

这是模型:

hidden_neurons = 50timestamps = 20nb_features = 18model = Sequential()model.add(LSTM(                units=hidden_neurons,                return_sequences=True,                 input_shape=(timestamps,nb_features),                dropout=0.15                #recurrent_dropout=0.2              )         )model.add(TimeDistributed(Dense(units=round(timestamps/2),activation='sigmoid')))model.add(Dense(units=hidden_neurons,               activation='sigmoid'))model.add(Flatten())model.add(Dense(units=nb_classes,               activation='softmax'))model.compile(loss="categorical_crossentropy",              metrics = [my_metric],              optimizer='adadelta')

当我运行这段代码时,我得到了这个错误:

————————————————————————— TypeError Traceback (most recent call last) in () 30 model.compile(loss=”categorical_crossentropy”, 31 metrics = [my_metric], #’accuracy’, —> 32 optimizer=’adadelta’)

~/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) 402 “”” 403 # score_array has ndim >= 2 –> 404 score_array = fn(y_true, y_pred) 405 if mask is not None: 406 # Cast the mask to floatX to avoid float64 upcasting in Theano

in my_metric(targ, predict) 22 val_predict = predict 23 val_targ = tf.math.argmax(targ, axis=1) —> 24 return metrics.balanced_accuracy_score(val_targ, val_predict) 25 #return 5 26

~/anaconda3/lib/python3.6/site-packages/sklearn/metrics/classification.py in balanced_accuracy_score(y_true, y_pred, sample_weight, adjusted)
1431 1432 “”” -> 1433 C = confusion_matrix(y_true, y_pred, sample_weight=sample_weight) 1434 with np.errstate(divide=’ignore’, invalid=’ignore’): 1435
per_class = np.diag(C) / C.sum(axis=1)

~/anaconda3/lib/python3.6/site-packages/sklearn/metrics/classification.py in confusion_matrix(y_true, y_pred, labels, sample_weight) 251 252 “”” –> 253 y_type, y_true, y_pred = _check_targets(y_true, y_pred) 254 if y_type not in (“binary”, “multiclass”): 255 raise ValueError(“%s is not supported” % y_type)

~/anaconda3/lib/python3.6/site-packages/sklearn/metrics/classification.py in _check_targets(y_true, y_pred) 69 y_pred : array or indicator matrix 70 “”” —> 71 check_consistent_length(y_true, y_pred) 72 type_true = type_of_target(y_true) 73 type_pred = type_of_target(y_pred)

~/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in check_consistent_length(*arrays) 229 “”” 230 –> 231 lengths = [_num_samples(X) for X in arrays if X is not None] 232 uniques = np.unique(lengths) 233 if len(uniques) > 1:

~/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in (.0) 229 “”” 230 –> 231 lengths = [_num_samples(X) for X in arrays if X is not None] 232 uniques = np.unique(lengths) 233 if len(uniques) > 1:

~/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in _num_samples(x) 146 return x.shape[0] 147 else: –> 148 return len(x) 149 else: 150 return len(x)

TypeError: object of type ‘Tensor’ has no len()


回答:

你不能在 Keras 张量上调用 sklearn 函数。你需要使用 Keras 的后端函数,或者如果你使用的是 TF 后端,则使用 TensorFlow 函数来自己实现这个功能。

balanced_accuracy_score 定义为每列召回率的平均值。查看这个链接以获取精确度和召回率的实现方法。至于balanced_accuracy_score,你可以按以下方式实现:

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注