【问题标题】:Error in Keras when I want to calculate the Sensitivity and Specificity当我想计算灵敏度和特异性时,Keras 中的错误
【发布时间】:2019-04-11 19:48:36
【问题描述】:

我正在编写一个代码,用于基于 CNN 对两种类型的图像进行分类。 我想测量我工作的准确性、敏感性和特异性,但不幸的是,我遇到了以下错误。 你能否让我知道我的问题是什么。

m = tf.keras.metrics.SensitivityAtSpecificity(0.5)
model.compile(optimizer='adam', loss=keras.losses.binary_crossentropy, metrics=['accuracy',m])

错误:

Traceback (most recent call last):
  File "C:/Users/Hamed/PycharmProjects/Deep Learning/CNN.py", line 77, in <module>
    validation_steps = 1600//batch_size)
  File "C:\Users\Hamed\Anaconda3\envs\tensorflowGPU\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\Hamed\Anaconda3\envs\tensorflowGPU\lib\site-packages\keras\engine\training.py", line 1418, in fit_generator
    initial_epoch=initial_epoch)
  File "C:\Users\Hamed\Anaconda3\envs\tensorflowGPU\lib\site-packages\keras\engine\training_generator.py", line 217, in fit_generator
    class_weight=class_weight)
  File "C:\Users\Hamed\Anaconda3\envs\tensorflowGPU\lib\site-packages\keras\engine\training.py", line 1217, in train_on_batch
    outputs = self.train_function(ins)
  File "C:\Users\Hamed\Anaconda3\envs\tensorflowGPU\lib\site-packages\keras\backend\tensorflow_backend.py", line 2715, in __call__
    return self._call(inputs)
  File "C:\Users\Hamed\Anaconda3\envs\tensorflowGPU\lib\site-packages\keras\backend\tensorflow_backend.py", line 2675, in _call
    fetched = self._callable_fn(*array_vals)
  File "C:\Users\Hamed\Anaconda3\envs\tensorflowGPU\lib\site-packages\tensorflow\python\client\session.py", line 1439, in __call__
    run_metadata_ptr)
  File "C:\Users\Hamed\Anaconda3\envs\tensorflowGPU\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 528, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.NotFoundError: Resource localhost/false_negatives/class tensorflow::Var does not exist.
     [[{{node metrics/sensitivity_at_specificity/AssignAddVariableOp_1}}]]
     [[{{node metrics/sensitivity_at_specificity/Mean}}]]

【问题讨论】:

    标签: keras conv-neural-network metrics


    【解决方案1】:

    指标 tf.keras.metrics.SensitivityAtSpecificity 计算给定特异性 Click here 的灵敏度。

    不幸的是,Keras 中尚未包含敏感性和特异性指标,因此您必须按照 here 的规定编写自己的自定义指标。

    以下是计算this answer 的特异性的一种简单方法。

    def specificity(y_true, y_pred):
        """
        param:
        y_pred - Predicted labels
        y_true - True labels 
        Returns:
        Specificity score
        """
        neg_y_true = 1 - y_true
        neg_y_pred = 1 - y_pred
        fp = K.sum(neg_y_true * y_pred)
        tn = K.sum(neg_y_true * neg_y_pred)
        specificity = tn / (tn + fp + K.epsilon())
        return specificity
    

    您可以在 this link 上获取 Keras 实现的特异性和敏感性。

    【讨论】:

      【解决方案2】:

      如果有帮助,你可以试试这个……

      import keras
      
      model.compile(optimizer="adam",
                    loss="categorical_crossentropy",
                    metrics=[keras.metrics.Precision(), keras.metrics.Recall(), keras.metrics.SpecificityAtSensitivity(0.5), keras.metrics.SensitivityAtSpecificity(0.5), 'accuracy'])
      

      【讨论】:

        猜你喜欢
        • 2019-01-01
        • 2021-03-04
        • 2016-06-14
        • 1970-01-01
        • 1970-01-01
        • 2018-11-13
        • 1970-01-01
        • 2013-02-22
        • 2021-11-12
        相关资源
        最近更新 更多