Esempio n. 1
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()
Esempio n. 3
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)
Esempio n. 4
0
from dlgo.encoders.oneplane import OnePlaneEncoder
from dlgo.encoders.sevenplane import SevenPlaneEncoder

from dlgo.networks import large
from keras.models import Sequential
from keras.layers.core import Dense
from keras.callbacks import ModelCheckpoint

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

go_board_rows, go_board_cols = 19, 19
num_classes = go_board_rows * go_board_cols
num_games = 100

encoder = SevenPlaneEncoder((go_board_rows, go_board_cols))

processor = GoDataProcessor(encoder=encoder.name())

if __name__ == '__main__':
    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, go_board_rows, go_board_cols)
    network_layers = large.layers(input_shape)
    model = Sequential()
    for layer in network_layers:
        model.add(layer)
    model.add(Dense(num_classes, activation='softmax'))
def model03():
    mainmodel_start_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print('Start model03(): ' + mainmodel_start_time)

    optimizer = Adagrad()
    # optimizer = Adadelta()
    # optimizer = SGD(lr=0.01, momentum=0.9, decay=0.001)

    go_board_rows, go_board_cols = 19, 19
    num_classes = go_board_rows * go_board_cols
    num_games = 100

    one_plane_encoder = OnePlaneEncoder((go_board_rows, go_board_cols))
    seven_plane_encoder = SevenPlaneEncoder((go_board_rows, go_board_cols))
    simple_encoder = SimpleEncoder((go_board_rows, go_board_cols))

    encoder = seven_plane_encoder

    processor = GoDataProcessor(encoder=encoder.name())

    train_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, go_board_rows, go_board_cols)

    network_large = large
    network_small = small

    network = network_small
    network_layers = network.layers(input_shape)
    model = Sequential()
    for layer in network_layers:
        model.add(layer)

    model.add(Dense(num_classes, activation='relu'))

    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=['accuracy'])
    epochs = 5
    batch_size = 128
    model.fit_generator(
        generator=train_generator.generate(batch_size, num_classes),
        epochs=epochs,
        steps_per_epoch=train_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(
                filepath=
                'D:\\CODE\\Python\\Go\\code\\dlgo\\data\\checkpoints\\small_epoch_{epoch:02d}'
                '-acc-{accuracy:.4f}-val_acc_{'
                'val_accuracy:.4f}f.h5',
                monitor='accuracy')
        ])
    model.evaluate_generator(
        generator=test_generator.generate(batch_size, num_classes),
        steps=test_generator.get_num_samples() / batch_size)
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)
Esempio n. 7
0
from dlgo.data.parallel_processor import GoDataProcessor
from dlgo.encoders.alphago import AlphaGoEncoder
from dlgo.encoders.sevenplane import SevenPlaneEncoder
from dlgo.agent.predict import DeepLearningAgent
from dlgo.networks.alphago import alphago_model

from keras.callbacks import ModelCheckpoint
import h5py

rows, cols = 19, 19
num_classes = rows * cols
num_games = 2000

# encoder = AlphaGoEncoder()
encoder = SevenPlaneEncoder((rows, cols))
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,