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()
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)
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)
def main(): go_board_rows, go_board_cols = 19, 19 num_classes = go_board_rows * go_board_cols num_games = 100 encoder = OnePlaneEncoder((go_board_rows, go_board_cols)) # <1> processor = GoDataProcessor(encoder=encoder.name()) # <2> generator = processor.load_go_data('train', num_games, use_generator=True) # <3> test_generator = processor.load_go_data('test', num_games, use_generator=True) # <1> First we create an encoder of board size. # <2> Then we initialize a Go Data processor with it. # <3> From the processor we create two data generators, for training and testing. # end::train_generator_generator[] # tag::train_generator_model[] input_shape = (encoder.num_planes, go_board_rows, go_board_cols) network_layers = small.layers(input_shape) model = Sequential() for layer in network_layers: model.add(layer) model.add(Dense(num_classes, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) # end::train_generator_model[] # tag::train_generator_fit[] epochs = 5 batch_size = 128 model.fit_generator(generator=generator.generate(batch_size, num_classes), # <1> epochs=epochs, steps_per_epoch=generator.get_num_samples() / batch_size, # <2> validation_data=test_generator.generate(batch_size, num_classes), # <3> validation_steps=test_generator.get_num_samples() / batch_size, # <4> callbacks=[ModelCheckpoint('./checkpoints/small_model_epoch_{epoch}.h5')]) # <5> try: with tf.device('/device:GPU:0'): model.evaluate_generator(generator=test_generator.generate(batch_size, num_classes), steps=test_generator.get_num_samples() / batch_size) # <6> except RuntimeError as e: print(e)
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')) model.compile(loss='categorical_crossentropy', optimizer='adagrad',
from keras.models import Sequential from keras.layers import Dense from dlgo.agent.predict import DeepLearningAgent, load_prediction_agent from dlgo.data.parallel_processor import GoDataProcessor from dlgo.encoders.sevenplane import SevenPlaneEncoder from dlgo.httpfrontend import get_web_app from dlgo.networks import large # end::e2e_imports[] # 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'])
def my_first_network( cont_train=True, num_games=100, epochs=10, batch_size=128, optimizer='adadelta', patience=5, where_save_model='../checkpoints/large_model_epoch_{epoch:3d}_{val_loss:.3f}_{val_accuracy:.3f}.h5', where_save_bot='../checkpoints/deep_bot.h5', pr_kgs='n', seed=1337): go_board_rows, go_board_cols = 19, 19 num_classes = go_board_rows * go_board_cols encoder = MySevenPlaneEncoder_S((go_board_rows, go_board_cols)) processor = GoDataProcessor(encoder=encoder.name(), data_directory='data') if pr_kgs == 'y': # Only forming train and test data into data directory generator = processor.load_go_data('train', num_games, use_generator=True, seed=seed) test_generator = processor.load_go_data('test', num_games, use_generator=True, seed=seed) return else: generator = processor.load_go_data('train', num_games, use_generator=True, seed=0) test_generator = processor.load_go_data('test', num_games, use_generator=True, seed=0) input_shape = (encoder.num_planes, go_board_rows, go_board_cols) network_layers = my_network.layers(input_shape) train_log = 'training_' + str(num_games) + '_epochs_' + str( epochs) + '_' + optimizer + '.log' csv_logger = CSVLogger(train_log, append=True, separator=';') if patience > 2: r_patience = patience - 1 else: r_patience = patience Reduce = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=r_patience, verbose=1, mode='auto', min_delta=0.0005, cooldown=0, min_lr=0) tensor_board_log_dir = '/home/nail/CODE_GO/checkpoints/my_log_dir' tensorboard = TensorBoard(log_dir=tensor_board_log_dir, histogram_freq=1, embeddings_freq=1, write_graph=True) if optimizer == 'adagrad': callback_list = [ ModelCheckpoint(where_save_model, save_best_only=True), EarlyStopping(monitor='val_accuracy', mode='auto', verbose=verb, patience=patience, min_delta=0, restore_best_weights=True), csv_logger, Reduce, tensorboard ] else: callback_list = [ ModelCheckpoint(where_save_model, save_best_only=True), EarlyStopping(monitor='val_accuracy', mode='auto', verbose=verb, patience=patience, min_delta=0, restore_best_weights=True), csv_logger, Reduce, tensorboard ] if cont_train is False: # Обучение с самого начала с случайных весов model = Sequential() for layer in network_layers: model.add(layer) model.add(Dense(num_classes, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) #model.summary() history = model.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, verbose=verb, callbacks=callback_list) if cont_train is True: # Обучение используя уже предобученную модель, продолжение обучения. model = load_model('../checkpoints/model_continue.h5') model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) history = model.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, verbose=verb, callbacks=callback_list) score = model.evaluate_generator( generator=test_generator.generate(batch_size, num_classes), steps=test_generator.get_num_samples() / batch_size) bot_save(model, encoder, where_save_bot) # Отрисовка графика результата print("Test score: ", score[0]) print("Test accuracy: ", score[1]) plt.subplot(211) plt.title("Accuracy") plt.plot(history.history["accuracy"], color="g", label="Train") plt.plot(history.history["val_accuracy"], color="b", label="Validation") plt.legend(loc="best") plt.subplot(212) plt.title("Loss") plt.plot(history.history["loss"], color="g", label="Train") plt.plot(history.history["val_loss"], color="b", label="Validation") plt.legend(loc="best") plt.tight_layout() plt.show()
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)
from dlgo.data.parallel_processor import GoDataProcessor processor = GoDataProcessor() generator = processor.load_go_data('train', 100, use_generator=True) print(generator.get_num_samples()) generator = generator.generate(batch_size=10) X, y = next(generator) print(X) print(y)
#data_dir = '//media//nail//SSD_Disk//kgs_data' encoder = SimpleEncoder((19, 19)) lst_files = os.listdir(data_dir) lst_npy = [] for entry in lst_files: if fnmatch.fnmatch(entry, '*train*npy'): lst_npy.append(entry) cnt_files_begin = len(lst_npy) print('--------------------------- Count files npy Before = ', cnt_files_begin) #os.chdir(data_dir) processor = GoDataProcessor(encoder=encoder.name(), data_directory=data_dir) generator = processor.load_go_data('train', num_games, use_generator=True, seed=seed) test_generator = processor.load_go_data('test', num_games, use_generator=True, seed=seed) lst_files = os.listdir(data_dir) lst_npy = [] for entry in lst_files: if fnmatch.fnmatch(entry, '*train*npy'): lst_npy.append(entry)
def my_first_network( cont_train=True, num_games=100, num_samples=None, num_samples_test=None, epochs=10, batch_size=128, percent_validation=10, optimizer='adadelta', learning_rate=0.1, patience=5, where_save_model='../checkpoints/small_model_epoch_{epoch:3d}_{val_loss:.3f}_{val_accuracy:.3f}.h5', where_save_bot='../checkpoints/small_deep_bot.h5', pr_kgs='n', seed=1337, name_model='my_small', num_layers=12, first_filter=192, num_filters=64, first_kernel_size=5, other_kernel_size=3): go_board_rows, go_board_cols = 19, 19 num_classes = go_board_rows * go_board_cols model = Sequential() encoder = AlphaGoEncoder((go_board_rows, go_board_cols), use_player_plane=True) processor = GoDataProcessor(encoder=encoder.name(), data_directory='data') if pr_kgs == 'y': # Only forming train and test data into data directory generator = processor.load_go_data('train', num_games, use_generator=True, seed=seed) test_generator = processor.load_go_data('test', num_games, use_generator=True, seed=seed) return else: generator = processor.load_go_data('train', num_games, use_generator=True, seed=0) test_generator = processor.load_go_data('test', num_games, use_generator=True, seed=0) input_shape = (encoder.num_planes, go_board_rows, go_board_cols) if network_alphago != 'y': network_layers = large.layers(input_shape) train_log = 'training_' + name_model + '_' + str( num_games) + '_epochs_' + str(epochs) + '_' + optimizer + '.csv' csv_logger = CSVLogger(train_log, append=True, separator=';') lrate = LearningRateScheduler(step_decay) if patience > 2: r_patience = patience - 1 else: r_patience = patience Reduce = ReduceLROnPlateau(monitor='val_accuracy', factor=0.1, patience=r_patience, verbose=1, mode='auto', min_delta=0.00001, cooldown=0, min_lr=0) # tensor_board_log_dir = '/home/nail/CODE_GO/checkpoints/my_log_dir' # tensorboard = TensorBoard(log_dir=tensor_board_log_dir, histogram_freq=1, embeddings_freq=1, write_graph=True) if optimizer == 'adagrad': callback_list = [ ModelCheckpoint(where_save_model, monitor='val_accuracy', save_best_only=True), EarlyStopping(monitor='val_accuracy', mode='auto', verbose=verb, patience=patience, min_delta=0, restore_best_weights=True), csv_logger, Reduce ] elif optimizer == 'SGD': callback_list = [ ModelCheckpoint(where_save_model, monitor='val_accuracy', save_best_only=True), EarlyStopping(monitor='val_accuracy', mode='auto', verbose=verb, patience=patience, min_delta=0, restore_best_weights=True), csv_logger, Reduce, lrate ] else: callback_list = [ ModelCheckpoint(where_save_model, monitor='val_accuracy', save_best_only=True), EarlyStopping(monitor='val_accuracy', mode='auto', verbose=verb, patience=patience, min_delta=0, restore_best_weights=True), csv_logger, Reduce, ] if cont_train is False: # Обучение с самого начала с случайных весов if network_alphago != 'y': # Not AlphaGo network for layer in network_layers: model.add(layer) model.add(Dense(num_classes, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) model.summary() else: # AlphaGo network model = alphago_predict.alphago_model( input_shape=input_shape, first_filter=first_filter, num_filters=num_filters, num_layers=num_layers, first_kernel_size=first_kernel_size, other_kernel_size=other_kernel_size) model.add(Dense(num_classes, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) model.summary() # if num_samples == None: history = model.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, verbose=verb, callbacks=callback_list) # else: # step_per_ep_train = int(num_samples / batch_size) # valid_steps = int(percent_validation/100 * num_samples_test / batch_size) # 10% от всех тестовых данных # history = model.fit_generator( # generator=generator.generate(batch_size, num_classes), # epochs=epochs, # steps_per_epoch=step_per_ep_train, # validation_data=test_generator.generate( # batch_size, num_classes), # validation_steps=valid_steps, # verbose=verb, # callbacks=callback_list # ) if cont_train is True: # Обучение используя уже предобученную модель, продолжение обучения. model = load_model('../checkpoints/model_continue.h5') if optimizer == 'SGD': opt = SGD(learning_rate=learning_rate) if optimizer == 'adagrad': opt = Adagrad(learning_rate=learning_rate) if optimizer == 'adadelta': opt = Adadelta(learning_rate=learning_rate) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) # if num_samples == None: history = model.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, verbose=verb, callbacks=callback_list) # else: # step_per_ep_train = int(num_samples / batch_size) # valid_steps = int(percent_validation/100 * num_samples_test / batch_size) # 10% от всех тестовых данных # history = model.fit_generator( # generator=generator.generate(batch_size, num_classes), # epochs=epochs, # steps_per_epoch=step_per_ep_train, # validation_data=test_generator.generate( # batch_size, num_classes), # validation_steps=valid_steps, # verbose=verb, # callbacks=callback_list # ) # if num_samples_test == None: # score = model.evaluate_generator( # generator=test_generator.generate(batch_size, num_classes), # steps=test_generator.get_num_samples() / batch_size) # else: score = model.evaluate_generator(generator=test_generator.generate( batch_size, num_classes), steps=num_samples_test / batch_size) bot_save(model, encoder, where_save_bot) # Отрисовка графика результата print("Test score: ", score[0]) print("Test accuracy: ", score[1]) # В конце вывести архитектуру модели. model.summary() plt.subplot(211) plt.title("Accuracy") plt.plot(history.history["accuracy"], color="g", label="Train") plt.plot(history.history["val_accuracy"], color="b", label="Validation") plt.legend(loc="best") plt.subplot(212) plt.title("Loss") plt.plot(history.history["loss"], color="g", label="Train") plt.plot(history.history["val_loss"], color="b", label="Validation") plt.legend(loc="best") plt.tight_layout() plt.show()