Exemplo n.º 1
0
    def test_1_supervised_learning(self):
        print("TEST 1\n=====================================================")

        gpus = tf.config.experimental.list_physical_devices('GPU')
        if gpus:
            # Restrict TensorFlow to only use the first GPU
            try:
                tf.config.experimental.set_visible_devices(gpus[0], 'GPU')
                tf.config.experimental.set_memory_growth(gpus[0], True)
                tf.config.set_soft_device_placement(True)
            except RuntimeError as e:
                print(e)

        rows, cols = 19, 19
        encoder = AlphaGoEncoder()

        input_shape = (encoder.num_planes, rows, cols)
        alphago_sl_policy = alphago_model(input_shape, is_policy_net=True)

        alphago_sl_policy.compile('sgd',
                                  'categorical_crossentropy',
                                  metrics=['accuracy'])

        alphago_sl_agent = DeepLearningAgent(alphago_sl_policy, encoder)

        inputs = np.ones((10, ) + input_shape)
        outputs = alphago_sl_policy.predict(inputs)
        assert (outputs.shape == (10, 361))

        with h5py.File('test_alphago_sl_policy.h5', 'w') as sl_agent_out:
            alphago_sl_agent.serialize(sl_agent_out)
Exemplo n.º 2
0
def main():
	parser = argparse.ArgumentParser()
	parser.add_argument('--epochs', '-e', default=20, type=int,
		help="Training epochs. Default is 20")

	args = parser.parse_args()
	# tag::e2e_processor[]
	go_board_rows, go_board_cols = 19, 19
	nb_classes = go_board_rows * go_board_cols
	encoder = SevenPlaneEncoder((go_board_rows, go_board_cols))
	processor = GoDataProcessor(encoder=encoder.name())

	X, y = processor.load_go_data(num_samples=100)
	# end::e2e_processor[]

	# tag::e2e_model[]
	input_shape = (go_board_rows, go_board_cols, encoder.num_planes)
	model = Sequential()
	network_layers = drew_example.layers(input_shape)
	for layer in network_layers:
		model.add(layer)
	model.add(Dense(nb_classes, activation='softmax'))
	model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])

	model.fit(X, y, batch_size=128, epochs=args.epochs, verbose=1)
	# end::e2e_model[]

	# tag::e2e_agent[]
	deep_learning_bot = DeepLearningAgent(model, encoder)
	fpath = os.path.join(CODE_ROOT_DIR, "agents/deep_bot.h5")
	model_file = h5py.File(fpath, "w")
	deep_learning_bot.serialize(model_file)
	model_file.close()
def main():
    # tag::e2e_processor[]
    go_board_rows, go_board_cols = 19, 19
    nb_classes = go_board_rows * go_board_cols
    encoder = SevenPlaneEncoder((go_board_rows, go_board_cols))
    processor = GoDataProcessor(encoder=encoder.name())

    X, y = processor.load_go_data(num_samples=100)
    # end::e2e_processor[]

    # tag::e2e_model[]
    input_shape = (encoder.num_planes, go_board_rows, go_board_cols)
    model = Sequential()
    network_layers = large.layers(input_shape)
    for layer in network_layers:
        model.add(layer)
    model.add(Dense(nb_classes, activation='softmax'))
    model.compile(loss='categorical_crossentropy',
                  optimizer='adadelta',
                  metrics=['accuracy'])

    model.fit(X, y, batch_size=128, epochs=20, verbose=1)
    # end::e2e_model[]

    # tag::e2e_agent[]
    deep_learning_bot = DeepLearningAgent(model, encoder)
    model_file = h5py.File("../agents/deep_bot.h5", "w")
    deep_learning_bot.serialize(model_file)
    model_file.close()
Exemplo n.º 4
0
def main():
    # sl data
    encoder = AlphaGoEncoder()
    processor = GoDataProcessor(encoder=encoder.name())

    # Paraller Processor
    generator = processor.load_go_data('train', NUM_GAMES, use_generator=True)
    test_generator = processor.load_go_data('test',
                                            NUM_GAMES,
                                            use_generator=True)

    # Data Processor
    # todo: does not have use_generator capability
    # generator = processor.load_go_data('train', NUM_GAMES)
    # test_generator = processor.load_go_data('test', NUM_GAMES)

    # sl model
    input_shape = (encoder.num_planes, ROWS, COLS)
    alphago_sl_policy = alphago_model(input_shape=input_shape,
                                      is_policy_net=True)

    # read earlier trained bot
    bot_filepath = 'alphago/alpha_sl_policy_e13_1k.h5'
    alphago_sl_policy.load_weights(bot_filepath)

    alphago_sl_policy.compile(optimizer='sgd',
                              loss='categorical_crossentropy',
                              metrics=['accuracy'])

    # sl train
    epochs = 200
    batch_size = 128
    alphago_sl_policy.fit_generator(
        generator=generator.generate(batch_size, NUM_CLASSES),
        epochs=epochs,
        steps_per_epoch=generator.get_num_samples() / batch_size,
        validation_data=test_generator.generate(batch_size, NUM_CLASSES),
        validation_steps=test_generator.get_num_samples() / batch_size,
        callbacks=[ModelCheckpoint('alphago_sl_policy_load_train_{epoch}.h5')])
    alphago_sl_agent = DeepLearningAgent(alphago_sl_policy, encoder)

    # save model
    with h5py.File('alphago_sl_policy_load_train.h5', 'w') as sl_agent_out:
        alphago_sl_agent.serialize(sl_agent_out)

    # evaluate
    alphago_sl_policy.evaluate_generator(
        generator=test_generator.generate(batch_size, NUM_CLASSES),
        steps=test_generator.get_num_samples() / batch_size)
