Back

Saving Keras Weights

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!

Update Backtest







0

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.


According to 

https://www.quantconnect.com/docs/key-concepts/supported-libraries

The 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().

1

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)

 

1

Update Backtest





0

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.


Loading...

This discussion is closed