Pickling Keras Models

It's pretty annoying that Keras doesn't support Pickle to serialize its objects (Models). Yes, the Model structure is serializable (keras.models.model_from_json) and so are the weights (model.get_weights), and we can always use the built-in keras.models.save_model to store it as an hdf5 file, but all these won't help when we want to store another object that references the model (like keras.callbacks.History), or use the %store magic of iPython notebook.

After some frustration, I ended up with a patchy solution that does the work for me. It's not the nicest thing, but works regardless of how you reference your Keras model. Basically, if an object has __getstate__ and __setstate__ methods, pickle will use them to serialize the object. The problem is that Keras Model doesn't implement these. My patchy solution adds those methods after the class has been loaded:

I have a general python module that I always import on all my notebooks and contains some stuff I always need so I just added it there. You can just add to one of your initializers files or the beginning of your script.

UPDATE 09/04/2017: If you're running on Windows and encountered an OSError: Unable to create file error, you might want to try setting delete=False in the NamedTemporaryFile constructor in both functions. Thanks Henry Thornton for pointing this out! (see comments below)