def _prepare_training_data_single_process(worker_idx, chunk, corpus_index, output_q, stop_q): # Make sure ^C gets handled in the main process. _disable_keyboard_interrupt() processor = SevenPlaneProcessor() chunk = corpus_index.get_chunk(chunk) xs, ys = [], [] for board, next_color, next_move in chunk: if not stop_q.empty(): print("Got stop signal, aborting.") return feature, label = processor.feature_and_label(next_color, next_move, board, processor.num_planes) xs.append(feature) ys.append(label) X = np.array(xs) # one-hot encode the moves nb_classes = 19 * 19 Y = np.zeros((len(ys), nb_classes)) for i, y in enumerate(ys): Y[i][y] = 1 output_q.put((worker_idx, X, Y)) output_q.close()
def load_keras_bot(bot_name): model_file = 'model_zoo/' + bot_name + '_bot.yml' weight_file = 'model_zoo/' + bot_name + '_weights.hd5' with open(model_file, 'r') as f: yml = yaml.load(f) model = model_from_yaml(yaml.dump(yml)) # Note that in Keras 1.0 we have to recompile the model explicitly model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) model.load_weights(weight_file) processor = SevenPlaneProcessor() return KerasBot(model=model, processor=processor)
#!/usr/bin/env python from __future__ import print_function import argparse import yaml import os import webbrowser from keras.models import model_from_yaml from betago.model import HTTPFrontend, KerasBot, RandomizedKerasBot from betago.processor import SevenPlaneProcessor processor = SevenPlaneProcessor() bot_name = 'demo' model_file = 'model_zoo/' + bot_name + '_bot.yml' weight_file = 'model_zoo/' + bot_name + '_weights.hd5' with open(model_file, 'r') as f: yml = yaml.load(f) model = model_from_yaml(yaml.dump(yml)) # Note that in Keras 1.0 we have to recompile the model explicitly model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) model.load_weights(weight_file) parser = argparse.ArgumentParser() parser.add_argument('--host', default='localhost', help='host to listen to') parser.add_argument( '--port', '-p',
from keras.layers.convolutional import Convolution2D, MaxPooling2D from keras.utils import np_utils from betago.processor import SevenPlaneProcessor batch_size = 128 nb_epoch = 100 nb_classes = 19 * 19 # One class for each position on the board go_board_rows, go_board_cols = 19, 19 # input dimensions of go board nb_filters = 32 # number of convolutional filters to use nb_pool = 2 # size of pooling area for max pooling nb_conv = 3 # convolution kernel size # SevenPlaneProcessor loads seven planes (doh!) of 19*19 data points, so we need 7 input channels processor = SevenPlaneProcessor() input_channels = processor.num_planes # Load go data and one-hot encode labels X, y = processor.load_go_data(num_samples=1000) X = X.astype('float32') Y = np_utils.to_categorical(y, nb_classes) # Specify a keras model with two convolutional layers and two dense layers, # connecting the (num_samples, 7, 19, 19) input to the 19*19 output vector. model = Sequential() model.add(Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='valid', input_shape=(input_channels, go_board_rows, go_board_cols))) model.add(Activation('relu')) model.add(Convolution2D(nb_filters, nb_conv, nb_conv)) model.add(Activation('relu'))
here = os.path.dirname(os.path.abspath(__file__)) model_zoo = os.path.join(here, '..', 'model_zoo') weight_file = os.path.join(model_zoo, args.bot_name + '_weights.hd5') checkpoint_file_pattern = os.path.join(model_zoo, args.bot_name + '_epoch_{epoch}.hd5') model_file = os.path.join(model_zoo, args.bot_name + '_model.yml') batch_size = 128 nb_classes = 19 * 19 # One class for each position on the board go_board_rows, go_board_cols = 19, 19 # input dimensions of go board nb_filters = 32 # number of convolutional filters to use nb_pool = 2 # size of pooling area for max pooling nb_conv = 3 # convolution kernel size # SevenPlaneProcessor loads seven planes (doh!) of 19*19 data points, so we need 7 input channels processor = SevenPlaneProcessor(use_generator=True) input_channels = processor.num_planes # Load go data and one-hot encode labels data_generator = processor.load_go_data(num_samples=args.sample_size) print(data_generator.get_num_samples()) # Specify a keras model with two convolutional layers and two dense layers, # connecting the (num_samples, 7, 19, 19) input to the 19*19 output vector. model = Sequential() model.add(Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='valid', input_shape=(input_channels, go_board_rows, go_board_cols))) model.add(Activation('relu')) model.add(Convolution2D(nb_filters, nb_conv, nb_conv)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
from keras.layers.convolutional import Convolution2D, MaxPooling2D from keras.utils import np_utils from betago.processor import SevenPlaneProcessor batch_size = 128 nb_epoch = 100 nb_classes = 19 * 19 # One class for each position on the board go_board_rows, go_board_cols = 19, 19 # input dimensions of go board nb_filters = 32 # number of convolutional filters to use nb_pool = 2 # size of pooling area for max pooling nb_conv = 3 # convolution kernel size # SevenPlaneProcessor loads seven planes (doh!) of 19*19 data points, so we need 7 input channels processor = SevenPlaneProcessor(use_generator=True) input_channels = processor.num_planes # Load go data and one-hot encode labels data_generator = processor.load_go_data(num_samples=1000) print(data_generator.get_num_samples()) # Specify a keras model with two convolutional layers and two dense layers, # connecting the (num_samples, 7, 19, 19) input to the 19*19 output vector. model = Sequential() model.add(Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='valid', input_shape=(input_channels, go_board_rows, go_board_cols))) model.add(Activation('relu')) model.add(Convolution2D(nb_filters, nb_conv, nb_conv)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))