Esempio n. 1
0
                        default=100000,
                        help='Max rank of the embedding')
    parser.add_argument('--classname', help='Name of class in model.py')
    parser.add_argument(
        '--like',
        help='train a new model like this one, pick parameters from the name')
    parser.add_argument('--model-file', help='file-name-prefix to save to')
    args = parser.parse_args()

    word_embeddings = data.read_embeddings(args.embeddings, args.maxrank_emb)

    with open(args.train_file) as f:
        train_conllu = data.read_conll(f)
        inputs_train_dict, outputs_train_dict, output_features = data.prep_data(
            train_conllu,
            args.dicts_file,
            word_embeddings.vocab,
            word_seq_len=None,
            shuffle=True)
    word_seq_len = inputs_train_dict["inp_char_seq"].shape[1]
    with open(args.devel_file) as f:
        devel_conllu = data.read_conll(f)
        inputs_devel_dict, outputs_devel_dict, output_features_dev = data.prep_data(
            devel_conllu,
            args.dicts_file,
            word_embeddings.vocab,
            word_seq_len=word_seq_len,
            shuffle=False)
        assert output_features == output_features_dev

    if args.like:
        params = params_from_name(args.like)
Esempio n. 2
0
def train_model(epochs, net, criterion, optimizer, early_stop):
    """Perform the training."""
    # DataLoaders
    train_loader, val_loader = prep_data(BATCH_SIZE, THREADS, USE_CUDA)

    net.train()
    loss_list = []

    for epoch in tqdm(range(epochs), total=epochs):
        running_loss = MetricMeter()
        running_accuracy = MetricMeter()
        val_loss_meter = MetricMeter()
        val_acc_meter = MetricMeter()

        for i, dict_ in enumerate(train_loader):
            images = dict_['img']
            target = dict_['target'].type(torch.FloatTensor)  #.long()

            if USE_CUDA:
                images = images.cuda()
                target = target.cuda()

            images = Variable(images)
            target = Variable(target)

            output = net(images).squeeze()
            loss = criterion(output, target)
            acc = accuracy(target.data, output.data)
            running_loss.update(loss.data[0])
            running_accuracy.update(acc)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

        for i, dict_ in enumerate(val_loader):
            images = dict_['img']
            target = dict_['target'].type(torch.FloatTensor)  #.long()

            if USE_CUDA:
                images = images.cuda()
                target = target.cuda()

            images = Variable(images)
            target = Variable(target)

            output = net(images).squeeze()
            val_loss = criterion(output, target)
            val_acc = accuracy(target.data, output.data)
            val_loss_meter.update(val_loss.data[0])
            val_acc_meter.update(val_acc)

        # hacky early stopping...
        loss_list.append(val_loss_meter.avg)
        if (mean(loss_list[-12:-4]) < mean(
                loss_list[-4:])) and (len(loss_list) > 12) and early_stop:
            print('Validation loss no longer decreasing... stopping training.')
            break

        print("[ loss: {:.4f} | acc: {:.4f} | vloss: {:.4f} | vacc: {:.4f} ]".
              format(running_loss.avg, running_accuracy.avg,
                     val_loss_meter.avg, val_acc_meter.avg))

    ts = time.strftime("%Y-%m-%d_%H-%M")
    vl = '_{:.5f}'.format(val_loss_meter.avg)
    fname = 'model_' + ts + vl + '.pth'
    torch.save(net, os.path.join(THIS_DIR, '..', 'weights', fname))