Beispiel #1
0
def main():
    parser = argparse.ArgumentParser(description='Chainer example: Titanic')
    parser.add_argument('--unit',
                        '-u',
                        type=int,
                        default=1000,
                        help='Number of units')
    args = parser.parse_args()

    print('# unit: {}'.format(args.unit))
    print('')

    # Set up a neural network to train
    # Classifier reports softmax cross entropy loss and accuracy at every
    # iteration, which will be used by the PrintReport extension below.
    model = L.Classifier(nets.MLP(args.unit, 2, False, 0.5))

    test_df = get_test_titanic()
    x_test = test_df[:, 1:]
    # 標準化
    sc = StandardScaler()
    x_test = sc.fit_transform(x_test)

    chainer.serializers.load_npz("train_chainer.model", model)
    label_prediction = predict(model, x_test)

    ids = test_df[:, 0].reshape(x_test.shape[0], 1)
    result = np.hstack((ids, label_prediction.reshape(x_test.shape[0], 1)))
    np.savetxt("predict_chainer.csv",
               result,
               delimiter=",",
               header="PassengerId,Survived",
               fmt='%.0f')
Beispiel #2
0
Datei: svr.py Projekt: marcwww/LL
def load_mlp(opt, device):
    basename = "mlp"
    model_fname = basename + ".model"
    location = {
        'cuda:' + str(opt.gpu): 'cuda:' + str(opt.gpu)
    } if opt.gpu != -1 else 'cpu'
    model_dict = torch.load(model_fname, map_location=location)
    model = nets.MLP(opt.idim, opt.nclasses).to(device)
    model.load_state_dict(model_dict)
    return model
Beispiel #3
0
Datei: svr.py Projekt: marcwww/LL
def build_model(opt):
    location = opt.gpu if torch.cuda.is_available(
    ) and opt.gpu != -1 else 'cpu'
    device = torch.device(location)
    model = nets.MLP(opt.idim, opt.nclasses).to(device)
    task_permutation = np.random.permutation(784)
    # task_permutation = np.array(range(784))
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(params=filter(lambda p: p.requires_grad,
                                         model.parameters()),
                           lr=opt.lr)

    return device, model, task_permutation, criterion, optimizer
Beispiel #4
0
    elif args.dataset == 'cifar10' or args.dataset == 'cifar100':
        # return (- torch.distributions.Normal(x.view(-1, img_dim ** 2), 1.).log_prob(rce)).sum() + beta * kld
        return F.mse_loss(rce, x, reduction='sum') + beta * kld


if args.flow == 'Planar':
    flows = SimpleFlowModel(
        [Planar((args.latent_size, )) for k in range(args.K)])
elif args.flow == 'Radial':
    flows = SimpleFlowModel(
        [Radial((args.latent_size, )) for k in range(args.K)])
elif args.flow == 'RealNVP':
    b = torch.Tensor([1 if i % 2 == 0 else 0 for i in range(args.latent_size)])
    flows = []
    for i in range(args.K):
        s = nets.MLP([args.latent_size, 8, args.latent_size])
        t = nets.MLP([args.latent_size, 8, args.latent_size])
        if i % 2 == 0:
            flows += [MaskedAffineFlow(b, s, t)]
        else:
            flows += [MaskedAffineFlow(1 - b, s, t), BatchNorm()]
    flows = SimpleFlowModel(
        flows[:-1])  # Remove last Batch Norm layer to allow arbitrary output

model = FlowVAE(flows).to(device)
optimizer = optim.Adam(model.parameters(), lr=0.001)
# train_losses = []
train_loader, test_loader = flow_vae_datasets(args.dataset)


