Keras: ValueError: 'Unknown metric function'

Created on 5 Oct 2017  路  3Comments  路  Source: keras-team/keras

Hello, I have "unknown metric function" error for my custom metric. There is a similar issue for when loading a model whereas in mine, problem occurs in compile part.

The same metric works without problem in the following model

        model = Sequential()
    model.add(Dense(hidden_neurons, input_dim=inputdim, init='normal', activation='relu'))
    model.add(Dense(hidden_neurons, init='normal', activation='relu'))
    model.add(Dense(hidden_neurons, init='normal', activation='relu'))
    model.add(Dense(1, init='normal')) #output layer

    model.compile(loss='mean_squared_error', optimizer='adam', metrics=[my_metric])

but does not work when model is similar to this.

tweet_a = Input(shape=(140, 256))
tweet_b = Input(shape=(140, 256))
shared_lstm = LSTM(64)
encoded_a = shared_lstm(tweet_a)
encoded_b = shared_lstm(tweet_b)
merged_vector = keras.layers.concatenate([encoded_a, encoded_b], axis=-1)
predictions = Dense(1, activation='sigmoid')(merged_vector)
model = Model(inputs=[tweet_a, tweet_b], outputs=predictions)
model.compile(optimizer='adam',  loss='mean_squared_error', metrics=['my_metric'])

This is my metric:

def my_metric(y_true, y_pred):

    g = tf.subtract(tf.expand_dims(y_pred, -1), y_pred)
    g = tf.cast(g == 0.0, tf.float32) * 0.5 + tf.cast(g > 0.0, tf.float32)

    f = tf.subtract(tf.expand_dims(y_true, -1), y_true) > 0.0
    f = tf.matrix_band_part(tf.cast(f, tf.float32), -1, 0)

    g = tf.reduce_sum(tf.multiply(g, f))
    f = tf.reduce_sum(f)

    return tf.where(tf.equal(g, 0), 0.0, g/f) #select

Could you please help me about this? Thank you!

Most helpful comment

You're passing the metric as a string in the second example.

model.compile(optimizer='adam',  loss='mean_squared_error', metrics=['my_metric'])

Pass it like this:

model.compile(optimizer='adam',  loss='mean_squared_error', metrics=[my_metric])

All 3 comments

You're passing the metric as a string in the second example.

model.compile(optimizer='adam',  loss='mean_squared_error', metrics=['my_metric'])

Pass it like this:

model.compile(optimizer='adam',  loss='mean_squared_error', metrics=[my_metric])

You are right @nicolewhite, thanks! Sorry for such a trivial issue!

Regarding this mistake, I believe the documentation is wrong here https://keras.io/metrics/#custom-metrics
The metrics parameter cannot be a list if one wants to give a name to a custom metric, it has to be a dictionnary (doc defines metrics=['accuracy', mean_pred] ) -->
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics={'accuracy': mean_pred})

Was this page helpful?
0 / 5 - 0 ratings