Esempio n. 1
0
    def estimate_price(self, stock_name: str, year: str, month: str,
                       day: str) -> float:
        """
        Estimates price of the stock sent as parameter. Based on the date which is sent as parameter, it takes the
        month from the date and filters all the stock_name stocks which have a date in the specified month and does
        the average of the mid range price (average between high and low price)
        :param stock_name: stock name for which we try to predict
        :param year:
        :param month:
        :param day:
        :return: returns the prediction
        """
        reader = Reader()
        self._dateset = reader.load_data("./dow_jones_index.csv")
        self._dateset = reader.clean_data(self._dateset)

        estimator = Estimator()
        estimator.fit(self._dateset)
        stock_prediction = estimator.predict(stock_name, year, month, day)
        return stock_prediction
with tf.Session() as sess:
    random_action_probability = random_action_probability_start
    sess.run(tf.global_variables_initializer())

    for i_episode in range(num_episodes):
        state = env.reset()

        for t in range(500):
            env.render()

            action = None
            if np.random.rand(1) < random_action_probability:
                action = env.action_space.sample()
            else:
                if global_step % 2 == 0:
                    action = estimator_1.predict(sess, [state])[0]
                else:
                    action = estimator_2.predict(sess, [state])[0]

            if random_action_probability > random_action_probability_end:
                random_action_probability *= random_action_probability_decay

            next_state, reward, done, _ = env.step(action)

            replay_memory.add(state, action, reward, next_state, done)

            batch_s, batch_a, batch_r, batch_s1, batch_d = replay_memory.get_samples(
                batch_size)
            if batch_s.shape[0] == batch_size:
                if global_step % 2 == 0:
                    estimator_1.update(sess, estimator_2, batch_s, batch_a,
Esempio n. 3
0
class Player:
    def __init__(self, step_size=0.1, epsilon=0.1, symbol=0):
        self.step_size = step_size
        self.epsilon = epsilon
        self.previous_state = State()
        self.state = None
        self.symbol = symbol
        self.td_errors = []

        self.estimator = Estimator()
        self.policy = make_epsilon_greedy_policy(self.estimator)
        self.action = (0, 0)

        self.actions = []
        for i in range(BOARD_ROWS):
            for j in range(BOARD_COLS):
                self.actions.append((i, j))

    # Adiciona informação do novo estado
    def set_state(self, state):
        if self.state != None:
            self.previous_state.data = np.copy(self.state.data)
        self.state = state

    def set_symbol(self, symbol):
        self.symbol = symbol

    def set_epsilon(self, epsilon):
        self.epsilon = epsilon

    # Faz o update da estimação
    def backup(self, next_state, other=False):
        is_end = next_state.is_end()
        reward = 0
        if is_end:
            if next_state.winner == self.symbol:
                reward = 1
            elif next_state.winner == -self.symbol:
                reward = -1
            else:
                reward = 0

        if other:
            next_state.data = np.copy(self.state.data)
            self.state = self.previous_state

        # Update do TD
        q_values_next = self.estimator.predict(next_state)

        # Q-value para o TD Target
        if is_end:
            td_target = reward
        else:
            gamma = 1
            td_target = reward + gamma * np.max(q_values_next)

        # Cálculo do TD error
        td = self.estimator.predict(self.state, self.action)
        td_error = np.abs(td_target - td)
        self.td_errors.append(td_error)

        # Atualiza o aproximador usando o td_target
        self.estimator.update(self.state, self.action, td_target)

    # Escolhe uma ação baseada no estado
    def act(self):
        action_probs = self.policy(self.state, self.epsilon)
        action_idx = np.random.choice(np.arange(len(self.actions)),
                                      p=action_probs)
        self.action = self.actions[action_idx]

        next_state = self.state.next_state(self.action[0], self.action[1],
                                           self.symbol)
        is_end = next_state.is_end()

        self.backup(next_state)

        return next_state, is_end

    def save_policy(self, epoch):
        with open(
                'app/saves/policy_%s_%d.bin' %
            (('first' if self.symbol == 1 else 'second'), epoch), 'wb') as f:
            pickle.dump(self.estimator, f)

        path = 'app/saves/metrics_%s.csv' % ('first'
                                             if self.symbol == 1 else 'second')
        metrics_file = open(path, "a")
        with metrics_file:
            writer = csv.writer(metrics_file)
            for td_error in self.td_errors:
                writer.writerow([td_error])

        self.td_errors.clear()

    def load_policy(self, epoch):
        with open(
                'app/saves/policy_%s_%d.bin' %
            (('first' if self.symbol == 1 else 'second'), epoch), 'rb') as f:
            self.estimator = pickle.load(f)
            self.policy = make_epsilon_greedy_policy(self.estimator)
    sess.run(tf.global_variables_initializer())

    for i_episode in range(num_episodes):
        state = env.reset()

        if i_episode % update_target_estimator_every_n_episodes == 0:
            target_estimator.copy_model_from(sess, q_estimator)

        for t in range(500):
            env.render()

            action = None
            if np.random.rand(1) < random_action_probability:
                action = env.action_space.sample()
            else:
                action = q_estimator.predict(sess, [state])[0]

            if random_action_probability > random_action_probability_end:
                random_action_probability *= random_action_probability_decay

            next_state, reward, done, _ = env.step(action)

            replay_memory.add(state, action, reward, next_state, done)

            batch_s, batch_a, batch_r, batch_s1, batch_d = replay_memory.get_samples(
                batch_size)
            if batch_s.shape[0] == batch_size:
                q_estimator.update(sess, target_estimator, batch_s, batch_a,
                                   batch_r, batch_s1, batch_d)

            if done:
def main():
    """Main program execution function"""
    args = parse_args()

    # Setup logging
    log_format = '%(asctime)s %(levelname)s %(message)s'
    logging.basicConfig(level=logging.INFO, format=log_format)
    logging.info('Initializing')
    if args.show_config:
        logging.info('Command line config: %s' % args)

    # CUDA option
    set_cuda(args.cuda)

    # Load the data
    logging.info('Loading input graphs')
    filenames = [os.path.join(args.input_dir, 'event%06i.npz' % i)
                 for i in range(args.n_samples)]
    graphs = load_graphs(filenames, SparseGraph)

    # We round by batch_size to avoid partial batches
    logging.info('Partitioning the data')
    n_test = int(args.n_samples * args.test_frac) // args.batch_size * args.batch_size
    n_valid = int(args.n_samples * args.valid_frac) // args.batch_size * args.batch_size
    n_train = (args.n_samples - n_valid - n_test) // args.batch_size * args.batch_size
    n_train_batches = n_train // args.batch_size
    n_valid_batches = n_valid // args.batch_size
    n_test_batches = n_test #// args.batch_size

    # Partition the dataset
    train_graphs, test_graphs = train_test_split(graphs, test_size=n_test)
    train_graphs, valid_graphs = train_test_split(train_graphs, test_size=n_valid)

    logging.info('Train set size: %i' % len(train_graphs))
    logging.info('Valid set size: %i' % len(valid_graphs))
    logging.info('Test set size:  %i' % len(test_graphs))

    # Prepare the batch generators
    train_batcher = batch_generator(train_graphs, n_samples=n_train,
                                    batch_size=args.batch_size)
    valid_batcher = batch_generator(valid_graphs, n_samples=n_valid,
                                    batch_size=args.batch_size, train=False)
    test_batcher = batch_generator(test_graphs, n_samples=n_test,
                                   batch_size=1, train=False)

    # Construct the model
    logging.info('Building the model')
    n_features = feature_scale.shape[0]
    model = SegmentClassifier(input_dim=n_features,
                              hidden_dim=args.hidden_dim,
                              n_iters=args.n_iters)
    loss_func = nn.BCELoss()
    estim = Estimator(model, loss_func=loss_func, cuda=args.cuda)

    # Train the model
    estim.fit_gen(train_batcher, n_batches=n_train_batches,
                  valid_generator=valid_batcher, n_valid_batches=n_valid_batches,
                  n_epochs=args.n_epochs, verbose=args.train_verbosity)

    # Evaluate on the test set
    logging.info('Evaluating the test set')
    test_outputs = estim.predict(test_batcher, n_test_batches, concat=False)
    test_preds = [torch_to_np(o) for o in test_outputs]

    # Flatten the predictions and labels
    flat_y = np.concatenate([g.y.flatten() for g in test_graphs])
    flat_pred = np.concatenate([p.flatten() for p in test_preds])

    # Print some general statistics for sanity checks
    logging.info('Mean output: %.4f, stddev %.4f' % (flat_pred.mean(), flat_pred.std()))
    logging.info('Mean label: %.4f, stddev %.4f' % (flat_y.mean(), flat_y.std()))

    # Print out some metrics from scikit-learn
    thresh = 0.5
    logging.info('Test set results with threshold of %g' % thresh)
    logging.info('Accuracy:  %.4f' % sklearn.metrics.accuracy_score(flat_y, flat_pred>thresh))
    logging.info('Precision: %.4f' % sklearn.metrics.precision_score(flat_y, flat_pred>thresh))
    logging.info('Recall:    %.4f' % sklearn.metrics.recall_score(flat_y, flat_pred>thresh))

    # Save outputs
    if args.output_dir is not None:
        logging.info('Writing outputs to %s' % args.output_dir)
        make_path = lambda s: os.path.join(args.output_dir, s)
        # Serialize the model
        torch.save(estim.model.state_dict(), make_path('model'))
        # Save the losses for plotting
        np.savez(os.path.join(args.output_dir, 'losses'),
                 train_losses=estim.train_losses,
                 valid_losses=estim.valid_losses)

    # Optional interactive session
    if args.interactive:
        import IPython
        IPython.embed()

    logging.info('All done!')
Esempio n. 6
0
import config
from estimator import Estimator

e = Estimator()
preds = e.predict('Testset/test.txt')

if not config.TEST:
    out = open('result.txt', 'w')
    for pred in preds:
        if pred == 'positive':
            print(1)
            out.write('1\n')
        elif pred == 'notr':
            print(0)
            out.write('0\n')
        else:
            print(-1)
            out.write('-1\n')
else:
    labels = open('Testset/label.txt').read().split('\n')

    c = 0
    for pred, real in zip(preds, labels):
        if (pred == real):
            c += 1
    print("%.5lf" % (c / len(preds)))