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,

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


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.

  • $\begingroup$ So, what can I do, or how can I solve that? Or how can I calculate f1_score with my model? $\endgroup$
    – megasaw
    Oct 28 '21 at 7:44
  • $\begingroup$ I am not sure why you are using model.predict within your f1_score function since the predicted y values are already provided to the function as an argument, and would therefore assume that you can simply remove that method call. Another option would be to use the F1Score implementation of the tensorflow-addons package. $\endgroup$
    – Oxbowerce
    Oct 28 '21 at 7:59
  • $\begingroup$ The code is an adaptation from other and in the original the author uses model.predict, to generate y_pred, and where are predicted y values generated? $\endgroup$
    – megasaw
    Oct 28 '21 at 8:29
  • $\begingroup$ They are predicted behind the scenes in model.fit, where the labels and predicted values are passed to the different metrics supplied in model.compile to calculate those metrics. $\endgroup$
    – Oxbowerce
    Oct 28 '21 at 8:32
  • $\begingroup$ I use tensorflow.addons and apparently works fine, thanks. $\endgroup$
    – megasaw
    Oct 28 '21 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.