예제 #1
0
    def __init__(self, args: Dict[str, Any]) -> None:
        # engine attributes:
        # encoder/decoder_optim
        # encoder/decoder
        # device
        in_dim = VOCAB_SIZE
        ce = int(args['--char_embedding'])
        we = int(args['--word_embedding'])
        layers = int(args['--layers'])
        lr = float(args['--lr'])
        mhs = int(args['--mhidden_size'])
        hs = int(args['--hidden_size'])
        dropout = float(args['--dropout'])
        margin = float(args['--margin'])

        self.alpha = float(args['--alpha'])
        self.device = device(args['--device'])
        self.teacher_forcing = float(args['--teacher_forcing'])

        if args['--protein_data'] != '':
            self.pEncoder = encoder.EncoderLstm({
                'char_embedding': ce,
                'morph_out': mhs,
                'device': self.device,
                'layers': layers,
                'hidden': hs,
                'bidirectional': True,
                'encoder': 'lstm',
                'activation': 'relu',
                'input_dim': in_dim,
                'dropout': dropout,
                'word_embedding': we
            }).to(self.device)

            self.pLoss = loss.SimilarityLoss(margin=margin)
            self.pOptimizer = optim.Adam(self.pEncoder.parameters(), lr=lr)

        if args['--morpheme_data'] != '':
            self.mSeq2Seq = net.Model(vocab_size=VOCAB_SIZE,
                                      tag_size=args['--tag_size'],
                                      embedding_size=ce,
                                      hidden_size=mhs,
                                      use_hierarchical_attention=False,
                                      use_ptr_gen=True,
                                      device=self.device,
                                      dropout_p=dropout).to(self.device)
            if args['--protein_data'] != '':
                self.pEncoder.char_embedd = self.mSeq2Seq.lemma_encoder.embedder
                self.pEncoder.share_lstm = self.mSeq2Seq.lemma_encoder.lstm
                self.pOptimizer = optim.Adam(self.pEncoder.parameters(), lr=lr)

            self.mOptimizer = optim.Adam(self.mSeq2Seq.parameters(), lr=lr)
예제 #2
0
def main():
    parser = argparse.ArgumentParser(description='CNN Shogi USI Interface:')
    parser.add_argument('--npz',
                        '-n',
                        default='result/snapshot',
                        help='Snapshot')
    parser.add_argument('--gpu',
                        '-g',
                        type=int,
                        default=-1,
                        help='GPU ID (negative value indicates CPU)')
    args = parser.parse_args()

    xp = chainer.cuda.cupy if args.gpu >= 0 else np

    while True:
        try:
            if sys.version_info[0] == 2:
                command = raw_input()
            else:
                command = input()
        except EOFError:
            continue
        command = command.split()
        if command[0] == 'usi':
            print('id name CNNShogi')
            print('id author not522 modified by air-h-128k-il')
            print('usiok')
        elif command[0] == 'isready':
            model = net.Model()
            chainer.serializers.load_npz(args.npz, model)
            print('readyok')
        elif command[0] == 'position':
            shogi = Shogi()
            for move in command[1:]:
                if move == 'startpos' or move == 'moves':
                    continue
                shogi.move(move)
        elif command[0] == 'go':
            res = model(np.asarray([shogi.get_channels()]))
            res = F.argmax(res)
            res = res.data
            channel = res / 81
            to_y = res % 81 / 9
            to_x = res % 9
            if shogi.turn == 1:
                shogi.board = xp.flipud(shogi.board)
                shogi.board = xp.fliplr(shogi.board)
            if channel < 20:
                dy = [0, 0, 1, -1, 1, -1, 1, -1, 2, 2]  #移動方向らしい
                dx = [1, -1, 0, 0, 1, -1, -1, 1, 1, -1]  #移動方向らしい
                try:
                    for i in range(1, 9):
                        from_y = (to_y).astype(int) + i * dy[
                            (channel % 10).astype(int)]
                        from_x = (to_x).astype(int) + i * dx[
                            (channel % 10).astype(int)]
                        if shogi.board[from_y][from_x] != 0:
                            break
                except IndexError:
                    print('bestmove resign')
                if shogi.turn == 1:
                    to_y = 8 - to_y
                    to_x = 8 - to_x
                    from_y = 8 - from_y
                    from_x = 8 - from_x
                    to_y = round(to_y)
                    to_x = round(to_x)
                    from_y = round(from_y)
                    from_x = round(from_x)
                print('bestmove ' + chr(ord('9') - (from_x).astype(int)) +
                      chr(ord('a') + (from_y).astype(int)) +
                      chr(ord('9') - (to_x).astype(int)) +
                      chr(ord('a') +
                          (to_y).astype(int)) + ('' if channel < 10 else '+'))
            else:
                if shogi.turn == 1:
                    to_y = 8 - to_y
                    to_x = 8 - to_x
                    to_y = round(to_y)
                    to_x = round(to_x)
                piece = ['P', 'L', 'N', 'S', 'G', 'B', 'R']
                print('bestmove ' + piece[channel - 20] + '*' +
                      chr(ord('9') - (to_x).astype(int)) +
                      chr(ord('a') + (to_y).astype(int)))
        elif command[0] == 'quit':
            break
        sys.stdout.flush()
