def predict_on_stocks(array: numpy.array, model_path: str, interval: str, stock_path: str): scaler = StandardScaler() open_data, close_data = init_data(array) open_data, close_data = normalize_data(open_data, close_data, scaler) (x_train, y_train, x_test, y_test) = split_data(open_data, close_data) (x_train, y_train) = shuffle_data(x_train, y_train) (model, checkpoint_callback) = create_model(model_path) model.fit(x_train, y_train, validation_data=(x_test, y_test), batch_size=64, epochs=EPOCHS, callbacks=[checkpoint_callback]) #test_model(model, x_test, y_test, scaler, interval) // uncomment this if you want to test the ai efficiency dump(scaler, f'{model_path}/std_scaler.bin', compress=True)
def train_network(sess, model, state_data, reward_data, trace_lengths, reward_targets, episode_ids): # load original dataset (needed for markov error computation) vb = pd.read_csv(DATA_FILE) converge_flag = False merge = tf.summary.merge_all() saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) start_iteration = 1 state_data_orig = state_data.copy() trace_lengths_orig = trace_lengths.copy() mse_errors = [] restore_model = True if restore_model: checkpoint = tf.train.get_checkpoint_state(SAVE_DIR) if checkpoint and checkpoint.model_checkpoint_path: start_iteration = int( (checkpoint.model_checkpoint_path.split("-"))[-1]) + 1 saver.restore(sess, checkpoint.model_checkpoint_path) print("Successfully loaded:", checkpoint.model_checkpoint_path) else: print("Could not find old network weights.") # initial Q values [rnn_outputs_prev, q_predictions_prev] = sess.run([model.outputs, model.read_out], feed_dict={ model.trace_lengths: trace_lengths_orig, model.rnn_input: state_data_orig }) pickle.dump(q_predictions_prev, open(SAVE_DIR + '/q_values_before.pkl', 'wb')) for iteration in range(start_iteration, 251): t = time.time() print('Iteration %d ...' % (iteration)) costs = [] sample_index = 0 state_data, reward_data, trace_lengths, episode_ids = shuffle_data( state_data, reward_data, trace_lengths, episode_ids) while sample_index < len(state_data): # prepare batch states0, states1, rewards, traces0, traces1, end_index = get_next_batch( state_data, reward_data, trace_lengths, sample_index, BATCH_SIZE) # compute NN prediction [rnn_outputs_t1, q_predictions_t1] = sess.run([model.outputs, model.read_out], feed_dict={ model.trace_lengths: traces1, model.rnn_input: states1 }) q_target = np.zeros([len(rewards), 1]) for i in range(len(rewards)): if rewards[i] != 0: # in this case, we're at the end of an episode q_target[i, 0] = rewards[i] else: q_target[i, 0] = rewards[i] + GAMMA * q_predictions_t1[i, 0] # update with gradient [diff, read_out, cost, summary_train, _] = sess.run( [ model.diff, model.read_out, model.cost, merge, model.train_step ], feed_dict={ model.y: q_target, model.trace_lengths: traces0, model.rnn_input: states0 }) sample_index = end_index + 1 costs.append(cost) #if sample_index >= 96794: # print(read_out) # end of iteration loop iteration_cost = np.mean(costs) print('... done. Time elapsed: %.2f seconds' % (time.time() - t)) print('Iteration cost: %.3f' % (iteration_cost)) [rnn_outputs, q_predictions] = sess.run( [model.outputs, model.read_out], feed_dict={ model.trace_lengths: trace_lengths_orig, model.rnn_input: state_data_orig }) # compute mean change in Q predictions avg_change = np.mean(np.abs(q_predictions - q_predictions_prev)) avg_Q = np.mean(q_predictions) print('Mean change from last iteration: %.5f' % (avg_change)) print('Mean Q value: %.5f' % (avg_Q)) q_predictions_prev = q_predictions # clip and compute MSE error q_predictions[q_predictions > 1] = 1 q_predictions[q_predictions < -1] = -1 mse_error = metrics.mean_squared_error(reward_targets, q_predictions) print('MSE error: %.5f' % (mse_error)) mse_errors.append(mse_error) # compute markov property error outcome_regex = '[\-\!\+]' outcome_cond0 = pd.Series( vb['ActionOutcome0']).str.match(outcome_regex) outcome_cond1 = pd.Series( vb['ActionOutcome1']).str.match(outcome_regex) cond0 = (vb['ActionType0'] == 'S') & (vb['ActionHome0'] == 1) & (outcome_cond0) cond1 = (vb['ActionType1'] == 'S') & (vb['ActionHome1'] == 1) & (outcome_cond1) markov_error = np.abs( np.mean(q_predictions[cond1]) - np.mean(q_predictions[cond0])) print('Markov error: %.5f' % (markov_error)) saver.save(sess, SAVE_DIR + '/nn-iter', global_step=iteration) pickle.dump( mse_errors, open(SAVE_DIR + ('/mse_errors%.8f.pkl' % LEARNING_RATE), 'wb')) # check for convergence if avg_change < THRESHOLD: converge_flag = True if avg_change < 10 * THRESHOLD: pickle.dump( q_predictions, open('%s/q_values_%03d.pkl' % (SAVE_DIR, iteration), 'wb')) if converge_flag: print('Convergence detected, exiting.') print('Saving Q values.') pickle.dump(q_predictions, open(SAVE_DIR + '/q_values.pkl', 'wb')) break print('Done.')
inputs=[src_inp, tgt_inp, tgt_op, src_lens, tgt_mask], outputs=cost, updates=updates) f_eval = theano.function( inputs=[src_inp, tgt_inp, src_lens], outputs=final_output, ) num_epochs = 100 logging.info('Training network ...') BEST_BLEU = 1.0 costs = [] for i in range(num_epochs): logging.info('Shuffling data ...') train_src, train_tgt = shuffle_data(train_src, train_tgt) for j in xrange(0, len(train_src), batch_size): batch_src, batch_tgt_inp, batch_tgt_op, batch_src_lens, batch_src_mask, batch_tgt_mask \ = prepare_batch( train_src[j: j + batch_size], train_tgt[j: j + batch_size], src_word2ind, tgt_word2ind ) entropy = f_train(batch_src, batch_tgt_inp, batch_tgt_op, batch_src_lens, batch_tgt_mask) costs.append(entropy) logging.info('Epoch : %d Minibatch : %d Loss : %.3f' % (i, j, entropy)) if j % 64000 == 0 and j != 0: dev_predictions = decode_dev() dev_bleu = get_bleu(dev_predictions, dev_tgt)
# Initializing the variables init = tf.global_variables_initializer() display_step = 1 SHUFFLE = True config = tf.ConfigProto(device_count={'cpu': 0}) with tf.Session(config=config) as sess: sess.run(init) # Training cycle x_trained, y_trained = x_train, y_train for epoch in range(epochs): total_batch = ceil(n_samples / batch_size) # Loop over all batches if SHUFFLE is True: x_trained, y_trained = shuffle_data(x_train, y_train, seed) tp = time.time() for i in range(total_batch): batch_x = x_trained[i * batch_size:(i + 1) * batch_size] batch_y = y_trained[i * batch_size:(i + 1) * batch_size] # Run optimization op (backprop) and cost op (to get loss value) _, c = sess.run([train_op, cost], feed_dict={ X: batch_x, Y: batch_y }) # Compute average loss # Display logs per epoch step if epoch % display_step == 0: y_trained = sess.run(pred, feed_dict={X: x_train}) y_validated = sess.run(pred, feed_dict={X: x_val})
# gradients optimizer = tf.train.MomentumOptimizer(learning_rate=args.lr, momentum=0.9) gvs = optimizer.compute_gradients(loss) capped_gvs = [(tf.clip_by_value(grad, -args.grad_clip, args.grad_clip), var) for grad, var in gvs] train_op = optimizer.apply_gradients(capped_gvs) # Initialize all the variables in the current session session.run(tf.global_variables_initializer()) # Begin training for epoch in range(const.MAX_EPOCHS): # Shuffle the data for every epoch train_data_shuffled = data_utils.shuffle_data(wnut.train_data) print("Epoch %d" % (epoch)) num_batches = 0 start_time = time.time() for batch_id in tqdm(range(n_train_batches)): run_options = tf.RunOptions( trace_level=tf.RunOptions.FULL_TRACE) # run_metadata = tf.RunMetadata() feed_dict = model.get_feed_dict(train_data_shuffled, batch_id, batch_size, word_input, char_input, orth_input, orth_char_input, seq_length_input, labels)
def run(): # Fetch MNIST data data, target = data_utils.load_data_mnist() data = data.reshape(data.shape[0], 1, 28, 28) split = 60000 # Split data into train and test data X_train = data[:split] y_train = target[:split] n_classes = np.unique(y_train).size X_test = data[split:] y_test = target[split:] # Shuffle the training set X_train, y_train = data_utils.shuffle_data(X_train, y_train) # Reduce the size of the training set (reducing training time) X_train, y_train = X_train[:5000], y_train[:5000] # Setup convolutional neural network nn = nnet.NeuralNetwork( layers=[ nnet.Conv( n_feats=12, filter_shape=(5, 5), strides=(1, 1), weight_scale=0.1, weight_decay=0.001, ), nnet.Activation('relu'), nnet.Pool( pool_shape=(2, 2), strides=(2, 2), mode='max', ), nnet.Conv( n_feats=16, filter_shape=(5, 5), strides=(1, 1), weight_scale=0.1, weight_decay=0.001, ), nnet.Activation('relu'), nnet.Flatten(), nnet.Linear( n_out=n_classes, weight_scale=0.1, weight_decay=0.02, ), nnet.LogRegression(), ], ) # Train neural network t0 = time.time() nn.fit(X_train, y_train, X_test, y_test, learning_rate=0.01, max_iter=20, batch_size=100, test_iter=5) t1 = time.time() print('Training Duration: %.1fs' % (t1-t0)) # Evaluate on test data error = nn.error(X_test, y_test) print('Test error rate: %.4f' % error) # Plot loss curve nn.call_plot(blockFig=True, save_folder = 'cnn_mnist') # Classify 10 random images from the test set idx = np.random.permutation(X_test.shape[0])[:10] for i in idx: print nn.predict(X_test[i].reshape(1,1,28,28)) plt.figure(); plt.imshow(X_test[i].reshape(28,28)); plt.show()
def get_mini_batches(X_tr, y_tr, mb_num): X_tr, y_tr = shuffle_data(X_tr, y_tr) mbs = np.array_split(X_tr, mb_num, axis=1) mbs_labels = np.array_split(y_tr, mb_num, axis=0) return mbs, mbs_labels