Esempio n. 1
0
def main(args):

    trainning = GameArchive('../../data/train1.data')
    train_parser = ChunkParser(trainning.games)
    dataset = tf.data.Dataset.from_generator(train_parser.parse_chunk,
                                             output_types=(tf.string))
    dataset = dataset.shuffle(1 << 18)
    dataset = dataset.map(_parse_function)
    dataset = dataset.batch(BATCH_SIZE)
    dataset = dataset.prefetch(4)
    train_iterator = dataset.make_one_shot_iterator()

    testing = GameArchive('../../data/test1.data')
    test_parser = ChunkParser(testing.games)
    dataset = tf.data.Dataset.from_generator(test_parser.parse_chunk,
                                             output_types=(tf.string))
    dataset = dataset.map(_parse_function)
    dataset = dataset.batch(BATCH_SIZE)
    dataset = dataset.prefetch(4)
    test_iterator = dataset.make_one_shot_iterator()

    tfprocess = TFProcess()
    tfprocess.init(dataset, train_iterator, test_iterator)

    if args:
        restore_file = args.pop(0)
        tfprocess.restore(restore_file)
    while True:
        tfprocess.process(BATCH_SIZE)
Esempio n. 2
0
def main(args):
    train_data_prefix = args.pop(0)

    chunks = get_chunks(train_data_prefix)
    print("Found {0} chunks".format(len(chunks)))

    if not chunks:
        return

    parser = ChunkParser(chunks)

    dataset = tf.data.Dataset.from_generator(parser.parse_chunk,
                                             output_types=(tf.float32,
                                                           tf.float32,
                                                           tf.float32))
    dataset = dataset.shuffle(65536)
    dataset = dataset.batch(BATCH_SIZE)
    dataset = dataset.prefetch(16)
    iterator = dataset.make_one_shot_iterator()
    next_batch = iterator.get_next()

    tfprocess = TFProcess(next_batch)
    if args:
        restore_file = args.pop(0)
        tfprocess.restore(restore_file)
    while True:
        tfprocess.process(BATCH_SIZE)
Esempio n. 3
0
def main(cmd):
    cfg = yaml.safe_load(cmd.cfg.read())
    print(yaml.dump(cfg, default_flow_style=False))

    num_chunks = cfg['dataset']['num_chunks']
    chunks = get_latest_chunks(cfg['dataset']['input'], num_chunks)

    train_ratio = cfg['dataset']['train_ratio']
    num_train = int(num_chunks * train_ratio)
    shuffle_size = cfg['training']['shuffle_size']
    ChunkParser.BATCH_SIZE = cfg['training']['batch_size']

    root_dir = os.path.join(cfg['training']['path'], cfg['name'])
    if not os.path.exists(root_dir):
        os.makedirs(root_dir)

    train_parser = ChunkParser(FileDataSrc(chunks[:num_train]),
                               shuffle_size=shuffle_size,
                               sample=SKIP,
                               batch_size=ChunkParser.BATCH_SIZE)
    dataset = tf.data.Dataset.from_generator(train_parser.parse,
                                             output_types=(tf.string,
                                                           tf.string,
                                                           tf.string))
    dataset = dataset.map(ChunkParser.parse_function)
    dataset = dataset.prefetch(4)
    train_iterator = dataset.make_one_shot_iterator()

    shuffle_size = int(shuffle_size * (1.0 - train_ratio))
    test_parser = ChunkParser(FileDataSrc(chunks[num_train:]),
                              shuffle_size=shuffle_size,
                              sample=SKIP,
                              batch_size=ChunkParser.BATCH_SIZE)
    dataset = tf.data.Dataset.from_generator(test_parser.parse,
                                             output_types=(tf.string,
                                                           tf.string,
                                                           tf.string))
    dataset = dataset.map(ChunkParser.parse_function)
    dataset = dataset.prefetch(4)
    test_iterator = dataset.make_one_shot_iterator()

    tfprocess = TFProcess(cfg)
    tfprocess.init(dataset, train_iterator, test_iterator)

    if os.path.exists(os.path.join(root_dir, 'checkpoint')):
        cp = get_checkpoint(root_dir)
        tfprocess.restore(cp)

    # Sweeps through all test chunks statistically
    num_evals = (num_chunks - num_train) * 10 // ChunkParser.BATCH_SIZE
    print("Using {} evaluation batches".format(num_evals))

    for _ in range(cfg['training']['total_steps']):
        tfprocess.process(ChunkParser.BATCH_SIZE, num_evals)

    tfprocess.save_leelaz_weights(cmd.output)

    tfprocess.session.close()
    train_parser.shutdown()
    test_parser.shutdown()
