Skip to content

Commit

Permalink
Handle copying keras history result
Browse files Browse the repository at this point in the history
  • Loading branch information
Uri Granta committed Aug 1, 2023
1 parent 314fee9 commit 7b01105
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions trieste/models/gpflux/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ def __getstate__(self) -> dict[str, Any]:
finally:
self._model_keras.history.model = history_model

# don't try to serialize any other copies of the history callback
if isinstance(state.get("_last_optimization_result"), keras.callbacks.History):
state["_last_optimization_result"] = ...

return state

def __setstate__(self, state: dict[str, Any]) -> None:
Expand Down Expand Up @@ -265,6 +269,8 @@ def __setstate__(self, state: dict[str, Any]) -> None:
model = tf.keras.models.model_from_json(model_json)
model.set_weights(weights)
self._model_keras.history.set_model(model)
if state.get("_last_optimization_result") is ...:
self._last_optimization_result = self._model_keras.history

def __repr__(self) -> str:
""""""
Expand Down
7 changes: 7 additions & 0 deletions trieste/models/keras/architectures.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from typing import Any, Callable, Sequence

import dill
import keras.callbacks
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
Expand Down Expand Up @@ -127,6 +128,10 @@ def __getstate__(self) -> dict[str, Any]:
finally:
self._model.history.model = history_model

# Don't try to serialize any other copies of the history callback
if isinstance(state.get("_last_optimization_result"), keras.callbacks.History):
state["_last_optimization_result"] = ...

return state

def __setstate__(self, state: dict[str, Any]) -> None:
Expand All @@ -150,6 +155,8 @@ def __setstate__(self, state: dict[str, Any]) -> None:
)
model.set_weights(weights)
self._model.history.set_model(model)
if state.get("_last_optimization_result") is ...:
self._last_optimization_result = self._model.history


class KerasEnsembleNetwork:
Expand Down

0 comments on commit 7b01105

Please sign in to comment.