Deepcopy keras model with custom loss function (custom objects)

Viewed 23

I have a very simple custom loss function that basically does mae*=2 if the predictions are smaller than true value else returns mae. Now I am training my model in an sklearn pipeline and I want to deepcopy the pipeline along with the model and custom objects as following:

def custom_loss(y_true, y_pred):
        mae = tf.keras.losses.MeanAbsoluteError()
        penalty = 2
        # penalize the loss heavily if the prediction is smaller than true
        loss = tf.where(
            condition=tf.greater(y_true, y_pred),
            x=mae(y_true, y_pred) * penalty,
            y=mae(y_true, y_pred)
        )
        return loss
regr = deepcopy(regr)
temp = RegressionRecords([], regr, r2_score(np.array(predict_df["true_data"]), np.array(predict_df["predictions"])), predict_df, None)
class PredictionTransformer(BaseEstimator, TransformerMixin):
    def __init__(self, estimator):
        self.estimator = estimator # Keras model passed in as estimator
    @property
    def history(self):
        return self.estimator.history

    @property
    def model(self):
        return self.estimator.model

    def fit(self, X, y):
        self.estimator.train(X, y)

    def predict(self, X):
        return self.estimator.transform(X)

But I get the following error:

File "/usr/lib/python3.8/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib/python3.8/copy.py", line 270, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python3.8/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.8/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.8/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.8/copy.py", line 205, in _deepcopy_list
    append(deepcopy(a, memo))
  File "/usr/lib/python3.8/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.8/copy.py", line 210, in _deepcopy_tuple
    y = [deepcopy(a, memo) for a in x]
  File "/usr/lib/python3.8/copy.py", line 210, in <listcomp>
    y = [deepcopy(a, memo) for a in x]
  File "/usr/lib/python3.8/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib/python3.8/copy.py", line 270, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python3.8/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.8/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.8/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib/python3.8/copy.py", line 270, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python3.8/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.8/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.8/copy.py", line 153, in deepcopy
    y = copier(memo)
  File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 337, in __deepcopy__
    new = pickle_utils.deserialize_model_from_bytecode(
  File "/usr/local/lib/python3.8/dist-packages/keras/saving/pickle_utils.py", line 48, in deserialize_model_from_bytecode
    model = save_module.load_model(temp_dir)
  File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/usr/local/lib/python3.8/dist-packages/keras/saving/saved_model/load.py", line 994, in revive_custom_object
    raise ValueError(
ValueError: Unable to restore custom object of type _tf_keras_metric. Please make sure that any custom layers are included in the `custom_objects` arg when calling `load_model()` and make sure that all layers implement `get_config` and `from_config`

I need to use sklearn pipeline because I am doing some other operations in the pipeline before running the final step which is the Keras model. I know what the error means but I can't find an easy way to fix it. Can anybody please help?

0 Answers
Related