NameError: name 'model' is not defined Keras with f1_score

I'm having a problem with my Keras model, in the .compile() I use accuracy, loss, precision, recall and AUC, but also I need f1_score, due to Keras doesn´t include f1_score, I tried to calculate by myself but I get this error NameError: name 'model' is not defined, here's my code:

def residual_network_1d(input_shape):
    n_feature_maps = 64
    input_layer = keras.layers.Input(input_shape)

    # BLOCK 1
    conv_x = keras.layers.Conv1D(filters=n_feature_maps, kernel_size=8, padding='same')(input_layer)
    ...
    # FINAL
    gap_layer = keras.layers.GlobalAveragePooling1D()(output_block_3)
    output_layer = keras.layers.Dense(27, activation='softmax')(gap_layer)
    model = keras.models.Model(inputs=input_layer, outputs=output_layer)

    return model

residual_network_1d_model=residual_network_1d(input_shape = (5000,1))

def f1_score(y_test,y_pred):
    import numpy as np
    from sklearn.metrics import f1_score
    y_test = np.argmax(folds[0][1],axis=0)
    y_pred1 = model.predict(x=pc.generate_validation_data(ecg_filenames,y,folds[0][1])[0])
    y_pred = np.argmax(y_pred1, axis=1)
    my_f1_score=f1_score(y_test, y_pred , average=macro)
return my_f1_score
   residual_network_1d_model.compile(loss=tf.keras.losses.BinaryCrossentropy(), optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), metrics=[tf.keras.metrics.BinaryAccuracy(
        name='accuracy', dtype=None, threshold=0.5),tf.keras.metrics.Recall(name='Recall'),tf.keras.metrics.Precision(name='Precision'),f1_score,
                    tf.keras.metrics.AUC(
        num_thresholds=200,
        curve=ROC,
        summation_method=interpolation,
        name=AUC,
        dtype=None,
        thresholds=None,
        multi_label=True,
        label_weights=None,
    )])

Why say model is not defined if I load my model previously?

Topic f1score keras deep-learning scikit-learn python

Category Data Science


In your f1_score function you are calling model.predict, but the function only takes the variables y_test and y_pred as input. Therefore the model variable you are referring to is not defined within the scope of this function.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.