Пример #1
0
            loss = loss_fn(images, outputs)
            loss_train += loss.item()

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

        if epoch == 0 or epoch % 5 == 0:
            l = loss_train / images.shape[0]
            model.losses.append(l)
            print('epoch {} loss {:.4f} {}'.format(
                epoch, l,
                datetime.datetime.now().strftime('%H:%M:%S')))
    end = datetime.datetime.now()
    diff = end - start
    print('elapsed', diff)


model.apply(init_weights)

dataloader = torch.utils.data.DataLoader(grid_dataset,
                                         batch_size=128,
                                         shuffle=True)
training_loop(model, optimizer, loss_fn, dataloader, n_epochs)

date_time = datetime.datetime.now().strftime("%Y_%m_%d__%H_%M_%S")
torch.save(model.state_dict(), os.path.join(models_dir,
                                            date_time + '_model.pt'))

plt.plot(model.losses)
Пример #2
0
    if opt.seed == 0:
        seed = random.randint(1, 10000)
        random.seed(seed)
        torch.manual_seed(seed)
        torch.cuda.manual_seed(args.seed)
    else:
        seed = opt.seed
        torch.manual_seed(opt.seed)
        torch.cuda.manual_seed(opt.seed)
    torch.backends.cudnn.benchmark = True

    dictionary = Dictionary.load_from_file('data/dictionary.pkl')
    opt.ntokens = dictionary.ntoken

    model = Model(opt)
    model.apply(weights_init_kn)
    model = nn.DataParallel(model).cuda()

    train_dset = SelfCriticalDataset(opt.split, dictionary, opt)
    train_loader = DataLoader(train_dset,
                              opt.batch_size,
                              shuffle=True,
                              num_workers=0)
    opt.use_all = 1
    eval_dset = SelfCriticalDataset(opt.split_test, dictionary, opt)
    eval_loader = DataLoader(eval_dset,
                             opt.batch_size,
                             shuffle=False,
                             num_workers=0)

    train(model, train_loader, eval_loader, opt)