def predict_price(currency, start_date, end_date): window_len = 20 model_path = "models/%s_model.h5" % currency market_info = pd.read_html("https://coinmarketcap.com/currencies/%s/historical-data/?start=%s&end=%s" % (currency, start_date, end_date))[0] # convert the date string to the correct date format market_info = market_info.assign(Date=pd.to_datetime(market_info['Date'])) temp_list = [] for i in range(0, len(market_info.columns)): if market_info.columns[i] == "Close**": temp_list.append("Close") elif market_info.columns[i] == "Open*": temp_list.append("Open") else: temp_list.append(market_info.columns[i]) market_info.columns = temp_list # Feature Eng # print(market_info.columns) market_info.columns = [market_info.columns[0]] + [currency + '_' + i for i in market_info.columns[1:]] # print(market_info.columns) kwargs = { currency + '_day_diff': lambda x: (x[currency + '_Close'] - x[currency + '_Open']) / x[currency + '_Open']} market_info = market_info.assign(**kwargs) kwargs = { currency + '_close_off_high': lambda x: 2 * (x[currency + '_High'] - x[currency + '_Close']) / (x[currency + '_High'] - x[currency + '_Low']) - 1, currency + '_volatility': lambda x: (x[currency + '_High'] - x[currency + '_Low']) / (x[currency + '_Open'])} market_info = market_info.assign(**kwargs) model_data = market_info[['Date'] + [currency + "_" + metric for metric in ['Close', 'Volume', 'close_off_high', 'volatility', 'day_diff', 'Market Cap']]] # need to reverse the data frame so that subsequent rows represent later timepoints model_data = model_data.sort_values(by='Date') model_data = model_data.drop('Date', 1) norm_cols = [currency + "_" + metric for metric in ['Close', 'Volume', 'Market Cap']] LSTM_test_inputs = ModelUtils.buildLstmInput(model_data, norm_cols, window_len) LSTM_test_inputs = [np.array(LSTM_test_inputs) for LSTM_test_inputs in LSTM_test_inputs] LSTM_test_inputs = np.array(LSTM_test_inputs) if os.path.isfile(model_path): estimator = load_model(model_path, custom_objects={'r2_keras': ModelUtils.r2_keras}) return (((np.transpose(estimator.predict(LSTM_test_inputs)) + 1) * model_data[currency + '_Close'].values[:-window_len])[0])[0] else: print("Please train the model for %s currency" % currency) return
training_set = training_set.drop('Date', 1) test_set = test_set.drop('Date', 1) print(model_data.columns) norm_cols = [ v for i, v in enumerate(model_data.columns) if v not in ['Date', 'bt_day_diff', 'bt_close_off_high', 'bt_volatility'] ] # norm_cols = [coin + metric for coin in ['bt_'] for metric in \ # ['Close', 'High', 'Low', 'Open', 'Volume', 'Market Cap', \ # 'MA_5', 'EMA_5', 'MOM_5', 'ROC_5', 'ATR_5', 'BollingerB1_5', 'BollingerB2_5', \ # 'PP', 'R1', 'S1', 'R2', 'S2', 'R3', 'S3', 'SOK', 'SOK_5', 'SOD_5', 'Trix_5', 'ADX_5', \ # 'MACD_10_5', 'MACDsign_10_5', 'MACDdiff_10_5', 'Mass_Index', 'Vortex_5', 'KST' \ # , 'RSI_5', 'TSI_10_5','ret_level1' ,'ret_level2' ,'ret_level3' ,'ext_level1' ,'ext_level2' ,'ext_level3']] # LSTM_training_inputs = ModelUtils.buildLstmInput(training_set, norm_cols, window_len)[30:] # model output is next price normalised to 10th previous closing price LSTM_training_outputs = ModelUtils.buildLstmOutput(training_set, 'bt_Close', window_len)[30:] LSTM_test_inputs = ModelUtils.buildLstmInput(test_set, norm_cols, window_len) # model output is next price normalised to 10th previous closing price LSTM_test_outputs = ModelUtils.buildLstmOutput(test_set, 'bt_Close', window_len) print("\nNumber Of Input Training's sequences: {}".format( len(LSTM_training_inputs))) print("\nNumber Of Output Training's sequences: {}".format( len(LSTM_training_outputs)))
def trainTheModel(): model_data = extractFeaturesFromData() print("Training Start") #need to reverse the data frame so that subsequent rows represent later timepoints model_data = model_data.sort_values(by='date') # #create Training and Test set # training_set, test_set = model_data[model_data['date'] < split_date], model_data[model_data['date'] >= split_date]##2 yr data for training and 4 mnth for testing # test_set=test_set[test_set['date']<=end_date] # training_set,test_set=train_test_split(model_data,shuffle=False,test_size=.3) training_set = model_data # # we don't need the date columns anymore training_set = training_set.drop('date', 1) # test_set = test_set.drop('date', 1) #Prepare model input and output LSTM_training_inputs = ModelUtils.buildLstmInput(training_set, None, window_len) LSTM_training_outputs = ModelUtils.buildLstmOutput(training_set, 'bt_close', window_len) # LSTM_test_inputs = ModelUtils.buildLstmInput(test_set, None, window_len) # LSTM_test_outputs = ModelUtils.buildLstmOutput(test_set, 'bt_close', window_len) print("\nNumber Of Input Training's sequences: {}".format( len(LSTM_training_inputs))) print("\nNumber Of Output Training's sequences: {}".format( len(LSTM_training_outputs))) # print("\nNumber Of Input Test's sequences: {}".format(len(LSTM_test_inputs))) # print("\nNumber Of Output Test's sequences: {}".format(len(LSTM_test_outputs))) #convert to numpy array LSTM_training_inputs = [ np.array(LSTM_training_input) for LSTM_training_input in LSTM_training_inputs ] LSTM_training_inputs = np.array(LSTM_training_inputs) LSTM_training_inputs = [x.ravel() for x in LSTM_training_inputs] LSTM_training_inputs = np.array(LSTM_training_inputs) LSTM_training_outputs = [ np.array(LSTM_training_output) for LSTM_training_output in LSTM_training_outputs ] LSTM_training_outputs = np.array(LSTM_training_outputs) LSTM_training_outputs = [x.ravel() for x in LSTM_training_outputs] LSTM_training_outputs = np.array(LSTM_training_outputs) # LSTM_test_inputs = [np.array(LSTM_test_input) for LSTM_test_input in LSTM_test_inputs] # LSTM_test_inputs = np.array(LSTM_test_inputs) # LSTM_test_inputs = [x.ravel() for x in LSTM_test_inputs] # LSTM_test_inputs = np.array(LSTM_test_inputs) # # LSTM_test_outputs = [np.array(LSTM_test_output) for LSTM_test_output in LSTM_test_outputs] # LSTM_test_outputs = np.array(LSTM_test_outputs) # LSTM_test_outputs = [x.ravel() for x in LSTM_test_outputs] # LSTM_test_outputs = np.array(LSTM_test_outputs) #reshape input to be 3D [samples, timesteps, features] as LSTM accepts only 3D vector train_X = LSTM_training_inputs.reshape( (LSTM_training_inputs.shape[0], 1, LSTM_training_inputs.shape[1])) # test_X = LSTM_test_inputs.reshape((LSTM_test_inputs.shape[0], 1, LSTM_test_inputs.shape[1])) # print(train_X.shape, LSTM_training_outputs.shape, test_X.shape, LSTM_test_outputs.shape) #Build the model model = ModelUtils.build_model(train_X.shape[1:], output_size=2, neurons_lv1=num_of_neurons_lv1, neurons_lv2=num_of_neurons_lv2, neurons_lv3=num_of_neurons_lv3, neurons_lv4=num_of_neurons_lv4) # #Fit data to the model # history = model.fit(train_X, LSTM_training_outputs, epochs=200, batch_size=3000, validation_data=(test_X,LSTM_test_outputs), verbose=2, shuffle=False) history = model.fit(train_X, LSTM_training_outputs, epochs=300, batch_size=3000, verbose=2, shuffle=False) # #Make predictions to find accuracy # pre = (model.predict(test_X)) # # #Classification report # b = np.zeros_like(pre) # b[np.arange(len(pre)), pre.argmax(1)] = 1 # print( metrics.classification_report(LSTM_test_outputs,b)) #Save trained model model.save(model_path) model.save_weights(model_weights_path) print('model saved')
model_data = eth_market_info[['Date'] + [coin + metric for coin in [ 'eth_'] for metric in ['Close', 'Volume', 'close_off_high', 'volatility', 'day_diff', 'Market Cap']]] # need to reverse the data frame so that subsequent rows represent later timepoints model_data = model_data.sort_values(by='Date') ''' print("Model Data") print('\nshape: {}'.format(model_data.shape)) print(model_data.head()) print("\n") ''' model_data = model_data.drop('Date', 1) norm_cols = [coin + metric for coin in ['eth_'] for metric in ['Close', 'Volume', 'Market Cap']] LSTM_test_inputs = ModelUtils.buildLstmInput(model_data, norm_cols, window_len) # print("\nNumber Of Input Test's sequences: {}".format(len(LSTM_test_inputs))) # I find it easier to work with numpy arrays rather than pandas dataframes # especially as we now only have numerical data LSTM_test_inputs = [np.array(LSTM_test_inputs) for LSTM_test_inputs in LSTM_test_inputs] LSTM_test_inputs = np.array(LSTM_test_inputs) # if best iteration's model was saved then load and use it if os.path.isfile(model_path): estimator = load_model(model_path, custom_objects={'r2_keras': ModelUtils.r2_keras}) print((((np.transpose(estimator.predict(LSTM_test_inputs)) + 1) * model_data['eth_Close'].values[:-window_len])[0])[0]) predictions.append((((np.transpose(estimator.predict(LSTM_test_inputs)) + 1) * model_data['eth_Close'].values[:-window_len])[0])[0])
def train_model(currency, from_date, to_date, model_path): # Splitting the data into 65-35 ratio. 65% point will be the split date. fd = datetime.datetime(int(from_date[0:4]), int(from_date[4:6]), int(from_date[6:])) td = datetime.datetime(int(to_date[0:4]), int(to_date[4:6]), int(to_date[6:])) delta = 0.90 * (td - fd) split_date = fd + datetime.timedelta(days=int(str(delta).split()[0])) # Our LSTM model will use previous data to predict the next day's closing price of eth. # We must decide how many previous days it will have access to window_len = 20 eth_epochs = 100 eth_batch_size = 32 num_of_neurons_lv1 = 50 num_of_neurons_lv2 = 25 # Get Market info market_info = pd.read_html( "https://coinmarketcap.com/currencies/%s/historical-data/?start=%s&end=%s" % (currency, from_date, to_date))[0] temp_list = [] for i in range(0, len(market_info.columns)): if market_info.columns[i] == "Close**": temp_list.append("Close") elif market_info.columns[i] == "Open*": temp_list.append("Open") else: temp_list.append(market_info.columns[i]) market_info.columns = temp_list # convert the date string to the correct date format market_info = market_info.assign(Date=pd.to_datetime(market_info['Date'])) market_info.columns = [market_info.columns[0]] + [ currency + '_' + i for i in market_info.columns[1:] ] kwargs = { currency + '_day_diff': lambda x: (x[currency + '_Close'] - x[currency + '_Open']) / x[ currency + '_Open'] } market_info = market_info.assign(**kwargs) kwargs = { currency + '_close_off_high': lambda x: 2 * (x[currency + '_High'] - x[currency + '_Close']) / (x[currency + '_High'] - x[currency + '_Low']) - 1, currency + '_volatility': lambda x: (x[currency + '_High'] - x[currency + '_Low']) / (x[currency + '_Open']) } market_info = market_info.assign(**kwargs) model_data = market_info[['Date'] + [ currency + "_" + metric for metric in [ 'Close', 'Volume', 'close_off_high', 'volatility', 'day_diff', 'Market Cap' ] ]] model_data = model_data.sort_values(by='Date') training_set, test_set = model_data[ model_data['Date'] < split_date], model_data[ model_data['Date'] >= split_date] # we don't need the date columns anymore training_set = training_set.drop('Date', 1) test_set = test_set.drop('Date', 1) norm_cols = [ currency + "_" + metric for metric in ['Close', 'Volume', 'Market Cap'] ] LSTM_training_inputs = ModelUtils.buildLstmInput(training_set, norm_cols, window_len) LSTM_training_outputs = ModelUtils.buildLstmOutput(training_set, currency + '_Close', window_len) LSTM_test_inputs = ModelUtils.buildLstmInput(test_set, norm_cols, window_len) LSTM_test_outputs = ModelUtils.buildLstmOutput(test_set, currency + '_Close', window_len) LSTM_training_inputs = [ np.array(LSTM_training_input) for LSTM_training_input in LSTM_training_inputs ] LSTM_training_inputs = np.array(LSTM_training_inputs) LSTM_test_inputs = [ np.array(LSTM_test_inputs) for LSTM_test_inputs in LSTM_test_inputs ] LSTM_test_inputs = np.array(LSTM_test_inputs) # initialise model architecture eth_model = ModelUtils.build_model(LSTM_training_inputs, output_size=1, neurons_lv1=num_of_neurons_lv1, neurons_lv2=num_of_neurons_lv2) # train model on data eth_history = eth_model.fit( LSTM_training_inputs, LSTM_training_outputs, epochs=eth_epochs, batch_size=eth_batch_size, verbose=2, shuffle=True, validation_split=0.2, callbacks=[ keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=10, mode='min'), keras.callbacks.ModelCheckpoint(model_path, monitor='val_loss', save_best_only=True, mode='min', verbose=0) ]) # We've just built an LSTM model to predict tomorrow's Ethereum closing price. scores = eth_model.evaluate(LSTM_test_inputs, LSTM_test_outputs, verbose=1, batch_size=eth_batch_size) print('\nMSE: {}'.format(scores[1])) print('\nMAE: {}'.format(scores[2])) print('\nR^2: {}'.format(scores[3])) # Plot Error # figErr, ax1 = plt.subplots(1, 1) # ax1.plot(eth_history.epoch, eth_history.history['loss']) # ax1.set_title('Training Error') # if eth_model.loss == 'mae': # ax1.set_ylabel('Mean Absolute Error (MAE)', fontsize=12) # # just in case you decided to change the model loss calculation # else: # ax1.set_ylabel('Model Loss', fontsize=12) # ax1.set_xlabel('# Epochs', fontsize=12) # #plt.show() # figErr.savefig("output/%s_error.png" % currency) ##################################### # EVALUATE ON TEST DATA ##################################### # Plot Performance # fig, ax1 = plt.subplots(1, 1, figsize=(10, 10)) # ax1.set_xticks([datetime.date(2017, i + 1, 1) for i in range(12)]) # ax1.set_xticklabels([datetime.date(2017, i + 1, 1).strftime('%b %d %Y') for i in range(12)]) # ax1.plot(model_data[model_data['Date'] >= split_date]['Date'][window_len:].astype(datetime.datetime), # test_set[currency + '_Close'][window_len:], label='Actual') # ax1.plot(model_data[model_data['Date'] >= split_date]['Date'][window_len:].astype(datetime.datetime), # ((np.transpose(eth_model.predict(LSTM_test_inputs)) + 1) * test_set[currency + '_Close'].values[:-window_len])[0], # label='Predicted') # ax1.annotate('MAE: %.4f' % np.mean(np.abs((np.transpose(eth_model.predict(LSTM_test_inputs)) + 1) - \ # (test_set[currency + '_Close'].values[window_len:]) / (test_set[currency + '_Close'].values[:-window_len]))), # xy=(0.75, 0.9), xycoords='axes fraction', # xytext=(0.75, 0.9), textcoords='axes fraction') # ax1.set_title('Test Set: Single Timepoint Prediction', fontsize=13) # ax1.set_ylabel('Ethereum Price ($)', fontsize=12) # ax1.legend(bbox_to_anchor=(0.1, 1), loc=2, borderaxespad=0., prop={'size': 14}) # #plt.show() # fig.savefig("output/%s_performanceTraining.png" % currency) return
print("\n") # create Training and Test set training_set, test_set = model_data[ model_data['Date'] < split_date], model_data[ model_data['Date'] >= split_date] # we don't need the date columns anymore training_set = training_set.drop('Date', 1) test_set = test_set.drop('Date', 1) norm_cols = [ coin + metric for coin in ['eth_'] for metric in ['Close', 'Volume', 'Market Cap'] ] LSTM_training_inputs = ModelUtils.buildLstmInput(training_set, norm_cols, window_len) # model output is next price normalised to 10th previous closing price LSTM_training_outputs = buildLstmOutputMoreDays(training_set, 'eth_Close', pred_range) LSTM_test_inputs = ModelUtils.buildLstmInput(test_set, norm_cols, window_len) # model output is next price normalised to 10th previous closing price LSTM_test_outputs = buildLstmOutputMoreDays(test_set, 'eth_Close', pred_range) print("\nNumber Of Input Training's sequences: {}".format( len(LSTM_training_inputs))) print("\nNumber Of Output Training's sequences: {}".format( len(LSTM_training_outputs))) print("\nNumber Of Input Test's sequences: {}".format(
def predikcija(): if __name__ == '__main__': ######################## # Configuration ######################## model_path = "../Output/bt_model.h5" start_dates = [] for dana_unazad in range(30, 0, -1): danas = datetime.date.today() - timedelta(days=dana_unazad) start_dates.append(str(danas.strftime("%Y%m%d"))) print(start_dates) end_dates = [] for dana_unazad in range(1, -3, -1): danas = datetime.date.today() - timedelta(days=dana_unazad) end_dates.append(str(danas.strftime("%Y-%m-%d"))) print(end_dates) # get market info for bitcoin from the start of 2016 to the current day bt_market_info = pd.read_html("https://coinmarketcap.com/currencies/bitcoin/historical-data/?start=" + end_dates[0] + "&end=" + end_dates[len(end_dates)-1])[0] # convert the date string to the correct date format bt_market_info = bt_market_info.assign(Date=pd.to_datetime(bt_market_info['Date'])) burza = bt_market_info # look at the first few rows print("BT") print(bt_market_info.head()) print('\nshape: {}'.format(bt_market_info.shape)) print("\n") print(bt_market_info.Date) print(bt_market_info.Close) ground_truth = bt_market_info.Close #[9888.61, 10233.60, 10975.60] predictions = [] window_len = 20 ################################### # Getting the BTC ################################### bt_img = urllib.request.urlopen("http://logok.org/wp-content/uploads/2016/10/Bitcoin-Logo-640x480.png") #bt_img = open("../Output/Bitcoin-Logo-640x480.png") image_file = io.BytesIO(bt_img.read()) bt_im = Image.open(image_file) width_bt_im , height_bt_im = bt_im.size bt_im = bt_im.resize((int(bt_im.size[0] * 0.8), int(bt_im.size[1] * 0.8)), Image.ANTIALIAS) for start_date, end_date in zip(start_dates, end_dates): ################ # Data Ingestion ################ # get market info for BTC from the start of 2016 to the current day time.strftime("%Y%m%d") bt_market_info = pd.read_html("https://coinmarketcap.com/currencies/bitcoin/historical-data/?start=" + start_date + "&end=" + end_date)[0] # convert the date string to the correct date format bt_market_info = bt_market_info.assign(Date=pd.to_datetime(bt_market_info['Date'])) ''' # look at the first few rows print("ETH") print(bt_market_info.head()) print('\nshape: {}'.format(bt_market_info.shape)) print("\n") # PlotUtils.plotCoinTrend(bt_market_info, bt_im) ''' # Feature Eng # print("Feature ENG") bt_market_info.columns = [bt_market_info.columns[0]] + ['bt_' + i for i in bt_market_info.columns[1:]] for coins in ['bt_']: kwargs = { coins + 'day_diff': lambda x: (x[coins + 'Close'] - x[coins + 'Open']) / x[coins + 'Open']} bt_market_info = bt_market_info.assign(**kwargs) ''' print('\nshape: {}'.format(bt_market_info.shape)) print(bt_market_info.head()) print("\n") ''' ########################################################################################################### # DATA PREPARATION # In time series models, we generally train on one period of time and then test on another separate period. # I've created a new data frame called model_data. # I've removed some of the previous columns (open price, daily highs and lows) and reformulated some new ones. # close_off_high represents the gap between the closing price and price high for that day, where values of -1 and 1 # mean the closing price was equal to the daily low or daily high, respectively. # The volatility columns are simply the difference between high and low price divided by the opening price. # You may also notice that model_data is arranged in order of earliest to latest. # We don't actually need the date column anymore, as that information won't be fed into the model. ########################################################################################################### # close_off_high = 2 * (High - Close) / (High - Low) - 1 # volatility = (High - Low) / Open for coins in ['bt_']: kwargs = { coins + 'close_off_high': lambda x: 2 * (x[coins + 'High'] - x[coins + 'Close']) / (x[coins + 'High'] - x[coins + 'Low']) - 1, coins + 'volatility': lambda x: (x[coins + 'High'] - x[coins + 'Low']) / (x[coins + 'Open'])} bt_market_info = bt_market_info.assign(**kwargs) model_data = bt_market_info[['Date'] + [coin + metric for coin in [ 'bt_'] for metric in ['Close', 'Volume', 'close_off_high', 'volatility', 'day_diff', 'Market Cap']]] # need to reverse the data frame so that subsequent rows represent later timepoints model_data = model_data.sort_values(by='Date') ''' print("Model Data") print('\nshape: {}'.format(model_data.shape)) print(model_data.head()) print("\n") ''' model_data = model_data.drop('Date', 1) norm_cols = [coin + metric for coin in ['bt_'] for metric in ['Close', 'Volume', 'Market Cap']] LSTM_test_inputs = ModelUtils.buildLstmInput(model_data, norm_cols, window_len) # print("\nNumber Of Input Test's sequences: {}".format(len(LSTM_test_inputs))) # I find it easier to work with numpy arrays rather than pandas dataframes # especially as we now only have numerical data LSTM_test_inputs = [np.array(LSTM_test_inputs) for LSTM_test_inputs in LSTM_test_inputs] LSTM_test_inputs = np.array(LSTM_test_inputs) # if best iteration's model was saved then load and use it if os.path.isfile(model_path): estimator = load_model(model_path, custom_objects={'r2_keras': ModelUtils.r2_keras}) print(str(end_date) + '--> ') print((((np.transpose(estimator.predict(LSTM_test_inputs)) + 1) * model_data['bt_Close'].values[:-window_len])[0])[0]) predictions.append((((np.transpose(estimator.predict(LSTM_test_inputs)) + 1) * model_data['bt_Close'].values[:-window_len])[0])[0]) #zaokruzi na dvije decimale predictions = np.round(predictions,2) #datafreme predikcije stari datumi burza = pd.DataFrame(burza) burzaHtml = burza.to_html(justify=True) #datafreme predikcije tri dana dfPredikcija = pd.DataFrame() dfPredikcija['Datumi'] = end_dates dfPredikcija['Closing Price'] = predictions dfPredikcija = dfPredikcija.sort_values(by='Datumi', ascending=False) s = dfPredikcija.style.set_properties(**{'text-align': 'right'}) s.render() predictionsHtml = dfPredikcija.to_html(justify='right', index=False) #justfiy poravna samo zaglavlje predictionsHtml = predictionsHtml.replace('<tr>','<tr style="text-align: right;">') print(predictionsHtml) print('pearson test: WAIT!!! ') #print(pearsonr(predictions, ground_truth)) f = open('../Output/cijenaBTC.html','w', encoding='utf-16') message = """ <br> @{ <br> Layout = "~/_SiteLayout.cshtml"; Page.Title = "Predikcijski modeli, deep learning analize"; <br> } <br> <p> """ +str('Predikcija cijene za sljedeća tri dana')+""" <br> <br> </p> <br> """+str(predictionsHtml)+""" <br> <br> """+str(burzaHtml)+""" <br> <br> """+str(s.render())+""" <br> <br> <p> <img src="../BTCoutput/BitcoinTrend.png" alt="BTC"> <img src="../BTCoutput/BitcoinTrainTest.png" alt="BTC"> <img src="../BTCoutput/BTCperformanceTraining.png" alt="BTC"> <img src="../BTCoutput/BTCPredictionMoreDays.png" alt="BTC"> <img src="../BTCoutput/BTCmoreDays_error.png" alt="BTC"> </p> """ #kraj varijable message f.write(message) f.close()
os.makedirs("Models/" + folder_name, mode=0o0775) if not os.path.exists("Models/" + folder_name + "/Model Steps"): os.makedirs("Models/" + folder_name + "/Model Steps", mode=0o0775) #Initialize LSTM Model model = Bi_LSTM(ini['input_dim'], ini['hidden_dim'], ini['layer_dim'], ini["output_dim"], ini["readout_dim"], drop, graphics_card=ini["device"]) model_utils = ModelUtils(model=model, input_dim=ini['input_dim'], seq_dim=ini["seq_dim"], model_type="LSTM", learning_rate=lr, momentum=0.9, device_name=ini["device"], optimizer=ini["optimizer"], criterion="BCEWithLogitsLoss") metrics = model_utils.train_model(train_loader=train_loader, val_loaders=validations_loaders, epochs=ini["epochs"], metrics={}, save_epoch_model=True, start_epoch=0, eval_epochs=2, save_epochs_steps=1, path_model_save="Models/" + folder_name + "/Model Steps/") with open("Models/" + folder_name + "/model_metrics.json",
''' if len(models_steps_list) != 0 and ini["load_last_model"]: model.load_state_dict( torch.load("Model Steps/" + models_steps_list[len(models_steps_list) - 1])) first_epochs = int(models_steps_list[len(models_steps_list) - 1].split("_")[1].split(".")[0]) + 1 with open("Model Metrics/model_metrics.json") as metrics_file: metrics = json.load(metrics_file) # The script use the class ModelUtils that implements the function to train the model model_utils = ModelUtils(model=model, input_dim=ini['input_dim'], seq_dim=ini['seq_dim'], model_type=ini["model_type"], learning_rate=ini["learning_rate"], momentum=ini["momentum"], device_name=ini["device"], optimizer=ini["optimizer"], criterion="BCEWithLogitsLoss") metrics = model_utils.train_model(train_loader=train_loader, val_loaders=validations_loaders, epochs=ini["epochs"], metrics=metrics, save_epoch_model=True, start_epoch=first_epochs, eval_epochs=ini["eval_epochs"], save_epochs_steps=1, yolo=ini["yolo"]) # When the training finish the metrics obtained on each steps are saved into a json file. with open("Model Metrics/model_metrics.json", "w") as metrics_file:
print("\n") # create Training and Test set training_set, test_set = model_data[ model_data['Date'] < split_date], model_data[ model_data['Date'] >= split_date] # we don't need the date columns anymore training_set = training_set.drop('Date', 1) test_set = test_set.drop('Date', 1) norm_cols = [ coin + metric for coin in ['bt_'] for metric in ['Close', 'Volume', 'Market Cap'] ] LSTM_training_inputs = ModelUtils.buildLstmInput(training_set, norm_cols, window_len) # model output is next price normalised to 10th previous closing price LSTM_training_outputs = buildLstmOutputMoreDays(training_set, 'bt_Close', pred_range) LSTM_test_inputs = ModelUtils.buildLstmInput(test_set, norm_cols, window_len) # model output is next price normalised to 10th previous closing price LSTM_test_outputs = buildLstmOutputMoreDays(test_set, 'bt_Close', pred_range) print("\nNumber Of Input Training's sequences: {}".format( len(LSTM_training_inputs))) print("\nNumber Of Output Training's sequences: {}".format( len(LSTM_training_outputs))) print("\nNumber Of Input Test's sequences: {}".format(
layer_dim=ini['layer_dim'], output_dim=ini["output_dim"], drop_out=0.5, readout_dim=ini["readout_dim"], device=ini["device"], cnn_model=ini["cnn_bi_lstm_model"], yolo_dimension=ini["yolo_dimension"]) cnn_lstm_model.load_state_dict( torch.load("Model State/model_" + str(ini["model_to_load"]) + ".pth")) cnn_lstm_model.eval() model_utils = ModelUtils(model=cnn_lstm_model, seq_dim=ini["seq_dim"], input_dim=ini["input_dim"], model_type="CNN+LSTM", learning_rate=ini["learning_rate"], momentum=ini["momentum"], device_name=ini["device"], optimizer=ini["optimizer"], criterion="BCEWithLogitsLoss") out_sig, labels, predictions = model_utils.get_model_prediction( data_loader, pos=0, desc="Model Prediction", threshold=ini["threshold"], yolo=ini["yolo"]) pk.dump( predictions, open("Predictions/model_" + str(ini["model_to_load"]) + "_prediction.pkl", "wb")) #Threshold value equal to 0.5