huseinzol05 / Stock-Prediction-Models

Gathers machine learning and deep learning models for Stock forecasting including trading bots and simulations
Apache License 2.0
7.66k stars 2.74k forks source link

How do i save a Model or Agent after training so i can use just use it without training ? #85

Closed adamfils closed 3 years ago

adamfils commented 3 years ago

Can please provide like a code snippet to do that? It will be super help full Thanks🙏

IISuperluminaLII commented 3 years ago
with open('....model_name....', 'wb') as file:
    pickle.dump(model.copy(), file)

this creates the file to write bytes(wb) of the class (strategy) to the file as the python standard pickle file.

adamfils commented 3 years ago

Thanks

dougmbaya commented 3 years ago

How do we save and load tf sessions so we don't have to retrain the model every time. Note that tf models are thread locked so you can't pickle them. Is there a code on how to export into .pb or pbtxt and maintain accuracy?

IISuperluminaLII commented 3 years ago

unless you are using the same random seed across your sessions then accuracy cannot be guaretted. As far saving your model goes, you can either convert to keras model and create a custom callback, or since each layer of the tf model is callable you can convert to numpy and save the weights of each layer/variable..... there are multiple ways to approach this problem. I converted mine to tf 2.0 which has eager execution disabled and no need to worry about sessions as a whole....

dougmbaya commented 3 years ago

unless you are using the same random seed across your sessions then accuracy cannot be guaretted. As far saving your model goes, you can either convert to keras model and create a custom callback, or since each layer of the tf model is callable you can convert to numpy and save the weights of each layer/variable..... there are multiple ways to approach this problem. I converted mine to tf 2.0 which has eager execution disabled and no need to worry about sessions as a whole....

Hey @IISuperluminaLII would you be able to share part your code where you load the model and predict? I'm still new to tf. I have enough compute to train once or twice a day but cannot train each time. Thanks!

IISuperluminaLII commented 3 years ago

which model are you talking about? it is implementation dependent as layer names and layer configurations are different across the models.

dougmbaya commented 3 years ago

I'm using the LSTM.