def train(model, epoch, beta):
Beispiel #5
0
    if opt.net == 'rnn_atten':
        model = nets.RNNAtteion(voc_size=len(TXT.vocab.itos),
                                edim=opt.edim,
                                hdim=opt.hdim,
                                dropout=opt.dropout,
                                padding_idx=TXT.vocab.stoi[PAD],
                                nclasses=nclasses).to(device)
    if opt.net == 'rnn_atten_lm':
        model = nets.RNNAtteionLM(voc_size=len(TXT.vocab.itos),
                                  edim=opt.edim,
                                  hdim=opt.hdim,
                                  dropout=opt.dropout,
                                  padding_idx=TXT.vocab.stoi[PAD],
                                  nclasses=nclasses).to(device)
    if opt.net == 'mlp':
        model = nets.MLP(opt.idim, nclasses).to(device)

    if opt.net == 'rammlp':
        model = nets.RAMMLP(idim=opt.idim,
                            nclasses=nclasses,
                            capacity=opt.capacity,
                            criterion=nn.CrossEntropyLoss(),
                            bsz_sampling=opt.bsz_sampling).to(device)

    if opt.net == 'mbpamlp':
        model = nets.MbPAMLP(idim=opt.idim,
                             nclasses=nclasses,
                             capacity=opt.capacity,
                             criterion=nn.CrossEntropyLoss(),
                             add_per=opt.add_per,
                             device=device).to(device)
def main():
    parser = argparse.ArgumentParser(description='Chainer example: Titanic')
    parser.add_argument('--batchsize', '-b', type=int, default=100,
                        help='Number of images in each mini-batch')
    parser.add_argument('--epoch', '-e', type=int, default=10,
                        help='Number of sweeps over the dataset to train')
    parser.add_argument('--frequency', '-f', type=int, default=-1,
                        help='Frequency of taking a snapshot')
    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('--unit', '-u', type=int, default=1000,
                        help='Number of units')
    args = parser.parse_args()

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

    # Set up a neural network to train
    # Classifier reports softmax cross entropy loss and accuracy at every
    # iteration, which will be used by the PrintReport extension below.
    model = L.Classifier(nets.MLP(args.unit, 2))
    if args.gpu >= 0:
        chainer.cuda.get_device(args.gpu).use()  # Make a specified GPU current
        model.to_gpu()  # Copy the model to the GPU

    # Setup an optimizer
    optimizer = chainer.optimizers.Adam()
    optimizer.setup(model)
    optimizer.add_hook(chainer.optimizer.WeightDecay(0.01))

    # Load the MNIST dataset
    #train, test = chainer.datasets.get_mnist()
    train, test = get_titanic()
    
    train_iter = chainer.iterators.SerialIterator(train, args.batchsize)
    test_iter = chainer.iterators.SerialIterator(test, args.batchsize,
                                                 repeat=False, shuffle=False)

    # Set up a trainer
    updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)

    # Evaluate the model with the test dataset for each epoch
    trainer.extend(extensions.Evaluator(test_iter, model, device=args.gpu))

    # Dump a computational graph from 'loss' variable at the first iteration
    # The "main" refers to the target link of the "main" optimizer.
    trainer.extend(extensions.dump_graph('main/loss'))

    # Take a snapshot for each specified epoch
    frequency = args.epoch if args.frequency == -1 else max(1, args.frequency)
    trainer.extend(extensions.snapshot(), trigger=(frequency, 'epoch'))

    # Write a log of evaluation statistics for each epoch
    trainer.extend(extensions.LogReport())

    # Save two plot images to the result dir
    if extensions.PlotReport.available():
        trainer.extend(
            extensions.PlotReport(['main/loss', 'validation/main/loss'],
                                  'epoch', file_name='loss.png'))
        trainer.extend(
            extensions.PlotReport(
                ['main/accuracy', 'validation/main/accuracy'],
                'epoch', file_name='accuracy.png'))

    # Print selected entries of the log to stdout
    # Here "main" refers to the target link of the "main" optimizer again, and
    # "validation" refers to the default name of the Evaluator extension.
    # Entries other than 'epoch' are reported by the Classifier link, called by
    # either the updater or the evaluator.
    trainer.extend(extensions.PrintReport(
        ['epoch', 'main/loss', 'validation/main/loss',
         'main/accuracy', 'validation/main/accuracy', 'elapsed_time']))

    # Print a progress bar to stdout
    trainer.extend(extensions.ProgressBar())

    if args.resume:
        # Resume from a snapshot
        chainer.serializers.load_npz(args.resume, trainer)

    # Run the training
    trainer.run()

    chainer.serializers.save_npz("train_chainer.model", model)