Esempio n. 4
0
def main():
    if len(sys.argv) != 2:
        print("Usage: {} config.yaml".format(sys.argv[0]))
        return 1

    cfg = yaml.safe_load(open(sys.argv[1], 'r').read())
    print(yaml.dump(cfg, default_flow_style=False))

    batch_size = cfg['training']['batch_size']

    filename = os.path.join(cfg['dataset']['path'], 'train.bin')
    train_next_batch, parser = dataset_iterator(filename, batch_size)
    print("Creating trainingset from {}".format(filename))
    num_eval = parser.num_samples() // batch_size
    print("Train epoch in {} steps".format(num_eval))

    filename = os.path.join(cfg['dataset']['path'], 'test.bin')
    test_next_batch, parser = dataset_iterator(filename, batch_size)
    print("Creating testset from {}".format(filename))
    num_eval = parser.num_samples() // batch_size
    print("Test epoch in {} steps".format(num_eval))

    tfprocess = TFProcess(cfg, train_next_batch, test_next_batch, num_eval)

    root_dir = os.path.join(cfg['training']['path'], cfg['name'])
    if os.path.exists(os.path.join(root_dir, 'checkpoint')):
        checkpoint = parse.get_checkpoint(root_dir)
        tfprocess.restore(checkpoint)

    if not os.path.exists(root_dir):
        os.makedirs(root_dir)
        print("Created output directory: {}".format(root_dir))

    while True:
        tfprocess.process(batch_size)
Esempio n. 5
0
def main(args):

    train_data_prefix = args.pop(0)

    chunks = get_chunks(train_data_prefix)
    print("Found {0} chunks".format(len(chunks)))

    if not chunks:
        return

    parser = ChunkParser(chunks)

    run_test(parser)
    #benchmark(parser)

    dataset = tf.data.Dataset.from_generator(
        parser.parse_chunk, output_types=(tf.string))
    dataset = dataset.shuffle(65536)
    dataset = dataset.map(_parse_function)
    dataset = dataset.batch(BATCH_SIZE)
    dataset = dataset.prefetch(16)
    iterator = dataset.make_one_shot_iterator()
    next_batch = iterator.get_next()

    tfprocess = TFProcess(next_batch)
    if args:
        restore_file = args.pop(0)
        tfprocess.restore(restore_file)
    while True:
        tfprocess.process(BATCH_SIZE)
