Keras documentation only appears to support HDF5 for storing weights:
https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model
However I see that QuantConnect implemented an ObjectStore feature here:
https://www.quantconnect.com/docs/algorithm-reference/machine-learning#Machine-Learning-Storing-Trained-Models
But when I look at the Keras example here, nothing is being stored or loaded:
https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/KerasNeuralNetworkAlgorithm.py
How can I store trained keras models with weights in QuantConnect?
Thank You!
Adam W
According toÂ
https://www.quantconnect.com/docs/key-concepts/supported-librariesThe package h5py is supported on QC, so this should be able to be put into an ObjectStore feature. Haven't played around with the feature yet, but something like might work for now:
Â
class MyAlgo(QCAlgorithm): def Initialize(self): # Store weights self.weights = None self.Schedule.On(*args, self.TrainModel) def TrainModel(self): # Some NN architecture # Some NN layer layer = Dense(5) # Weights for the layer in Numpy array layer_weights = layer.get_weights() self.weights = # Loop over all layers and store in list
Then when you are making predictions, set the layer weights with layer.set_weights().
Pierre Pohly
With the Object Store u can Load and Save the weights for ur keras model.Â
Save Weights:Â
w = model.get_weights() encodedWeights = json.dumps(w, cls=NumpyArrayEncoder) self.ObjectStore.Save("model_weights", str(encodedWeights))
Â
Load Weights:Â
decodedWeights = json.loads(self.ObjectStore.Read("model_weights")) w = np.asarray(decodedWeights) model.set_weights(w)
Â
Jonathan Ganucheau
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!