예제 #1
0
def show_exemplar(model, x, x_as_onehot, y):
    """Picks a random example from the data."""
    i = np.random.randint(0, x.shape[0])
    example = x[i, :]
    example_as_onehot = x_as_onehot[i]
    score = y[i]
    example_as_single_example = np.reshape(example_as_onehot,
                                           (1, WIDTH * HEIGHT, MAX_TILE))
    prediction = model.predict(example_as_single_example)

    board = Board.from_vector(example)
    board.pretty_print()
    print("Actual score: %d; predicted score: %s" % (score, prediction))
예제 #2
0
    def add_n_examples(self, strategy, rnd, n,
                       starting_positions_dataset=None):
        """Runs games and adds them to the dataset until at least @p n
        examples have been added.  Returns the number of examples added.

        If @p starting_positions_dataset is set, games will be started from
        a randomly selected position from that dataset rather than from a
        blank board."""
        print("Adding", n, "examples to dataset.")
        added = 0
        while added < n:
            starting_game = None
            if starting_positions_dataset:
                random_position = starting_positions_dataset.nth_example(
                    rnd.randint(0,
                                starting_positions_dataset.num_examples() - 1))
                starting_game = Game(Board.from_vector(random_position))
                if not starting_game.board().can_move():
                    continue
            num_added = self.add_game(strategy, rnd, starting_game)
            if (added // 10000) != ((num_added + added) // 10000):
                print("Added %d so far..." % (num_added + added))
            added += num_added
        return added
예제 #3
0
 def test_encoding(self):
     board = self.realistic_board
     encoding = board.as_vector()
     self.assertEqual(encoding.size, Board.vector_width())
     decoding = Board.from_vector(encoding)
     self.assertEqual(board, decoding)