【问题标题】:Keras Multilabel Multiclass Individual Tag AccuracyKeras 多标签多类单个标签准确度
【发布时间】:2018-05-25 08:34:26
【问题描述】:

我正在尝试使用 Keras 中的 CNN 执行多类多标签分类。我试图根据类似问题的this function 创建一个单独的标签准确度函数

我尝试过的相关代码是:

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'.

我尝试更改类型无济于事。

【问题讨论】:

    标签: python machine-learning keras multilabel-classification multiclass-classification


    【解决方案1】:

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

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

    【讨论】:

    • 这给了我一个无效的参数错误。形状 64 与 12 不匹配
    • 我猜这是解决这个问题后发生的后验错误。恐怕我无法为您提供的代码片段提供进一步的帮助
    猜你喜欢
    • 2021-12-19
    • 2017-12-03
    • 1970-01-01
    • 2018-11-14
    • 2019-11-18
    • 2019-05-21
    • 2018-08-04
    • 2019-02-26
    • 1970-01-01
    相关资源
    最近更新 更多