Keras多标签多类单个标签的准确性 [英] Keras Multilabel Multiclass Individual Tag Accuracy

查看:360
本文介绍了Keras多标签多类单个标签的准确性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Keras中使用CNN执行多类多标签分类.我试图基于此函数来自类似问题

I'm trying to perform a multiclass multilabel classification with a CNN in Keras. I've attempted to create an individual label accuracy function based on this function from a similar question

我尝试过的相关代码是:

The relevant code I have attempted is:

labels = ["dog", "mammal", "cat", "fish", "rock"] #I have more
interesting_id = [0]*len(labels)
interesting_id[labels.index("rock")] = 1 #we only care about rock's accuracy
interesting_label = K.variable(np.array(interesting_label), dtype='float32')

def single_class_accuracy(interesting_class_id):
    def single(y_true, y_pred):
        class_id_true = K.argmax(y_true, axis=-1)
        class_id_preds = K.argmax(y_pred, axis=-1)
        # Replace class_id_preds with class_id_true for recall here
        accuracy_mask = K.cast(K.equal(class_id_preds, interesting_class_id), 'float32')
        class_acc_tensor = K.cast(K.equal(class_id_true, class_id_preds), 'float32') * accuracy_mask
        class_acc = K.sum(class_acc_tensor) / K.maximum(K.sum(accuracy_mask), 1)
        return class_acc
    return single

,然后将其称为指标:

model.compile(optimizer=SGD(lr=0.0001, momentum=0.9),
              loss='binary_crossentropy', metrics=[metrics.binary_accuracy, 
              single_class_accuracy(interesting_id)])

但是我遇到的错误是:

> Traceback (most recent call last):
  File "/share/pkg/tensorflow/r1.3/install/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 490, in apply_op
    preferred_dtype=default_dtype)
  File "/share/pkg/tensorflow/r1.3/install/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 676, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/share/pkg/tensorflow/r1.3/install/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 677, in _TensorConversionFunction
    "of type '%s'" % (dtype.name, v.dtype.name))
ValueError: Incompatible type conversion requested to type 'int64' for variable of type 'float32_ref'

During handling of the above exception, another exception occurred:

> Traceback (most recent call last):
  File "bottleneck_model.py", line 190, in <module>
    main()
  File "bottleneck_model.py", line 171, in main
    loss='binary_crossentropy', metrics=[metrics.binary_accuracy, binary_accuracy_with_threshold, single_class_accuracy(interesting_label)])
  File "/share/pkg/keras/2.0.6/install/lib/python3.6/site-packages/keras/engine/training.py", line 898, in compile
    metric_result = masked_metric_fn(y_true, y_pred, mask=masks[i])
  File "/share/pkg/keras/2.0.6/install/lib/python3.6/site-packages/keras/engine/training.py", line 494, in masked
    score_array = fn(y_true, y_pred)
  File "bottleneck_model.py", line 81, in single
    accuracy_mask = K.cast(K.equal(class_id_preds, interesting_class_id), 'float32')
  File "/share/pkg/keras/2.0.6/install/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 1516, in equal
    return tf.equal(x, y)
  File "/share/pkg/tensorflow/r1.3/install/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py", line 753, in equal
    result = _op_def_lib.apply_op("Equal", x=x, y=y, name=name)
  File "/share/pkg/tensorflow/r1.3/install/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 526, in apply_op
    inferred_from[input_arg.type_attr]))
TypeError: Input 'y' of 'Equal' Op has type float32 that does not match type int64 of argument 'x'.

我尝试将类型更改为无效.

I have tried changing the types to no avail.

推荐答案

K.equal的输入具有不同的数据类型.我假设您应该将class_id_preds强制转换为float32interesting_class_id强制转换为int64.如果后者是一个整数(否则将其他张量转换为其他张量),则应该解决错误:

The inputs to K.equal have different data types. I presume that you should cast class_id_preds to float32 or interesting_class_id to int64. If the latter is an integer (otherwise cast the other tensors), this should solve the error:

interesting_class_id = K.cast(interesting_class_id, 'int64')

这篇关于Keras多标签多类单个标签的准确性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