Exemplo n.º 5
0
def main():
    samp = 1000
    epo = 1

    # tag::e2e_processor[]
    timestr = time.strftime("%Y%m%d-%H%M%S")
    model_h5filename = "./agents/deep_bot_" + timestr + "_s" + str(
        samp) + "e" + str(epo) + ".h5"

    go_board_rows, go_board_cols = 19, 19
    nb_classes = go_board_rows * go_board_cols
    encoder = XPlaneEncoder((go_board_rows, go_board_cols))
    data_dir = "data/" + str(encoder.num_planes) + "-planes"
    processor = GoDataProcessor(encoder=encoder.name(),
                                data_directory=data_dir)

    X, y = processor.load_go_data(num_samples=samp)
    # end::e2e_processor[]

    # tag::e2e_model[]
    input_shape = (encoder.num_planes, go_board_rows, go_board_cols)
    model = Sequential()
    network_layers = large.layers(input_shape)
    for layer in network_layers:
        model.add(layer)
    try:
        with tf.device('/device:GPU:0'):
            model.add(Dense(nb_classes, activation='softmax'))
            model.compile(loss='categorical_crossentropy',
                          optimizer='adadelta',
                          metrics=['accuracy'])

            model.fit(X, y, batch_size=128, epochs=epo, verbose=1)
            # end::e2e_model[]

            # tag::e2e_agent[]
            deep_learning_bot = DeepLearningAgent(model, encoder)
            deep_learning_bot.serialize(h5py.File(model_h5filename, "w"))
            # end::e2e_agent[]

            # tag::e2e_load_agent[]
            model_file = h5py.File(model_h5filename, "r")
            bot_from_file = load_prediction_agent(model_file)

            web_app = get_web_app({'predict': bot_from_file})
            web_app.run()
            # end::e2e_load_agent[]
    except RuntimeError as e:
        print(e)
Exemplo n.º 6
0
    def test_1_supervised_learning(self):
        rows, cols = 19, 19
        encoder = AlphaGoEncoder()

        input_shape = (encoder.num_planes, rows, cols)
        alphago_sl_policy = alphago_model(input_shape, is_policy_net=True)

        alphago_sl_policy.compile('sgd',
                                  'categorical_crossentropy',
                                  metrics=['accuracy'])

        alphago_sl_agent = DeepLearningAgent(alphago_sl_policy, encoder)

        inputs = np.ones((10, ) + input_shape)
        outputs = alphago_sl_policy.predict(inputs)
        assert (outputs.shape == (10, 361))

        with h5py.File('test_alphago_sl_policy.h5', 'w') as sl_agent_out:
            alphago_sl_agent.serialize(sl_agent_out)
Exemplo n.º 7
0
X, y = processor.load_go_data(num_samples=100)
# end::e2e_processor[]

# tag::e2e_model[]
input_shape = (encoder.num_planes, go_board_rows, go_board_cols)
model = Sequential()
network_layers = large.layers(input_shape)
for layer in network_layers:
    model.add(layer)
