AttributeError: module 'keras.api._v2.keras.metrics' has no attribute 'auc'

AttributeError: module 'keras.api._v2.keras.metrics' has no attribute 'auc'

我是 运行 这个来自 link Chexpert Keras CNN 的特定笔记本,它正在 chexpert 数据集和 keras 库上训练 cnn 模型。

但是,当我使用 运行 这种语法时,我得到了这个特殊的错误。

    history = model.fit_generator(generator=train_generator,
                        steps_per_epoch=STEP_SIZE_TRAIN,
                        validation_data=validation_generator,
                        validation_steps=STEP_SIZE_VALID,
                        epochs=epochs, callbacks = [checkpointer])

我得到了这个特殊的错误

AttributeError:模块 'keras.api._v2.keras.metrics' 没有属性 'auc'。

谁能告诉我如何解决这个问题?

可以在这里找到错误的踪迹

---> 13     history = model.fit_generator(generator=train_generator,
     14                         steps_per_epoch=STEP_SIZE_TRAIN,
     15                         validation_data=validation_generator,

~\anaconda3\lib\site-packages\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
   2258         'Please use `Model.fit`, which supports generators.',
   2259         stacklevel=2)
-> 2260     return self.fit(
   2261         generator,
   2262         steps_per_epoch=steps_per_epoch,

~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

~\anaconda3\lib\site-packages\keras\engine\training.py in tf__train_function(iterator)
     13                 try:
     14                     do_return = True
---> 15                     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16                 except:
     17                     do_return = False

~\AppData\Local\Temp\__autograph_generated_filelydcowkp.py in tf__auc(y_true, y_pred)
      8                 do_return = False
      9                 retval_ = ag__.UndefinedReturnValue()
---> 10                 auc = ag__.converted_call(ag__.ld(tf).metrics.auc, (ag__.ld(y_true), ag__.ld(y_pred)), None, fscope)[1]
     11                 ag__.converted_call(ag__.converted_call(ag__.ld(K).get_session, (), None, fscope).run, (ag__.converted_call(ag__.ld(tf).local_variables_initializer, (), None, fscope),), None, fscope)
     12                 try:

AttributeError: in user code:

    File "C:\Users\danie\anaconda3\lib\site-packages\keras\engine\training.py", line 1051, in train_function  *
        return step_function(self, iterator)
    File "C:\Users\danie\AppData\Local\Temp/ipykernel_26088/1353090691.py", line 2, in auc  *
        auc = tf.metrics.auc(y_true, y_pred)[1]

    AttributeError: module 'keras.api._v2.keras.metrics' has no attribute 'auc'

我认为计算 auc 的自定义函数可能搞砸了。但是,您可以在 keras for AUC.

中使用现有的 class

build_model 函数中,将您编译模型的行替换为以下内容:

from tensorflow.keras.metrics import AUC
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy', AUC()])