how to implement custom metric in keras?

Here I’m answering to OP’s topic question rather than his exact problem. I’m doing this as the question shows up in the top when I google the topic problem. You can implement a custom metric in two ways. As mentioned in Keras docu. import keras.backend as K def mean_pred(y_true, y_pred): return K.mean(y_pred) model.compile(optimizer=”sgd”, loss=”binary_crossentropy”, metrics=[‘accuracy’, … Read more

How to replace (or insert) intermediate layer in Keras model?

The following function allows you to insert a new layer before, after or to replace each layer in the original model whose name matches a regular expression, including non-sequential models such as DenseNet or ResNet. import re from keras.models import Model def insert_layer_nonseq(model, layer_regex, insert_layer_factory, insert_layer_name=None, position=’after’): # Auxiliary dictionary to describe the network graph … Read more

Keras: How to save model and continue training?

As it’s quite difficult to clarify where the problem is, I created a toy example from your code, and it seems to work alright. import numpy as np from numpy.testing import assert_allclose from keras.models import Sequential, load_model from keras.layers import LSTM, Dropout, Dense from keras.callbacks import ModelCheckpoint vec_size = 100 n_units = 10 x_train = … Read more

Loading model with custom loss + keras

Yes, there is! custom_objects expects the exact function that you used as loss function (the inner one in your case): model = load_model(modelFile, custom_objects={ ‘loss’: penalized_loss(noise) }) Unfortunately keras won’t store in the model the value of noise, so you need to feed it to the load_model function manually.

How is the Keras Conv1D input specified? I seem to be lacking a dimension

Don’t let the name confuse you. The layer tf.keras.layers.Conv1D needs the following shape: (time_steps, features). If your dataset is made of 10,000 samples with each sample having 64 values, then your data has the shape (10000, 64), which is not directly applicable to the tf.keras.layers.Conv1D layer. You are missing the time_steps dimension. What you can … Read more