def __init__(self, input_size: int = INPUT_SIZE, output_size: int = OUTPUT_SIZE, hidden_size: int = HIDDEN_SIZE, embed_size: int = EMBED_SIZE, lr: float = LEARNING_RATE, clip_grad: float = CLIP_GRAD, init_range: float = INIT_RANGE): input_layers = [ Embedding(input_size, embed_size, init_range), LSTM(embed_size, hidden_size, init_range) ] output_layers = [ Embedding(output_size, embed_size, init_range), LSTM(embed_size, hidden_size, init_range, previous=input_layers[1]), Softmax(hidden_size, output_size, init_range) ] self.input_layers, self.output_layers = input_layers, output_layers self.hidden_size = hidden_size self.embed_size = embed_size self.input_size = input_size self.output_size = output_size self.lr = lr self.clip_grad = clip_grad
def __init__(self, args): super().__init__() self.features = Features(args) self.rnn1 = LSTM(Features.size + args.button_num, self.rnn_size) self.rnn2 = LSTM(self.rnn_size, self.rnn_size) self.features_bn = nn.BatchNorm1d(Features.size + args.button_num) self.pred = nn.Linear(self.rnn_size, args.variable_num * 3)
def __init__(self): self.dropout_rate = DROPOUT_RATE self.psrc_lookup = Parameter() self.ptrg_lookup = Parameter() self.pwhy = Parameter() self.pby = Parameter() self.src_lstm = LSTM() self.trg_lstm = LSTM() self.scan_attributes()
def train(self, df_inp, df_lab, batches, epoch, test_num, modelPath=None, reuse=False): if modelPath is None: model = LSTM(self.CELL_SIZE * self.STEPS, 3, self.CELL_SIZE, self.STEPS, reuse) else: model = LSTM(self.CELL_SIZE * self.STEPS, 3, self.CELL_SIZE, self.STEPS, reuse, isRestore=True, modelPath=modelPath) df = pd.concat([df_inp, df_lab], axis=1) df = df.dropna(how='any') sizes = len(df) delCols = ['ma_p', 'ma_n'] for c in delCols: if c in df.columns: del df[c] df = shuffle(df) inp_datas = [] lab_datas = [] for i in range(sizes // batches): df_datas = df.iloc[i * batches:(i + 1) * batches] inp = df_datas.drop(columns=['Label']).values lab = df_datas['Label'].values inp_datas.append(inp) lab_datas.append(lab) test_inp = inp_datas[-1 * test_num:] test_lab = lab_datas[-1 * test_num:] del inp_datas[-1 * test_num:] del lab_datas[-1 * test_num:] for i in range(epoch): model.training(inp_datas, lab_datas) loss, accuracy, precision = model.testing(test_inp, test_lab) return model, loss, accuracy, precision
def train_2(self, df_data, df_test, batches, epoch, modelPath=None, reuse=False): if modelPath is None: model = LSTM(self.CELL_SIZE * self.STEPS, 3, self.CELL_SIZE, self.STEPS, reuse) else: model = LSTM(self.CELL_SIZE * self.STEPS, 3, self.CELL_SIZE, self.STEPS, reuse, isRestore=True, modelPath=modelPath) df_data_ = df_data.copy() sizes = len(df_data_) print('data size : ', sizes) inp_datas = [] lab_datas = [] for i in range(sizes // batches): df_datas = df_data_.iloc[i * batches:(i + 1) * batches] inp = df_datas.drop(columns=['Label']).values lab = df_datas['Label'].values inp_datas.append(inp) lab_datas.append(lab) df_test_ = df_test.copy() test_sizes = len(df_test_) test_inp = [] test_lab = [] inp = df_test_.drop(columns=['Label']).values lab = df_test_['Label'].values test_inp.append(inp) test_lab.append(lab) for i in range(epoch): model.training(inp_datas, lab_datas) loss, accuracy, precision = model.testing(test_inp, test_lab) return model, loss, accuracy, precision
def _build_model(self): """Build traning model. First embed user/item inputs into training dimension, then feed them in LSTMCell. Further affine the matrics into emission dimension after LSTM and emit the prediction. """ phase = 'ENCODE' with tf.variable_scope(phase): self.encode_user = Transform(self.user_input, self.user_hparas, phase) self.encode_item = Transform(self.item_input, self.item_hparas, phase) phase = 'LSTM' with tf.variable_scope(phase): self.lstm_user = LSTM(self.encode_user.output, self.user_hparas) self.lstm_item = LSTM(self.encode_item.output, self.item_hparas) phase = 'AFFINE' with tf.variable_scope(phase): self.trans_user = Transform(self.lstm_user.output, self.user_hparas, phase) self.trans_item = Transform(self.lstm_item.output, self.item_hparas, phase) phase = 'EMISSION' with tf.variable_scope(phase): self.dynamic_state = tf.einsum('ijl,kjl->jik', self.trans_user.output, self.trans_item.output, name='dynamic_state') self.stationary_state = tf.matmul(self.user_stationary_factor, self.item_stationary_factor, transpose_b=True, name='stationary_state') if self.loss_function == 'log_loss': logits = tf.add(self.dynamic_state * 0.5, self.stationary_state * 0.5, name='logits') self.logits = tf.nn.sigmoid(logits, name='logits_activation') elif self.loss_function == 'rmse': logits = tf.add(self.dynamic_state, self.stationary_state, name='logits') self.logits = tf.nn.relu(logits, name='logits_activation') else: raise NotImplementedError( "Didn't implement the loss function yet.") self.logits_last = self.logits[-1, :, :]
def __init__(self): self.dropout_rate = DROPOUT_RATE self.psrc_lookup = Parameter() self.ptrg_lookup = Parameter() self.pwhj = Parameter() self.pbj = Parameter() self.pwjy = Parameter() self.pby = Parameter() self.src_fw_lstm = LSTM() self.src_bw_lstm = LSTM() self.trg_lstm = LSTM() self.add_all_parameters() self.add_all_submodels()
def __init__(self, name, src_vocab_size, trg_vocab_size, embed_size, hidden_size, dropout_rate): self.name_ = name self.dropout_rate_ = dropout_rate self.psrc_lookup_ = Parameter([embed_size, src_vocab_size], I.XavierUniform()) self.ptrg_lookup_ = Parameter([embed_size, trg_vocab_size], I.XavierUniform()) self.pwhy_ = Parameter([trg_vocab_size, hidden_size], I.XavierUniform()) self.pby_ = Parameter([trg_vocab_size], I.Constant(0)) self.src_lstm_ = LSTM(name + "_src_lstm", embed_size, hidden_size) self.trg_lstm_ = LSTM(name + "_trg_lstm", embed_size, hidden_size)
def main(_): # set random seed for comparing the two result calculations tf.set_random_seed(1) notes = reader.get_notes() # get amount of pitch names n_vocab = len(set(notes)) network_input, network_output = reader.prepare_sequences(notes, n_vocab) # model = create_network(network_input, n_vocab) # train(model, network_input, network_output) model = LSTM(LR, BATCH_SIZE, TIME_STEPS, INPUT_SIZE, HIDDEN_UNITS, n_vocab) sess = tf.Session() init = tf.global_variables_initializer() sess.run(init) train(sess, network_input, network_output, model) # result = pred(sess, network_input, {}) sess.close()
def forecast_lstm(actions): lstm = LSTM(CONFIG) model = lstm.load_model() vocabulary = restore_vocabulary() actions_scores = readScores(CONFIG) previous_action = None for action in actions: if (action != ''): # Compare previous action with the incoming action. action = json.loads(action) incoming_action = action_to_vector(action, vocabulary) if (previous_action == None): previous_action = incoming_action continue score = getScore(actions_scores, action['name']) previous_action_transformed = lstm.pretransform_dataset( [previous_action], reshape=True) incoming_action_transformed = lstm.pretransform_dataset( [incoming_action], reshape=True) predicted = lstm.forecast(model, previous_action_transformed, incoming_action_transformed) # Print anomaly score and set the new one now as previous action. print lstm.calculate_score(incoming_action_transformed, predicted, score) previous_action = incoming_action
def main(): argv = sys.argv if len(argv) != 3: print('Usage: ' + argv[0] + ' model_name dataset') sys.exit(0) model_name = argv[1] data = datautils.load(argv[2]) normalized, mean, std = datautils.normalize(data) normalized, _ = datautils.differentiate(normalized) (train, test) = datautils.split(normalized, 0.7) # utils.plot_data(data) print("training set length: {}".format(len(train))) print("test set length: {}".format(len(test))) """train""" model = LSTM() time_steps = 20 # window size batch_size = 5 # data augmentation history = model.train(model_name, train, 130, batch_size, time_steps) utils.plot_history(history) """test""" head = int(len(test) * 0.6) tail = len(test) - head projection = model.evaluate(model_name, test[:head], tail) """plot""" test = datautils.undifferentiate(test, sum(train)) projection = datautils.undifferentiate(projection, sum(train) + sum(test)) testset_denorm = datautils.denormalize(test, mean, std) results_denorm = datautils.denormalize(projection, mean, std) utils.plot_multiple([testset_denorm, results_denorm], [0, head + 1])
def __init__(self, vocab_len, embed_dim, hidden_dim, output_dim, pretrained_vec, layers=1, bidirectional=False, layernorm=False): super().__init__() self.name = 'lstm' self.hidden_dim = hidden_dim self.bidirectional = bidirectional self.layernorm = layernorm self.layers = layers self.embedding = nn.Embedding(vocab_len, embed_dim) # not training embedding layer if pretrained embedding is provided if pretrained_vec is not None: self.embedding = self.embedding.from_pretrained(pretrained_vec, freeze=True) self.lstm = LSTM(input_dim=embed_dim, hidden_dim=hidden_dim, layers=layers, bidirectional=bidirectional, layernorm=layernorm) if self.bidirectional: self.fc = nn.Linear(2 * hidden_dim, output_dim) else: self.fc = nn.Linear(hidden_dim, output_dim)
def create_model(self): self.log('Creating model') self.log('vocab size : ' + str(len(self.vocab_to_ints))) self.model = LSTM(input_units=self.maxlen, hidden_units=self.hidden_dim, vocab_size=len(self.vocab_to_ints) + 1, embedding_dim=self.embedding_size) #.to(device)
def main(): argv = sys.argv if len(argv) != 4: print('Usage: ' + argv[0] + ' model_name dataset original_dataset') sys.exit(0) model_name = argv[1] data = datautils.load(argv[2]) original_data = datautils.load(argv[3]) normalized, mean, std = datautils.normalize(data) (eval_sequences, cuts_indexes) = split_evaluation_sequences(normalized) """eval""" model = LSTM() clean = np.empty(0) for head, tail in eval_sequences: if len(clean) == 0: y_init = 0 else: y_init = clean[-1] head_diff = datautils.differentiate(head, y_init) projection = model.evaluate(model_name, head_diff, tail) head = datautils.undifferentiate(head_diff, y_init) projection = datautils.undifferentiate(projection, head[-1]) clean = np.concatenate((clean, head, projection)) """plot""" clean_denorm = datautils.denormalize(clean, mean, std) utils.plot_multiple([original_data, clean_denorm], [0, 0], vertical_lines=cuts_indexes)
def main(_): # pp.pprint(flags.FLAGS.__flags) # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333) # run_config = tf.ConfigProto() # run_config.gpu_options.allow_growth = True # with tf.Session(config=run_config) as sess: with tf.Session() as sess: rnn_model = LSTM( sess, "SP500" # FLAGS.stock_count, # lstm_size=FLAGS.lstm_size, # num_layers=FLAGS.num_layers, # num_steps=FLAGS.num_steps, # input_size=FLAGS.input_size, # embed_size=FLAGS.embed_size, ) show_all_variables() # stock_data_list = load_sp500( # FLAGS.input_size, # FLAGS.num_steps, # k=FLAGS.stock_count, # target_symbol=FLAGS.stock_symbol, # ) rnn_model.build_graph() rnn_model.train_lstm_graph("SP500")
def __init__(self, args): super(AdvantageActorCriticLSTM, self).__init__() self.discount = args.episode_discount self.feature_num = 64 self.conv1 = nn.Conv2d(in_channels=args.screen_size[0], out_channels=32, kernel_size=3, stride=1) self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1) self.conv3 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1) self.conv4 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=2) # self.rnn = nn.LSTMCell(64 * 14 * 19, self.feature_num) self.rnn = LSTM(64 * 14 * 19, self.feature_num) self.hx = None self.cx = None self.batch_norm = nn.BatchNorm2d(64) self.action = nn.Linear(self.feature_num, args.button_num) self.value = nn.Linear(self.feature_num, 1) self.outputs = [] self.rewards = []
def train_blstm(options, trainData, validData, testData, n_layer, n_hidden): # Initialize Model model = LSTM() # Build Neural Network n_input = 84 n_hidden = n_hidden n_output = 1 # model.add(blstm_layer(n_input, n_hidden)) # model.add(DropOut()) model.add(hidden_layer(n_input, n_hidden)) for i in range(n_layer): model.add(blstm_layer(n_hidden, n_hidden)) # model.add(DropOut()) model.add(bi_avec_activate(n_hidden, 1)) # Choose optimizer adadelta = ADADELTA() options["optimizer"] = adadelta # model.compile(options) # Training train_err, valid_err, test_err = model.fit(trainData, validData, testData) del model return train_err, valid_err, test_err
def load_model(): global nn, sess, trump if nn is None and sess is None and trump is None: nn = LSTM(max_lr=0.01, min_lr=0.0005, seq_len=57, vocab_size=2402, n_embedding=1000, n_out=2402, n_cell=1000, rand_seed=1234) trump = trump_data.TRUMP( trump_data.trump_tokenized_dataset_path_default, trump_data.trump_token_dict_path_default) tf_config = tf.ConfigProto() tf_config.gpu_options.allow_growth = True init = tf.global_variables_initializer() sess = tf.InteractiveSession(config=tf_config) sess.run(init) nn.build_arch() nn.restore_arch(sess) if nn is None or sess is None or trump is None: nn = None sess = None trump = None return False return True
def __init__(self, is_training=False): print( datetime.datetime.fromtimestamp(time.time()).strftime( '%Y-%m-%d %H:%M:%S') + ": Checking for data sets, downloading if needed...") data.check_all_unzip() print( datetime.datetime.fromtimestamp(time.time()).strftime( '%Y-%m-%d %H:%M:%S') + ": Initializing preproc...") self.__preproc = Preprocessor(evidence_length=EVIDENCE_LENGTH, hypothesis_length=HYPOTHESIS_LENGTH, vector_size=VECTOR_SIZE) print( datetime.datetime.fromtimestamp(time.time()).strftime( '%Y-%m-%d %H:%M:%S') + ": Processing GloVe Vector Data...") self.__preproc.setup_word_map(file=datasets.glove_vectors_840B_300d) self.__df_list = None self.__c_scores = None if is_training: print( datetime.datetime.fromtimestamp(time.time()).strftime( '%Y-%m-%d %H:%M:%S') + ": Updating data scores for training...") self.__df_list, self.__c_scores = self.__preproc.update_data_scores( file=datasets.snli_full_dataset_file) print( datetime.datetime.fromtimestamp(time.time()).strftime( '%Y-%m-%d %H:%M:%S') + ": Initializing LSTM...") self.__lstm = LSTM(e_length=self.__preproc.get_evidence_length(), h_length=self.__preproc.get_hypothesis_length(), v_size=self.__preproc.get_vector_size())
def stage1(options, trainData, validData, testData, n_layer, n_hidden): # Initialize Model model = LSTM() # Build Neural Network n_input = 102 n_hidden = n_hidden n_output = 1 # logistic regression layer model.add(hidden_layer(n_input, n_hidden)) #model.add(blstm_layer(n_input, n_hidden)) # BLSTM layer for i in range(n_layer): model.add(blstm_layer(n_hidden, n_hidden)) # linear regression layer model.add(bi_avec_activate(n_hidden, 1)) # Choose optimizer adadelta = ADADELTA() options["optimizer"] = adadelta # compile model.compile(options) # Training train_err, valid_err, test_err = model.fit(trainData, validData, testData) del model return train_err, valid_err, test_err
def __init__(self, input_dim, hidden_dim, output_dim, layers=1, bidirectional=False, layernorm=False): super().__init__() self.name = 'lstm' self.input_dim = input_dim self.hidden_dim = hidden_dim self.output_dim = output_dim self.layers = layers self.bidirectional = bidirectional self.layernorm = layernorm self.lstm = LSTM(input_dim=input_dim, hidden_dim=hidden_dim, layers=layers, bidirectional=bidirectional, layernorm=layernorm) if self.bidirectional: self.fc = nn.Linear(2 * hidden_dim, output_dim) else: self.fc = nn.Linear(hidden_dim, output_dim)
def courseratest(): np.random.seed(1) x_dim = 3 n_examples = 10 time_steps = 7 hidden_dim = 5 da = np.random.randn(5, 10, 4) x = np.ones((time_steps, n_examples, x_dim)) for i in range(time_steps): for j in range(n_examples): for k in range(x_dim): x[i, j, k] = np.random.randn() from functions import xavier_init net = LSTM(hidden_dim, x_dim) states, caches, preds, ys = net.forward( x, np.zeros((time_steps, n_examples, 1))) # print(states[-1]['z']) # print(states[-1]['c_out']) # print(states[-1]['f']) # print(states[-1]['u']) # print(states[-1]['o']) da_next = np.zeros_like(da[:, :, 0]) dc_next = np.zeros_like(states[0]['c']) grads = net.cell.init_grads() for t in reversed(range(4)): da_next, dc_next, grad_adds = net.cell.backward( states[t], da[:, :, t] + da_next, dc_next) for gate in ['c', 'u', 'o', 'f']: grads[gate]['w'] += grad_adds[gate]['w'] grads[gate]['b'] += grad_adds[gate]['b'] print(grad_adds['f']['b'])
def run_service(): stocks = StockHelper.get_stock_symbol_mapping() for stock, symbol in stocks.items(): logger.info(f"Starting training for {stock} [{symbol}] at {ctime()}") models = { "SVM": SVM(symbol, scaler=StandardScaler), "ARIMA": ARIMA(symbol, scaler=LogScaler), "LSTM": LSTM(symbol, scaler=MinMaxScaler, is_keras=True), } for model_name, model in models.items(): logger.info(f"\tTraining {model_name} for {stock}") start_time = time() train_data = model.train_data n_days = 300 test_data = Series(index=get_next_n_trading_days(n_days)) predictions = model.fit_predict(n_days) predictions = Series(data=predictions, index=test_data.index[: len(predictions)]) save_predictions(predictions, type(model).__name__, symbol, train_data.index.max().to_pydatetime()) logger.info(f"\tTrained {model_name} for {stock} in {time() - start_time:.3f} seconds") logger.info(f"Finished training for {stock} [{symbol}] at {ctime()}")
def __init__(self, input_transformer_params, record_model_params, num_classes=1, model_type='multilabel', pretrain_num_sampled=512, pretrain_freq_file=''): """Params: input_transformer_params: dict of params for InputTransformer. record_model_params: dict of params for the record-level LSTM. num_classes: number of classes to predict (1 for binary). model_type: 'multilabel' or 'multiclass'. Use 'multilabel' if binary. pretrain_num_sampled: number to sample for the pretraining loss. pretrain_freq_file: vocab frequency file to use for the sampling distribution during pretraining. """ self._input_transformer = InputTransformer(**input_transformer_params) self._record_model = LSTM(**record_model_params) self._num_classes = num_classes self._model_type = model_type self._pretrain_num_sampled = pretrain_num_sampled self._pretrain_freq_file = pretrain_freq_file self._notes_feature_name = input_transformer_params['notes_feature_name'] self._hierarchical = input_transformer_params['use_notes_model'] if self._hierarchical: self._notes_model_dim = input_transformer_params[ 'notes_model']['model_dim']) self._notes_bidirectional = input_transformer_params[ 'notes_model']['bidirectional'])
def build(self): print '\t building rnn cell...' if self.cell=='gru': hidden_layer=GRU(self.rng, self.n_input,self.n_hidden,self.n_batch, self.x,self.E,self.x_mask, self.is_train,self.p) else: hidden_layer=LSTM(self.rng, self.n_input,self.n_hidden,self.n_batch, self.x,self.E,self.x_mask, self.is_train,self.p) print '\t building softmax output layer...' softmax_shape=(self.n_hidden,self.n_output) output_layer=H_Softmax(softmax_shape, hidden_layer.activation, self.y_node,self.y_choice,self.y_bit_mask,self.y_mask) self.params=[self.E,] self.params+=hidden_layer.params self.params+=output_layer.params cost=output_layer.activation lr=T.scalar("lr") gparams=[T.clip(T.grad(cost,p),-10,10) for p in self.params] updates=sgd(self.params,gparams,lr) self.train=theano.function(inputs=[self.x,self.x_mask,self.y_node,self.y_choice,self.y_bit_mask,self.y_mask,self.n_batch,lr], outputs=cost, updates=updates, givens={self.is_train:np.cast['int32'](1)}) self.test=theano.function(inputs=[self.x,self.x_mask,self.y_node,self.y_choice,self.y_bit_mask,self.y_mask,self.n_batch], outputs=cost, givens={self.is_train:np.cast['int32'](0)}) '''
def test(option, trainData, validData, testData, n_layer, n_hidden): # Initialize Model model = LSTM() # Build Neural Network n_input = 84 n_hidden = 64 # n_hidden = int(n_input*n_hidden) n_output = 1 model.add(lstm_layer(84, 200)) model.add(lstm_layer(200, 160)) # model.add(lstm_layer()) # for i in range(n_layer): # model.add(lstm_layer(n_hidden, n_hidden)) # model.add(DropOut()) model.add(avec_activate(160, n_output)) # model.add(DropOut()) # Choose optimizer adadelta = ADADELTA() options["optimizer"] = adadelta # model.compile(options) # Training train_err, valid_err, test_err = model.fit(trainData, validData, testData) del model return train_err, valid_err, test_err
def compareFixed(): t = Tasks() x_test, y_test = t.sequence_type_1(100) add_params, mul_params = torch.load('program_memory/add.pt'), torch.load( 'program_memory/mul.pt') hnm = HNM(10, 20, add_params, mul_params) hnm.load_state_dict(torch.load("learned_params/hnm_arch_2.pt")) ntm = NTM(10, 20) ntm.load_state_dict(torch.load("learned_params/ntm.pt")) lstm = LSTM(14, 256, 325, 1) lstm.load_state_dict(torch.load("learned_params/lstm.pt")) hnm_diff, lstm_diff, ntm_diff = 0, 0, 0 for i in range(len(x_test)): hnm_out = hnm.recurrent_forward(x_test[i:i + 1]) ntm_out = ntm.recurrent_forward(x_test[i:i + 1]) lstm_out = lstm.recurrent_forward(x_test[i:i + 1]) answer = np.argmax(y_test[i:i + 1].detach().numpy()) hnm_diff += abs(answer - np.argmax(hnm_out.detach().numpy())) ntm_diff += abs(answer - np.argmax(ntm_out.detach().numpy())) lstm_diff += abs(answer - np.argmax(lstm_out.detach().numpy())) print(hnm_diff / len(y_test), ntm_diff / len(y_test), lstm_diff / len(y_test))
def load_model(modelConfig, dataConfig, data): # Set number of cores for TensorFlow to use try: tf_config = tf.ConfigProto( inter_op_parallelism_threads=int(modelConfig['NumCores']), intra_op_parallelism_threads=int(modelConfig['NumCores'])) except KeyError: tf_config = tf.ConfigProto() raise UserWarning( "Number of cores to use not specified! Setting to TensorFlow default." ) with tf.Graph().as_default(), tf.Session(config=tf_config) as session: with tf.variable_scope('Model', reuse=None): m = LSTM(False, modelConfig, dataConfig) saver = tf.train.Saver() saver.restore(session, modelConfig['InputDirectory']) print 'Model successfuly restored!' data.prepBatches(m.batch_size, m.num_steps) ypred, _, _, _ = run_epoch(session, m, data, tf.no_op()) return ypred
def simplefunc(): time_steps = 10 x_dim = 8 hidden_dim = 8 output_dim = 8 n_examples = 2048 batch_size = 256 x = np.random.randn(time_steps, n_examples, x_dim) y = np.random.randn(time_steps, n_examples, output_dim) x = np.ones((time_steps, n_examples, x_dim)) y = np.ones((time_steps, n_examples, output_dim)) / 4.5 y[4:, :, :] = y[4:, :, :] * 3.6 net = LSTM(hidden_dim, x_dim, output_dim=output_dim, learning_rate=1e-5) losses = [] for i in range(5000): start = time.time() loss = 0 for data, targets in minibatch_gen(x, y, batch_size): loss += np.mean(net.fit(data, targets)) losses.append(loss) print('Epoch {}: loss: {} time: {}'.format(i, loss, time.time() - start), end='\r', flush=True) print('\nEpoch {}: loss: {} time: {}'.format(i, loss, time.time() - start), end='\r', flush=True)
def pipeline(output_window): ''' Apply training of lstm and portfolio optimization :param output_window: time windows length of LSTM predictions :return: ''' data_autoen = get_data(scale=True) train_data, test_data, train_labels, test_labels = get_train_test(data_autoen, windows_size=net_config.windows_size, output_window=output_window) #data = get_data(windows_size=net_config.windows_size) batches = create_batches(train_data, batch_size=net_config.batch_size) labels_batches = create_batches(train_labels, batch_size=net_config.batch_size) with tf.Graph().as_default(): lstm_model = LSTM(input_size=data_autoen.shape[-1], output_size=data_autoen.shape[-1], rnn_hidden=net_config.rnn_hidden, window_size=net_config.windows_size, window_output=output_window, learning_rate=net_config.learning_rate, ) with tf.Session(config=config_proto) as sess: if net_config.lstm_from_file: lstm_model.saver.restore(sess, tf.train.latest_checkpoint('lstm-price/')) else: LSTM.train_lstm(sess, lstm_model, batches, labels_batches, test_data, test_labels, net_config.lstm_epochs, net_config.dropout_prob) lstm_model.saver.save(sess, "lstm-price/model", global_step=net_config.lstm_epochs) ''' feed_dict = { lstm_model.inputs: test_data[0:1, :, :], lstm_model.dropout_prob: 1.0 } preds = sess.run(lstm_model.predicted_outputs, feed_dict=feed_dict) n_stocks = data_autoen.shape[-1] fig, axes = plt.subplots(int(n_stocks / 2), 2) i = 0 for n in range(int(n_stocks / 2)): for j in range(2): axes[n, j].plot(preds[0, :, i], label='predicted') axes[n, j].plot(test_labels[0, :, i], label='true') i += 1 plt.legend() plt.show() ''' df_close = pd.DataFrame(test_data[:, -1, :]) df_close = pd.DataFrame(scaler.inverse_transform(df_close)) df_close = df_close.pct_change().dropna() train_dataset = concat(train_data, train_labels) all_weights, pnls = backtesting_optim_portfolio( df_close, test_data, test_labels, train_dataset, lstm_model, sess, net_config.risk_aversion, output_window, net_config) print(all_weights.shape) performances = compute_performance(pnls) print(performances) return np.array(performances), all_weights, pnls