# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:dl]
#     language: python
#     name: conda-env-dl-py
# ---

# ## Model training
# > In this notebook we train a model for each stock and save it to disk.

# ### 1) Here we import the data_manager and model modules 
# > * Here we use the `'companies()'` method from and `'data_manager'` module.
# > * From the `'model'` module we use the `'model_selector'` method and the `'ModelLoader'` class.

# +
# %load_ext autoreload
# %aimport data_manager
# %aimport model
# %autoreload 1

from data_manager import *
from model import *
# -

# ###  2) Import company list
# > Here we read a csv file and import a list of company trade symbols

stocks = companies()
symbols = stocks['Symbol'].values.tolist()
print(symbols)

# ###  3) RNN model training
# > In this section we train a RNN model for each stock.
#
# In the model selection [notebook](./model_selection.ipynb) we seleted a bidirectional RNN model where we would pass parameter lists and then select the model with lowest MSE test. We use that model here to train every stock.

print(final_model.__doc__)
print(model_selector.__doc__)

#our model selector input variables
window_sizes = [5,7,10]
dropouts =  [0.25,0.4]
learn_rates = [0.01,0.001]
epochs = [100,200]
batch_size = 50

# ### Train one stock
# > Here lets train the first stock in our list and save to disk.

# +
result = model_selector(symbols[0], window_sizes, learn_rates, dropouts, epochs, batch_size,verbose=1)

print("\nResults : ")
print("-"*60)
print(result[0])

print(result[1])
#save trained model
ModelLoader.save(result[1]['ticker'],result[0],result[1])
print("Saved trained model for {}".format(result[1]['ticker']))
# -

# ### Training remaining stocks
# > * Here train the remaining stocks and save the model to directory.

# +
from keras import backend as K
import tensorflow as tf

for ticker in symbols[1:]:
    #release memory
    K.clear_session()
    tf.reset_default_graph()
    
    result = model_selector(ticker, window_sizes, learn_rates, dropouts, epochs, batch_size,verbose=2)
    
    #save trained model
    ModelLoader.save(result[1]['ticker'],result[0],result[1])
    print(" ==> Saved trained model for {}".format(result[1]['ticker']))


StarcoderdataJupyterScriptsDedupFiltered