Esempio n. 6
0
def main():
    parser = argparse.ArgumentParser(
        description='Train network from game data.')
    parser.add_argument("trainpref",
        help='Training file prefix', nargs='?', type=str)
    parser.add_argument("restorepref",
        help='Training snapshot prefix', nargs='?', type=str)
    parser.add_argument("--train", '-t',
        help="Training file prefix", type=str)
    parser.add_argument("--test", help="Test file prefix", type=str)
    parser.add_argument("--restore", type=str,
        help="Prefix of tensorflow snapshot to restore from")
    parser.add_argument("--logbase", default='leelalogs', type=str,
        help="Log file prefix (for tensorboard)")
    parser.add_argument("--sample", default=DOWN_SAMPLE, type=int,
        help="Rate of data down-sampling to use")
    args = parser.parse_args()

    train_data_prefix = args.train or args.trainpref
    restore_prefix = args.restore or args.restorepref

    training = get_chunks(train_data_prefix)
    if not args.test:
        # Generate test by taking 10% of the training chunks.
        random.shuffle(training)
        training, test = split_chunks(training, 0.1)
    else:
        test = get_chunks(args.test)

    if not training:
        print("No data to train on!")
        return

    print("Training with {0} chunks, validating on {1} chunks".format(
        len(training), len(test)))

    train_parser = ChunkParser(FileDataSrc(training),
                               shuffle_size=1<<20, # 2.2GB of RAM.
                               sample=args.sample,
                               batch_size=RAM_BATCH_SIZE).parse()

    test_parser = ChunkParser(FileDataSrc(test),
                              shuffle_size=1<<19,
                              sample=args.sample,
                              batch_size=RAM_BATCH_SIZE).parse()

    tfprocess = TFProcess()
    tfprocess.init(RAM_BATCH_SIZE,
                   logbase=args.logbase,
                   macrobatch=BATCH_SIZE // RAM_BATCH_SIZE)

    #benchmark1(tfprocess)

    if restore_prefix:
        tfprocess.restore(restore_prefix)
    tfprocess.process(train_parser, test_parser)
Esempio n. 7
0
def main(cmd):
    cfg = yaml.safe_load(cmd.cfg.read())
    print(yaml.dump(cfg, default_flow_style=False))

    num_chunks = cfg['dataset']['num_chunks']
    chunks = get_latest_chunks(cfg['dataset']['input'], num_chunks)

    train_ratio = cfg['dataset']['train_ratio']
    num_train = int(num_chunks*train_ratio)
    shuffle_size = cfg['training']['shuffle_size']
    ChunkParser.BATCH_SIZE = cfg['training']['batch_size']

    root_dir = os.path.join(cfg['training']['path'], cfg['name'])
    if not os.path.exists(root_dir):
        os.makedirs(root_dir)

    train_parser = ChunkParser(FileDataSrc(chunks[:num_train]),
            shuffle_size=shuffle_size, sample=SKIP, batch_size=ChunkParser.BATCH_SIZE)
    dataset = tf.data.Dataset.from_generator(
        train_parser.parse, output_types=(tf.string, tf.string, tf.string))
    dataset = dataset.map(ChunkParser.parse_function)
    dataset = dataset.prefetch(4)
    train_iterator = dataset.make_one_shot_iterator()

    shuffle_size = int(shuffle_size*(1.0-train_ratio))
    test_parser = ChunkParser(FileDataSrc(chunks[num_train:]), 
            shuffle_size=shuffle_size, sample=SKIP, batch_size=ChunkParser.BATCH_SIZE)
    dataset = tf.data.Dataset.from_generator(
        test_parser.parse, output_types=(tf.string, tf.string, tf.string))
    dataset = dataset.map(ChunkParser.parse_function)
    dataset = dataset.prefetch(4)
    test_iterator = dataset.make_one_shot_iterator()

    tfprocess = TFProcess(cfg)
    tfprocess.init(dataset, train_iterator, test_iterator)

    if os.path.exists(os.path.join(root_dir, 'checkpoint')):
        cp = get_checkpoint(root_dir)
        tfprocess.restore(cp)

    # Sweeps through all test chunks statistically
    num_evals = (num_chunks-num_train)*10 // ChunkParser.BATCH_SIZE
    print("Using {} evaluation batches".format(num_evals))

    for _ in range(cfg['training']['total_steps']):
        tfprocess.process(ChunkParser.BATCH_SIZE, num_evals)

    tfprocess.save_leelaz_weights(cmd.output)

    tfprocess.session.close()
    train_parser.shutdown()
    test_parser.shutdown()
Esempio n. 8
0
def main():
    if len(sys.argv) != 2:
        print("Usage: {} config.yaml".format(sys.argv[0]))
        return 1

    cfg = yaml.safe_load(open(sys.argv[1], 'r').read())
    print(yaml.dump(cfg, default_flow_style=False))

    num_chunks = cfg['dataset']['num_chunks']
    chunks = get_latest_chunks(cfg['dataset']['input'], num_chunks)

    num_train = int(num_chunks*cfg['dataset']['train_ratio'])
    shuffle_size = cfg['training']['shuffle_size']
    ChunkParser.BATCH_SIZE = cfg['training']['batch_size']

    root_dir = os.path.join(cfg['training']['path'], cfg['name'])
    if not os.path.exists(root_dir):
        os.makedirs(root_dir)

    #bench_parser = ChunkParser(FileDataSrc(chunks[:1000]), shuffle_size=1<<14, sample=SKIP, batch_size=ChunkParser.BATCH_SIZE)
    #benchmark(bench_parser)

    train_parser = ChunkParser(FileDataSrc(chunks[:num_train]),
            shuffle_size=shuffle_size, sample=SKIP, batch_size=ChunkParser.BATCH_SIZE)
    #benchmark(train_parser)
    dataset = tf.data.Dataset.from_generator(
        train_parser.parse, output_types=(tf.string, tf.string, tf.string))
    dataset = dataset.map(ChunkParser.parse_function)
    dataset = dataset.prefetch(4)
    train_iterator = dataset.make_one_shot_iterator()

    test_parser = ChunkParser(FileDataSrc(chunks[num_train:]), batch_size=ChunkParser.BATCH_SIZE)
    dataset = tf.data.Dataset.from_generator(
        test_parser.parse, output_types=(tf.string, tf.string, tf.string))
    dataset = dataset.map(ChunkParser.parse_function)
    dataset = dataset.prefetch(4)
    test_iterator = dataset.make_one_shot_iterator()

    tfprocess = TFProcess(cfg)
    tfprocess.init(dataset, train_iterator, test_iterator)

    if os.path.exists(os.path.join(root_dir, 'checkpoint')):
        cp = get_checkpoint(root_dir)
        tfprocess.restore(cp)

    # Sweeps through all test chunks statistically
    num_evals = int(round(((num_chunks-num_train) * (200 / SKIP)) / ChunkParser.BATCH_SIZE))
    print("Using {} evaluation batches".format(num_evals))

    # while True:
    for _ in range(cfg['training']['total_steps']):
        tfprocess.process(ChunkParser.BATCH_SIZE, num_evals)
Esempio n. 9
0
def main(args):
    train_data_prefix = args.pop(0)

    chunks = get_chunks(train_data_prefix)
    print("Found {0} chunks".format(len(chunks)))

    if not chunks:
        return

    # The following assumes positions from one game are not
    # spread through chunks.
    random.shuffle(chunks)
    training, test = split_chunks(chunks, 0.1)
    print("Training with {0} chunks, validating on {1} chunks".format(
        len(training), len(test)))

    train_parser = ChunkParser(FileDataSrc(training),
                               shuffle_size=1 << 19,
                               sample=DOWN_SAMPLE,
                               batch_size=BATCH_SIZE)
    #benchmark(train_parser)
    dataset = tf.data.Dataset.from_generator(train_parser.parse,
                                             output_types=(tf.string,
                                                           tf.string,
                                                           tf.string))
    dataset = dataset.map(_parse_function)
    dataset = dataset.prefetch(4)
    train_iterator = dataset.make_one_shot_iterator()

    test_parser = ChunkParser(FileDataSrc(test),
                              shuffle_size=1 << 19,
                              sample=DOWN_SAMPLE,
                              batch_size=BATCH_SIZE)
    dataset = tf.data.Dataset.from_generator(test_parser.parse,
                                             output_types=(tf.string,
                                                           tf.string,
                                                           tf.string))
    dataset = dataset.map(_parse_function)
    dataset = dataset.prefetch(4)
    test_iterator = dataset.make_one_shot_iterator()

    tfprocess = TFProcess()
    tfprocess.init(dataset, train_iterator, test_iterator)

    #benchmark1(tfprocess)

    if args:
        restore_file = args.pop(0)
        tfprocess.restore(restore_file)
    while True:
        tfprocess.process(BATCH_SIZE)
Esempio n. 10
0
def main(args):
    train_data_prefix = args.pop(0)

    chunks = get_chunks(train_data_prefix)
    print("Found {0} chunks".format(len(chunks)))

    if not chunks:
        return

    # The following assumes positions from one game are not
    # spread through chunks.
    random.shuffle(chunks)
    training, test = split_chunks(chunks, 0.1)
    print("Training with {0} chunks, validating on {1} chunks".format(
        len(training), len(test)))

    #run_test(parser)
    #benchmark(parser)

    train_parser = ChunkParser(training)
    dataset = tf.data.Dataset.from_generator(train_parser.parse_chunk,
                                             output_types=(tf.string))
    dataset = dataset.shuffle(1 << 18)
    dataset = dataset.map(_parse_function)
    dataset = dataset.batch(BATCH_SIZE)
    dataset = dataset.prefetch(4)
    train_iterator = dataset.make_one_shot_iterator()

    test_parser = ChunkParser(test)
    dataset = tf.data.Dataset.from_generator(test_parser.parse_chunk,
                                             output_types=(tf.string))
    dataset = dataset.map(_parse_function)
    dataset = dataset.batch(BATCH_SIZE)
    dataset = dataset.prefetch(4)
    test_iterator = dataset.make_one_shot_iterator()

    tfprocess = TFProcess()
    tfprocess.init(dataset, train_iterator, test_iterator)
    if args:
        restore_file = args.pop(0)
        tfprocess.restore(restore_file)

    for _ in range(12001):
        tfprocess.process(BATCH_SIZE)

    for x in train_parser.mp_instances:
        x.terminate()
        x.join()

    os.killpg(0, signal.SIGTERM)
Esempio n. 11
0
def main(args):
    chunks = latest_chunks()
    parser, next_batch = chunks2batches(chunks)
    tfprocess = TFProcess(next_batch)
    if args:
        restore_file = args.pop(0)
        print("Restoring weights ....")
        tfprocess.restore(restore_file)
    print("Training starts ....")
    while True:
        change_data, run_val = tfprocess.process()
        if change_data:
            chunks = latest_chunks()
            parser.chunk_switch(chunks)
        if run_val:
            best_net = leela_conf.SAVE_DIR + "/best.txt"
            last_net = leela_conf.SAVE_DIR + "/latest.txt"
            cmd = leela_conf.VALIDATION_COMMAND % \
                (last_net,
                 best_net)
            print(cmd)
            subprocess.call(cmd.split(" ")) #, stdout=subprocess.PIPE)
            with open(leela_conf.VALIDATION_LOG, "r") as f:
                better = int(f.readlines()[-1].split("\t")[0])
                if better:
                    print("---------------- Better Network Found! --------------")
                    copy2(last_net, best_net)
                else:
                    print("------------- Checkout best net so far. -------------")
                    tfprocess.replace_weights(get_weights(best_net))
Esempio n. 12
0
def main():
    if len(sys.argv) != 2:
        print("Usage: {} config.yaml".format(sys.argv[0]))
        return 1

    cfg = yaml.safe_load(open(sys.argv[1], 'r').read())
    print(yaml.dump(cfg, default_flow_style=False))

    chunks = get_chunks(cfg['dataset']['input'])
    print("Found {0} chunks".format(len(chunks)))

    if not chunks:
        return

    parser = ChunkParser(chunks)

    run_test(parser)
    #benchmark(parser)

    dataset = tf.data.Dataset.from_generator(parser.parse_chunk,
                                             output_types=(tf.string))
    dataset = dataset.shuffle(65536)
    dataset = dataset.map(_parse_function)
    dataset = dataset.batch(cfg['training']['batch_size'])
    dataset = dataset.prefetch(16)
    iterator = dataset.make_one_shot_iterator()
    next_batch = iterator.get_next()

    tfprocess = TFProcess(cfg, next_batch)
    root_dir = os.path.join(cfg['training']['path'], cfg['name'])
    if os.path.exists(os.path.join(root_dir, 'checkpoint')):
        checkpoint = get_checkpoint(root_dir)
        tfprocess.restore(checkpoint)

    while True:
        tfprocess.process(cfg['training']['batch_size'])
Esempio n. 13
0
def main(cmd):
    cfg = yaml.safe_load(cmd.cfg.read())
    print(yaml.dump(cfg, default_flow_style=False))

    num_chunks = cfg['dataset']['num_chunks']
    chunks = get_latest_chunks(cfg['dataset']['input'], num_chunks)

    train_ratio = cfg['dataset']['train_ratio']
    num_train = int(num_chunks*train_ratio)
    shuffle_size = cfg['training']['shuffle_size']
    ChunkParser.BATCH_SIZE = cfg['training']['batch_size']

    root_dir = os.path.join(cfg['training']['path'], cfg['name'])
    if not os.path.exists(root_dir):
        os.makedirs(root_dir)

    train_parser = ChunkParser(FileDataSrc(chunks[:num_train]),
            shuffle_size=shuffle_size, sample=SKIP, batch_size=ChunkParser.BATCH_SIZE)
    dataset = tf.data.Dataset.from_generator(
        train_parser.parse, output_types=(tf.string, tf.string, tf.string))
    dataset = dataset.map(ChunkParser.parse_function)
    dataset = dataset.prefetch(4)
    train_iterator = dataset.make_one_shot_iterator()

    shuffle_size = int(shuffle_size*(1.0-train_ratio))
    test_parser = ChunkParser(FileDataSrc(chunks[num_train:]), 
            shuffle_size=shuffle_size, sample=SKIP, batch_size=ChunkParser.BATCH_SIZE)
    dataset = tf.data.Dataset.from_generator(
        test_parser.parse, output_types=(tf.string, tf.string, tf.string))
    dataset = dataset.map(ChunkParser.parse_function)
    dataset = dataset.prefetch(4)
    test_iterator = dataset.make_one_shot_iterator()

    tfprocess = TFProcess(cfg)
    tfprocess.init(dataset, train_iterator, test_iterator)

    if os.path.exists(os.path.join(root_dir, 'checkpoint')):
        cp = get_checkpoint(root_dir)
        tfprocess.restore(cp)

    # Sweeps through all test chunks statistically
    num_evals = (num_chunks-num_train)*10 // ChunkParser.BATCH_SIZE
    print("Using {} evaluation batches".format(num_evals))

    for _ in range(cfg['training']['total_steps']):
        tfprocess.process(ChunkParser.BATCH_SIZE, num_evals)

    tfprocess.save_leelaz_weights('/tmp/weights.txt')

    with open('/tmp/weights.txt', 'rb') as f:
        m = hashlib.sha256()
        w = f.read()
        m.update(w)
        digest = m.hexdigest()

    filename = '/tmp/{}.gz'.format(digest)
    with gzip.open(filename, 'wb') as f:
        f.write(w)

    if cmd.upload:
        metadata = {'training_id':'1', 'layers':cfg['model']['residual_blocks'],
                'filters':cfg['model']['filters']}
        print("\nUploading `{}'...".format(digest[:8]), end='')
        upload(cmd.upload, metadata, filename)
        print("[done]\n")
    else:
        print("\nStored `{}'\n".format(filename))
Esempio n. 14
0
def main():
    parser = argparse.ArgumentParser(
        description='Train network from game data.')
    parser.add_argument("blockspref",
                        help="Number of blocks",
                        nargs='?',
                        type=int)
    parser.add_argument("filterspref",
                        help="Number of filters",
                        nargs='?',
                        type=int)
    parser.add_argument("trainpref",
                        help='Training file prefix',
                        nargs='?',
                        type=str)
    parser.add_argument("restorepref",
                        help='Training snapshot prefix',
                        nargs='?',
                        type=str)
    parser.add_argument("--blocks", '-b', help="Number of blocks", type=int)
    parser.add_argument("--filters", '-f', help="Number of filters", type=int)
    parser.add_argument("--train", '-t', help="Training file prefix", type=str)
    parser.add_argument("--test", help="Test file prefix", type=str)
    parser.add_argument("--restore",
                        type=str,
                        help="Prefix of tensorflow snapshot to restore from")
    parser.add_argument(
        "--logbase",
        default='leelalogs',
        type=str,
        help="Log file prefix (for tensorboard) (default: %(default)s)")
    parser.add_argument(
        "--sample",
        default=DOWN_SAMPLE,
        type=int,
        help="Rate of data down-sampling to use (default: %(default)d)")
    args = parser.parse_args()

    blocks = args.blocks or args.blockspref
    filters = args.filters or args.filterspref
    train_data_prefix = args.train or args.trainpref
    restore_prefix = args.restore or args.restorepref

    if not blocks or not filters:
        print("Must supply number of blocks and filters")
        return

    training = get_chunks(train_data_prefix)
    if not args.test:
        # Generate test by taking 10% of the training chunks.
        random.shuffle(training)
        print("here1")
        training, test = split_chunks(training, 0.1)
    else:
        test = get_chunks(args.test)

    if not training:
        print("No data to train on!")
        return

    print("Training with {0} chunks, validating on {1} chunks".format(
        len(training), len(test)))

    train_parser = ChunkParser(
        FileDataSrc(training),
        shuffle_size=1 << 20,  # 2.2GB of RAM.
        sample=args.sample,
        batch_size=RAM_BATCH_SIZE).parse()

    test_parser = ChunkParser(FileDataSrc(test),
                              shuffle_size=1 << 19,
                              sample=args.sample,
                              batch_size=RAM_BATCH_SIZE).parse()

    tfprocess = TFProcess(blocks, filters)
    tfprocess.init(RAM_BATCH_SIZE,
                   logbase=args.logbase,
                   macrobatch=BATCH_SIZE // RAM_BATCH_SIZE)

    #benchmark1(tfprocess)

    if restore_prefix:
        tfprocess.restore(restore_prefix)
    tfprocess.process(train_parser, test_parser)
Esempio n. 15
0
def main():
    parser = argparse.ArgumentParser(
        description='Train network from game data.')
    parser.add_argument("blockspref",
        help="Number of blocks", nargs='?', type=int)
    parser.add_argument("filterspref",
        help="Number of filters", nargs='?', type=int)
    parser.add_argument("trainpref",
        help='Training file prefix', nargs='?', type=str)
    parser.add_argument("restorepref",
        help='Training snapshot prefix', nargs='?', type=str)
    parser.add_argument("--blocks", '-b',
        help="Number of blocks", type=int)
    parser.add_argument("--filters", '-f',
        help="Number of filters", type=int)
    parser.add_argument("--train", '-t',
        help="Training file prefix", type=str)
    parser.add_argument("--test", help="Test file prefix", type=str)
    parser.add_argument("--restore", type=str,
        help="Prefix of tensorflow snapshot to restore from")
    parser.add_argument("--logbase", default='leelalogs', type=str,
        help="Log file prefix (for tensorboard) (default: %(default)s)")
    parser.add_argument("--sample", default=DOWN_SAMPLE, type=int,
        help="Rate of data down-sampling to use (default: %(default)d)")
    parser.add_argument("--bufferbits", default=TRAIN_SHUFFLE_BITS, type=int,
        help="Train shuffle-buffer size in bits (default: %(default)d)")
    parser.add_argument("--rate", default=LEARN_RATE, type=float,
                        help="Learning rate (default: %(default)f)")
    parser.add_argument("--steps", default=TRAINING_STEPS, type=int,
        help="Training step before writing a network (default: %(default)d)")
    parser.add_argument("--maxsteps", default=MAX_TRAINING_STEPS, type=int,
        help="Terminates after this many steps (default: %(default)d)")
    parser.add_argument("--maxkeep", default=MAX_SAVER_TO_KEEP, type=int,
        help="Keeps meta files for at most this many networks (default: %(default)d)")
    parser.add_argument("--policyloss", default=POLICY_LOSS_WT, type=float,
        help="Coefficient for policy term in loss function (default: %(default)f)")
    parser.add_argument("--mseloss", default=MSE_LOSS_WT, type=float,
        help="Coefficient for mse term in loss function (default: %(default)f)")
    parser.add_argument("--regloss", default=REG_LOSS_WT, type=float,
        help="Coefficient for regularizing term in loss function (default: %(default)f)")
    args = parser.parse_args()

    blocks = args.blocks or args.blockspref
    filters = args.filters or args.filterspref
    train_data_prefix = args.train or args.trainpref
    restore_prefix = args.restore or args.restorepref

    if not blocks or not filters:
        print("Must supply number of blocks and filters")
        return

    training = get_chunks(train_data_prefix)
    if not args.test:
        # Generate test by taking 10% of the training chunks.
        random.shuffle(training)
        training, test = split_chunks(training, 0.1)
    else:
        test = get_chunks(args.test)

    if not training:
        print("No data to train on!")
        return

    print("Training with {0} chunks, validating on {1} chunks".format(
        len(training), len(test)))

    train_parser = ChunkParser(FileDataSrc(training),
                               shuffle_size=1<<args.bufferbits, # was 20 -- 2.2GB of RAM.
                               sample=args.sample,
                               batch_size=RAM_BATCH_SIZE).parse()

    test_parser = ChunkParser(FileDataSrc(test),
                              shuffle_size=1<<(args.bufferbits-3),  # was 19
                              sample=args.sample,
                              batch_size=RAM_BATCH_SIZE).parse()

    tfprocess = TFProcess(blocks, filters,
                          args.rate, args.steps, args.maxsteps, args.maxkeep,
                          args.policyloss, args.mseloss, args.regloss)
    tfprocess.init(RAM_BATCH_SIZE,
                   logbase=args.logbase,
                   macrobatch=BATCH_SIZE // RAM_BATCH_SIZE)

    #benchmark1(tfprocess)

    if restore_prefix:
        tfprocess.restore(restore_prefix)
    tfprocess.process(train_parser, test_parser)