예제 #3
0
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 14 16:19:14 2018

@author: Zongyue Qin
"""
import utils
import numpy as np
import net
import torch

if __name__ == '__main__':

    # Load data
    X, y, Xtest, ytest, labels = utils.load_data()
    model = net.Model(epoch=50, batch_size=100, lr=3e-4)
    model.fit(X, y)

    # Compute test error
    ytest = torch.tensor(ytest, dtype=torch.int64)
    t, _ = Xtest.shape
    error = 0

    # Because the memory limitation of my own computer, I cannot predict all at
    # once
    for i in range(0, t, 50):
        yhat = model.predict(Xtest[i:i + 50, :])
        error = error +\
        torch.sum(torch.tensor(yhat != ytest[i:i+50], dtype=torch.float)).item()

    print('test error = %f' % (error / t))
예제 #4
0
def main():
    parser = argparse.ArgumentParser(description='CNN Shogi:')
    parser.add_argument('--batchsize',
                        '-b',
                        type=int,
                        default=100,
                        help='Number of data in each mini-batch')
    parser.add_argument('--alpha',
                        '-a',
                        type=float,
                        default=0.001,
                        help='Alpha parameter of Adam')
    parser.add_argument('--epoch',
                        '-e',
                        type=int,
                        default=10,
                        help='Number of sweeps over the dataset to train')
    parser.add_argument('--gpu',
                        '-g',
                        type=int,
                        default=-1,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--out',
                        '-o',
                        default='result',
                        help='Directory to output the result')
    parser.add_argument('--resume',
                        '-r',
                        default='',
                        help='Resume the training from snapshot')
    parser.add_argument('--datasize',
                        '-d',
                        type=int,
                        default=1000,
                        help='Number of data')
    args = parser.parse_args()

    print('GPU: {}'.format(args.gpu))
    print('# Minibatch-size: {}'.format(args.batchsize))
    print('# epoch: {}'.format(args.epoch))
    print('')

    train, test = get_data(args.datasize)
    model = net.Model()
    classifier = L.Classifier(model)

    # GPUを使う場合
    if args.gpu >= 0:
        chainer.cuda.get_device(args.gpu).use()
        classifier.to_gpu()

    # trainerの設定
    optimizer = chainer.optimizers.Adam(alpha=args.alpha)
    optimizer.setup(classifier)
    optimizer.add_hook(chainer.optimizer.WeightDecay(5e-4))

    train_iter = chainer.iterators.SerialIterator(train, args.batchsize)
    test_iter = chainer.iterators.SerialIterator(test,
                                                 args.batchsize,
                                                 repeat=False,
                                                 shuffle=False)

    updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)
    trainer.extend(TestModeEvaluator(test_iter, classifier, device=args.gpu))
    trainer.extend(extensions.dump_graph('main/loss'))
    trainer.extend(
        extensions.snapshot_object(target=model,
                                   filename='snapshot',
                                   trigger=(args.epoch, 'epoch')))
    trainer.extend(extensions.LogReport())
    trainer.extend(
        extensions.PrintReport([
            'epoch', 'main/loss', 'validation/main/loss', 'main/accuracy',
            'validation/main/accuracy', 'elapsed_time'
        ]))
    trainer.extend(extensions.ProgressBar())

    if args.resume:
        chainer.serializers.load_npz(args.resume, trainer)

    trainer.run()