def multRun():
    totalCorrect = 0
    totalRan = 0
    numIterations = int(input("How many iterations? "))
    myNetwork = nn()
    myNetwork.setNetworkSize()

    for i in range(numIterations):
        print("Iteration " + str(i))
        myNetwork.initMatrix()
        myNetwork.runNetwork()
        totalCorrect += myNetwork.totalRight
        totalRan += myNetwork.totalTests

    print("Total Simulations: " + str(numIterations))
    print("Total Tests Ran: " + str(totalRan))
    print("Total Correct: " + str(totalCorrect))
    print("Average Correct: " + str(totalCorrect / numIterations))
    print("Average Error: " + str(1 - (totalCorrect / totalRan)))
Exemple #2
0
def main(board_size=11,
         tree_file=None,
         pretrained_model=None,
         game_file_saved_dict="game_record"):
    if not os.path.exists(game_file_saved_dict):
        os.mkdir(game_file_saved_dict)
    if pretrained_model:
        Net = torch.load(pretrained_model)
    else:
        Net = nn(input_layers=1, board_size=11)
    stack = utils.random_stack()
    if tree_file:
        tree = utils.read_file(tree_file)
    else:
        tree = MCTS(board_size=board_size, neural_network=Net)

    for game_time in range(100):
        game_record = tree.game()
        if len(game_record) % 2 == 1:
            print(
                "game {} completed, black win, this game length is {}".format(
                    game_time, len(game_record)))
        else:
            print("game {} completed, win win, this game length is {}".format(
                game_time, len(game_record)))
        utils.write_file(
            game_record, game_file_saved_dict + "/" +
            time.strftime("%Y%m%d-%H-%M-%S", time.localtime()))
        train_data = utils.generate_training_data(game_record=game_record,
                                                  board_size=11)
        for i in range(len(train_data)):
            stack.push(train_data[i])
        my_loader = utils.generate_data_loader(stack)
        Net.train(my_loader, game_time)
    torch.save(
        Net,
        "model_" + time.strftime("%Y%m%d-%H-%M", time.localtime()) + ".pkl")
Exemple #3
0
def main(tree_file=None,
         pretrained_model=None,
         game_file_saved_dict="game_record_2"):
    if not os.path.exists(game_file_saved_dict):
        os.mkdir(game_file_saved_dict)
    if pretrained_model:
        Net = torch.load(pretrained_model)
    else:
        Net = nn(input_layers=3,
                 board_size=utils.board_size,
                 learning_rate=utils.learning_rate)
    stack = utils.random_stack()
    if tree_file:
        tree = utils.read_file(tree_file)
    else:
        tree = MCTS(board_size=utils.board_size, neural_network=Net)
    Net.adjust_lr(1e-3)
    record = []
    game_time = 3600
    while True:
        game_record, eval, steps = tree.game()
        if len(game_record) % 2 == 1:
            print(
                "game {} completed, black win, this game length is {}".format(
                    game_time, len(game_record)))
        else:
            print(
                "game {} completed, white win, this game length is {}".format(
                    game_time, len(game_record)))
        print("The average eval:{}, the average steps:{}".format(eval, steps))
        utils.write_file(
            game_record, game_file_saved_dict + "/" +
            time.strftime("%Y%m%d_%H_%M_%S", time.localtime()) +
            '_game_time:{}.pkl'.format(game_time))
        train_data = utils.generate_training_data(game_record=game_record,
                                                  board_size=utils.board_size)
        for i in range(len(train_data)):
            stack.push(train_data[i])
        my_loader = utils.generate_data_loader(stack)
        utils.write_file(my_loader, "debug_loader.pkl")
        if game_time % 100 == 0:
            for _ in range(5):
                record.extend(Net.train(my_loader, game_time))
        print("train finished")
        print(" ")
        if game_time % 200 == 0:
            torch.save(Net, "model_{}.pkl".format(game_time))
            test_game_record, _, _ = tree.game(train=False)
            utils.write_file(
                test_game_record,
                game_file_saved_dict + "/" + 'test_{}.pkl'.format(game_time))
            print("We finished a test game at {} game time".format(game_time))
        if game_time % 200 == 0:
            plt.figure()
            plt.plot(record)
            plt.title("cross entropy loss")
            plt.xlabel("step passed")
            plt.ylabel("Loss")
            plt.savefig("loss record_{}.svg".format(game_time))
            plt.close()

        game_time += 1
def singleRun():
    myNetwork = nn()
    myNetwork.setNetworkSize()
    print(myNetwork)
    myNetwork.initMatrix()
    myNetwork.runNetwork()
Exemple #5
0
    # first get/load mnist
    #try:
    #   print 'Loading mnist'
    #   f = gzip.open('mnist.pkl.gz', 'rb')
    #   train_set, val_set, test_set = pickle.load(f)
    #except: download()

    # dummy data
    #data = np.asarray([[0,1], [1,0], [1,1], [0,0]])
    #labels = np.asarray([[1], [1], [0], [0]])

    X, y = sklearn.datasets.make_moons(200, noise=0.20)
    print('input shape:', X.shape)  # (200, 2) - 200 samples with 2 dims each
    #plt.scatter(X[:,0], X[:,1], s=40, c=y, cmap=plt.cm.Spectral)

    network = nn()

    # define the layers
    input_layer = Layer(2, input_=True, name='input_layer')
    hidden_layer1 = Layer(3, previous_layer=input_layer, name='hidden_layer1')
    output_layer = Layer(2,
                         previous_layer=hidden_layer1,
                         output_=True,
                         name='output_layer')

    # add layers to the network
    network.addLayer(input_layer)
    network.addLayer(hidden_layer1)
    network.addLayer(output_layer)

    network.train(X, y)