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

Time:05-18

I am running this particular notebook from this link Chexpert Keras CNN, which is training cnn model on the chexpert dataset and with keras library.

However I am getting this particular error when I am running this syntax.

    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])

and I get this particular error

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

Can someone tell me how I can resolve this?

The trace of the error can be found here

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

CodePudding user response:

I presume the custom function to calcualte auc may be messing things up. but, you can use the existing class in keras for AUC.

Inside the build_model function, replace the line you compile your model with following:

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