Good afternoon, QuantConnect team!
Following some discussion about QC's ObjectStore: is it possible to save Neural Networks, Random Forests and Gradient Boosters (from Tensorflow and Scikit) as strings to save/load from the ObjectStore?
If not, how else might I be able to save and load them in a backtesting algorithm?
Thanks!
Mia Alissi
Good afternoon!
Yes, it is possible to save and load models, including Neural Networks, Random Forests, and Gradient Boosters, in the Object Store in QuantConnect. The Object Store is a key-value storage location that allows you to save and retrieve data in QuantConnect's cache. It is shared across the entire organization, and using the same key, you can access data across all projects in an organization.
For Scikit-Learn models, you can save models in the Object Store by following these steps:
GetFilePath
method with the key.dump
method with the model and file path. If you dump the model using thejoblib
module before you save the model, you don't need to retrain the model.For Keras models, you can save models in the Object Store by following these steps:
GetFilePath
method with the key.save
method with the file path.To load the models, you can follow these steps:
ContainsKey
method.GetFilePath
method with the key.load
method with the file path, environment, and policy (for Stable Baselines).For TensorFlow models, you might need to serialize the model weights and architecture into a format that can be stored in the Object Store, similar to how it's done with PyTorch models as discussed in the forum. However, the exact steps might vary depending on the specifics of your TensorFlow model.
Please note that the process of adding new libraries takes 2-4 weeks to complete as they need to be reviewed for security and deployment.
Sources:
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.
Tom Frew
Thank you, Mia! What about if I wanted to save the model as part of a larger object and store that in the ObjectStore? Like, for example, what if I wanted to have:
ModelData:
I was thinking we could use the dictionary-saving method you created in this question, but your solution saves the model directly. Could we instead store it in an intermediate string variable?
Thanks a lot!
Mia Alissi
Yes, you can certainly save the model as part of a larger object and store that in the Object Store. You can create a dictionary to hold the model's serialized structure, name, and training time, and then save this dictionary to the Object Store.
Here's an example of how you might do this:
In this example, we first serialize the model using
joblib.dump
, then encode the serialized model to a base64 string so it can be stored in a dictionary. We then convert the dictionary to a string usingpickle.dumps
before saving it to the Object Store.To retrieve the model data from the Object Store, you can do:
Please note that this is a simplified example and you might need to adjust the code to suit your specific needs. For instance, you might need to handle errors and edge cases, and you might want to use a more efficient serialization format if your model is large.
Also, please note that the Object Store has helper methods to store strings, JSON objects, XML objects, and bytes. If you need to store an object that is in a different format, you need to encode it to one of the supported data types.
Sources:
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.
Tom Frew
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!