model.add(Dense(nb_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',
              optimizer='adadelta',
              metrics=['accuracy'])

model.fit(X, y, batch_size=128, epochs=20, verbose=1)
# end::e2e_model[]

# tag::e2e_agent[]
deep_learning_bot = DeepLearningAgent(model, encoder)
deep_learning_bot.serialize("../agents/deep_bot.h5")
# tag::e2e_agent[]

# tag::e2e_load_agent[]
model_file = h5py.File("../agents/deep_bot.h5", "r")
bot_from_file = load_prediction_agent(model_file)

web_app = get_web_app({'predict': bot_from_file})
web_app.run()
# end::e2e_load_agent[]
Exemplo n.º 8
0
from dlgo.networks import large

go_board_rows, go_board_cols = 19, 19
nb_classes = go_board_rows * go_board_cols
encoder = SevenPlaneEncoder((go_board_rows, go_board_cols))
processor = GoDataProcessor(encoder=encoder.name())

X, y = processor.load_go_data(num_samples=100)

input_shape = (encoder.num_planes, go_board_rows, go_board_cols)
model = Sequential()
network_layers = large.layers(input_shape)
for layer in network_layers:
    model.add(layer)
model.add(Dense(nb_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',
              optimizer='adadelta',
              metrics=['accuracy'])

model.fit(X, y, batch_size=128, epochs=20, verbose=1)

deep_learning_bot = DeepLearningAgent(model, encoder)
with h5py.File('./agents/deep_bot.h5', 'w') as outf:
    deep_learning_bot.serialize(outf)

model_file = h5py.File('./agents/deep_bot.h5', 'r')
bot_from_file = load_prediction_agent(model_file)

web_app = get_web_app({'predict': bot_from_file})
web_app.run()
Exemplo n.º 9
0
def bot_save(model, encoder, where_save_bot):
    # Сохранение для бота чтобы играть в браузере с ботом play_predict_19.html
    deep_learning_bot = DeepLearningAgent(model, encoder)
    model_file = h5py.File(where_save_bot, "w")
    deep_learning_bot.serialize(model_file)
Exemplo n.º 10
0
rows, cols = 19, 19
num_classes = rows * cols
num_games = 10000

encoder = AlphaGoEncoder()
processor = GoDataProcessor(encoder=encoder.name())
generator = processor.load_go_data('train', num_games, use_generator=True)
test_generator = processor.load_go_data('test', num_games, use_generator=True)

input_shape = (encoder.num_planes, rows, cols)
alphago_sl_policy = alphago_model(input_shape, is_policy_net=True)
alphago_sl_policy.compile('sgd',
                          'categorical_crossentropy',
                          metrics=['accuracy'])

epochs = 200
batch_size = 128
alphago_sl_policy = fit_generator(
    generator=generator.generate(batch_size, num_classes),
    epochs=epochs,
    steps_per_epoch=generator.get_num_samples() / batch_size,
    validation_data=test_generator.generate(batch_size, num_classes),
    validation_steps=test_generator.get_num_samples() / batch_size,
    callbacks=[ModelCheckpoint('alphago_sl_policy_{epoch}.h5')])

alphago_sl_agent = DeepLearningAgent(alphago_sl_policy, encoder)

with h5py.File('alphago_sl_policy.h5', 'w') as sl_agent_out:
    alphago_sl_agent.serialize(sl_agent_out)
Exemplo n.º 11
0
    input_shape = (encoder.num_planes, go_board_rows, go_board_cols)
    model = Sequential()
    network_layers = large.layers(input_shape)
    for layer in network_layers:
        model.add(layer)
    model.add(Dense(nb_classes, activation='softmax'))
    model.compile(loss='categorical_crossentropy',
                  optimizer=args.optimizer,
                  metrics=['accuracy'])

    model.fit(X, y, batch_size=args.batch_size, epochs=args.epochs, verbose=1)

    deep_learning_bot = DeepLearningAgent(model, encoder)
    try:
        deep_learning_bot.serialize(
            h5py.File("./agents/{}_bot.h5".format(args.bot_name)))
        print("Saving {}_bot.h5 to agents/".format(args.bot_name))
    except ValueError as e:
        print(e)
        new_name = input("Please enter a different name :  ")
        deep_learning_bot.serialize(
            h5py.File("./agents/{}_bot.h5".format(new_name)))
        print("Saving {}_bot.h5 to agents/".format(new_name))

    # model_file = h5py.File("./agents/deep_bot_3.h5", "r")
    # bot_from_file = load_prediction_agent(model_file)

    # web_app = get_web_app({'predict': bot_from_file})
    # web_app.run()
Exemplo n.º 12
0
model.add(Dropout(rate=0.6))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(rate=0.6))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(rate=0.6))
model.add(Dense(size * size, activation='softmax'))
model.summary()

model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])

model.fit(X_train, Y_train,
          batch_size=64,
          epochs=5,
          verbose=1,
          validation_data=(X_test, Y_test))
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])



encoder = SevenPlaneEncoder((19, 19))
dlAgent = DeepLearningAgent(model, encoder)
with h5py.File('cnn_agent.h5', 'w') as dlAgent_out:
	dlAgent.serialize(dlAgent_out)
# time.sleep(15)
Exemplo n.º 13
0
X, y = processor.load_go_data(num_samples=100)
# end::e2e_processor[]

# tag::e2e_model[]
input_shape = (encoder.num_planes, go_board_rows, go_board_cols)
model = Sequential()
network_layers = large.layers(input_shape)
for layer in network_layers:
    model.add(layer)
model.add(Dense(nb_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',
              optimizer='adadelta',
              metrics=['accuracy'])

model.fit(X, y, batch_size=128, epochs=20, verbose=1)
# end::e2e_model[]

# tag::e2e_agent[]
deep_learning_bot = DeepLearningAgent(model, encoder)
deep_learning_bot.serialize(h5py.File("../agents/deep_bot.h5", "w"))
# end::e2e_agent[]

# tag::e2e_load_agent[]
model_file = h5py.File("../agents/deep_bot.h5", "r")
bot_from_file = load_prediction_agent(model_file)

web_app = get_web_app({'predict': bot_from_file})
web_app.run()
# end::e2e_load_agent[]