コード例 #1
0
def main():
    datasets = get_datasets(in_dims=('speed', 'available', 'total', 'speed_ha'), out_dims=('speed',))
    dls = get_dataloaders(datasets, batch_size=16, collate_fn=bj_collate_fn)

    model = Ours(n_in=4, n_out=1)
    save_folder = args.save_folder

    trainer = BJTrainer(
        model,
        loss=get_loss(args.loss),
        device=torch.device(args.cuda),
        optimizer=get_optimizer(args.optimizer, model.parameters(), lr=args.lr),
        in_scalar=ZScoreScaler(
            mean=torch.tensor([34.71207, 0.55837995, 1.454227, 35.422764, 0.57980937, 1.4051558], dtype=torch.float32),
            std=torch.tensor([11.989664, 0.28689522, 0.5432855, 9.341317, 0.15121026, 0.4632336], dtype=torch.float32)
        ),
        out_scalar=ZScoreScaler(
            mean=torch.tensor([0.55837995], dtype=torch.float32),
            std=torch.tensor([0.28689522], dtype=torch.float32)
        ),
        max_grad_norm=args.max_grad_norm
    )
    if not args.test:
        train_model(
            dls,
            folder=save_folder,
            trainer=trainer,
            scheduler=None,
            epochs=args.epochs,
            early_stop_steps=args.early_stop_steps,
            use_checkpoint=args.resume
        )
    test_model(
        dls['test'],
        trainer=trainer,
        folder=save_folder,
    )
コード例 #2
0
], betas=(0.9, 0.999))

# 損失関数の設定
criterion = nn.CrossEntropyLoss()
# nn.LogSoftmax()を計算してからnn.NLLLoss(negative log likelihood loss)を計算


# In[ ]:


# 学習・検証を実施
from utils.train import train_model

# 学習・検証を実行する。
num_epochs = 1
net_trained = train_model(net, dataloaders_dict,
                          criterion, optimizer, num_epochs=num_epochs)


# In[ ]:


# 学習したネットワークパラメータを保存します
save_path = './weights/bert_fine_tuning_chABSA_test.pth'
torch.save(net_trained.state_dict(), save_path)


# # 4.サンプルの文章で推論とAttentionを可視化する。

# In[20]:

コード例 #3
0
ファイル: main.py プロジェクト: xiang526/CGCDemandPrediction
def train(conf, data_category):
    print(json.dumps(conf, indent=4))

    os.environ["CUDA_VISIBLE_DEVICES"] = str(conf['device'])
    device = torch.device(0)

    model_name = conf['model']['name']
    optimizer_name = conf['optimizer']['name']
    data_set = conf['data']['dataset']
    graph = h5py.File(os.path.join('data', data_set, 'all_graph.h5'), 'r')
    scheduler_name = conf['scheduler']['name']
    loss = get_loss(**conf['loss'])
    # data_category = conf['data']['data_category']

    loss.to(device)
    encoder, decoder, support = None, None, None
    if model_name == 'Costnet':
        base_model_name = conf['Base']['name']
        encoder, decoder = preprocessing(base_model_name, conf, loss, graph,
                                         data_category, device, data_set,
                                         optimizer_name, scheduler_name)
    if model_name == 'Metricnet' or model_name == 'GWNET' or model_name == 'Evonet' or model_name == 'STGCN' or model_name == 'DCRNN' or model_name == 'STG2Seq' or model_name == 'Evonet2':
        support = preprocessing_for_metric(
            data_category=data_category,
            dataset=conf['data']['dataset'],
            Normal_Method=conf['data']['Normal_Method'],
            _len=conf['data']['_len'],
            **conf['preprocess'])
    model, trainer = create_model(model_name, loss, conf['model'][model_name],
                                  data_category, device, graph, encoder,
                                  decoder, support)

    optimizer = get_optimizer(optimizer_name, model.parameters(),
                              conf['optimizer'][optimizer_name]['lr'])
    scheduler = get_scheduler(scheduler_name, optimizer,
                              **conf['scheduler'][scheduler_name])
    if torch.cuda.device_count() > 1:
        print("use ", torch.cuda.device_count(), "GPUS")
        model = nn.DataParallel(model)
    else:
        model.to(device)

    save_folder = os.path.join('save', conf['name'],
                               f'{data_set}_{"".join(data_category)}',
                               conf['tag'])
    run_folder = os.path.join('run', conf['name'],
                              f'{data_set}_{"".join(data_category)}',
                              conf['tag'])

    shutil.rmtree(save_folder, ignore_errors=True)
    os.makedirs(save_folder)
    shutil.rmtree(run_folder, ignore_errors=True)
    os.makedirs(run_folder)

    with open(os.path.join(save_folder, 'config.yaml'), 'w+') as _f:
        yaml.safe_dump(conf, _f)

    data_loader, normal = get_data_loader(**conf['data'],
                                          data_category=data_category,
                                          device=device,
                                          model_name=model_name)

    if len(data_category) == 2:
        train_model(model=model,
                    dataloaders=data_loader,
                    trainer=trainer,
                    node_num=conf['node_num'],
                    loss_func=loss,
                    optimizer=optimizer,
                    normal=normal,
                    scheduler=scheduler,
                    folder=save_folder,
                    tensorboard_folder=run_folder,
                    device=device,
                    **conf['train'])
        # test_model(folder = save_folder)
    else:
        train_baseline(model=model,
                       dataloaders=data_loader,
                       trainer=trainer,
                       optimizer=optimizer,
                       normal=normal,
                       scheduler=scheduler,
                       folder=save_folder,
                       tensorboard_folder=run_folder,
                       device=device,
                       **conf['train'])
        test_baseline(folder=save_folder,
                      trainer=trainer,
                      model=model,
                      normal=normal,
                      dataloaders=data_loader,
                      device=device)
コード例 #4
0
    'plot_latentspace': args.plot_latentspace,
    'plot_npoints': args.plot_npoints
}

if args.twofold and args.load is None:
    n_epochs = args.epochs
    train_options = {
        'epochs': args.epochs,
        'save_epochs': args.save_epochs,
        'name': '%s-solo' % args.name,
        'results_folder': args.saveroot + '/' + args.name + '-solo'
    }
    train_model(audioSet,
                vae,
                elbo_loss,
                task=args.conditioning,
                options=train_options,
                plot_options=plot_options,
                save_with={'sh_args': args})

#%% Second Training Step

# normalization
normalize = False

# target regularization dimensions
if args.target_dims in ['min', 'max']:
    out = vae.encode(vae.format_input_data(audioSet.data),
                     y=vae.format_label_data(audioSet.metadata.get(args.task)))
    zs = [out['z_params_enc'][0][0], out['z_params_enc'][0][1]]
    idx = np.argsort(zs[0].std())
コード例 #5
0
ファイル: main.py プロジェクト: aascode/self-attention-HAR
                        default='pamap2',
                        type=str,
                        help='Name of Dataset for Model Training')

    args = parser.parse_args()

    model_config_file = open('configs/model.yaml', mode='r')
    model_cfg = yaml.load(model_config_file, Loader=yaml.FullLoader)

    train_x, train_y, val_x, val_y, test_x, test_y = get_data(
        dataset=args.dataset)

    if args.train:
        print('\n[MODEL TRAINING]')
        train_model(dataset=args.dataset,
                    model_config=model_cfg,
                    train_x=train_x,
                    train_y=train_y,
                    val_x=val_x,
                    val_y=val_y,
                    epochs=args.epochs)

    if args.test:
        print('\n[MODEL INFERENCE]')
        pred = test_model(dataset=args.dataset,
                          model_config=model_cfg,
                          test_x=test_x)
        generate_result(dataset=args.dataset,
                        ground_truth=test_y,
                        prediction=pred)
コード例 #6
0
def train(conf, data_category):
    print(json.dumps(conf, indent=4))

    os.environ["CUDA_VISIBLE_DEVICES"] = str(conf['device'])
    device = torch.device(0)

    model_name = conf['model']['name']
    optimizer_name = conf['optimizer']['name']
    data_set = conf['data']['dataset']
    scheduler_name = conf['scheduler']['name']
    loss = get_loss(**conf['loss'])

    loss.to(device)


    support = preprocessing_for_metric(data_category=data_category, dataset=conf['data']['dataset'],
                                           Normal_Method=conf['data']['Normal_Method'], _len=conf['data']['_len'], **conf['preprocess'])
    model, trainer = create_model(model_name,
                                  loss,
                                  conf['model'][model_name],
                                  data_category,
                                  device,
                                  support)

    optimizer = get_optimizer(optimizer_name, model.parameters(), conf['optimizer'][optimizer_name]['lr'])
    scheduler = get_scheduler(scheduler_name, optimizer, **conf['scheduler'][scheduler_name])
    if torch.cuda.device_count() > 1:
        print("use ", torch.cuda.device_count(), "GPUS")
        model = nn.DataParallel(model)
    else:
        model.to(device)

    save_folder = os.path.join('save', conf['name'], f'{data_set}_{"".join(data_category)}', conf['tag'])
    run_folder = os.path.join('run', conf['name'], f'{data_set}_{"".join(data_category)}', conf['tag'])

    shutil.rmtree(save_folder, ignore_errors=True)
    os.makedirs(save_folder)
    shutil.rmtree(run_folder, ignore_errors=True)
    os.makedirs(run_folder)

    with open(os.path.join(save_folder, 'config.yaml'), 'w+') as _f:
        yaml.safe_dump(conf, _f)

    data_loader, normal = get_data_loader(**conf['data'], data_category=data_category, device=device,
                                          model_name=model_name)


    train_model(model=model,
                       dataloaders=data_loader,
                       trainer=trainer,
                       optimizer=optimizer,
                       normal=normal,
                       scheduler=scheduler,
                       folder=save_folder,
                       tensorboard_folder=run_folder,
                       device=device,
                       **conf['train'])
    test_model(folder=save_folder,
                      trainer=trainer,
                      model=model,
                      normal=normal,
                      dataloaders=data_loader,
                      device=device)
コード例 #7
0
ファイル: run.py プロジェクト: peternara/torchfashion
                               momentum=0.9,
                               weight_decay=1e-4)
elif args.optimizer == 'Adam':
    optimizer_conv = optim.Adam(parameters_totrain, lr=0.001)

# Decay LR by a factor of 0.1 every 7 epochs
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_conv, step_size=7, gamma=0.1)

# Make save folder
save_folder = Path('log') / Path(args.save_folder)
if not save_folder.exists():
    save_folder.mkdir(parents=True)

save_file = str(save_folder / Path(args.attribute + '.pth'))

# Load trained model before if define it
if args.trained_model:
    print("Load trained model from {}".format(args.trained_model))
    model_conv.load_state_dict(torch.load(args.trained_model))
# Kick off the train
model_conv = train_model(model_conv,
                         criterion,
                         optimizer_conv,
                         exp_lr_scheduler,
                         dataloaders,
                         dataset_sizes,
                         use_gpu,
                         save_file,
                         num_epochs=args.epochs,
                         verbose=args.verbose)
コード例 #8
0
                                     BATCH_SIZE)
test_data_loader = create_data_loader(test_data, BASE_TRANSFORMER, MAX_SEQ_LEN,
                                      BATCH_SIZE)

# init model
print("\nInit Model...")
models_class_name = globals()[TUNING_STRATEGY]
model = models_class_name(BASE_TRANSFORMER, 4).to(device)

# Run training
out_dir_name = BASE_TRANSFORMER + TUNING_STRATEGY + str(EPOCHS) + 'e'
best_model_path = os.path.join(SAVED_MODEL_DIR, out_dir_name,
                               out_dir_name + '-best.bin')

print("\nStart training...")
train_history = train_model(model,
                            train_data_loader,
                            val_data_loader,
                            EPOCHS,
                            device,
                            model_name=out_dir_name,
                            output_dir=SAVED_MODEL_DIR)

if EVALUATE_TEST:
    # load best model
    print("\nLoad best model...")
    best_state = torch.load(best_model_path)
    model.load_state_dict(best_state)
    print("Evaluating on test set...")
    compute_report(model, test_data_loader, device)
コード例 #9
0
        transforms.Normalize([0.4601,0.4601,0.4601],[0.2701,0.2701,0.2701])
    ])

    test_dataset = CustomDataset(dataset_root_folder , \
        test_set_csv, transform = test_set_transform)

    test_loader = DataLoader(test_dataset,
                             batch_size=8,
                             shuffle=True,
                             pin_memory=True,
                             num_workers=4)

    checkpoint = 'model_final.pth'

    model = ClassifierNet(NUM_CLASSES)
    model = model.to(device)
    criterion = torch.nn.CrossEntropyLoss()
    optim = torch.optim.Adagrad(model.parameters(),
                                lr=0.001,
                                weight_decay=1e-3)

    trained_model = train_model(model,
                                optim,
                                NO_EPOCHS,
                                criterion,
                                train_loader,
                                validation_loader=test_loader,
                                device=device)
    torch.save(trained_model.state_dict(), 'model_final.pth')

    eval_model(model, checkpoint, test_loader, criterion, device)
コード例 #10
0
    'train':
    DataLoader(DataGenerator(train_set),
               batch_size=batch_size,
               shuffle=True,
               num_workers=config['train']['num_workers']),
    'val':
    DataLoader(DataGenerator(val_set),
               batch_size=4,
               shuffle=True,
               num_workers=0)
}

dataset_sizes = {x: len(image_datasets[x]) for x in image_datasets.keys()}

logging.info(dataset_sizes)

model = ResNet50().cuda()

optimizer_ft = optim.SGD(model.parameters(),
                         lr=config['optimizer']['lr'],
                         momentum=config['optimizer']['momentum'])
exp_lr_scheduler = lr_scheduler.StepLR(
    optimizer_ft,
    step_size=config['optimizer']['scheduler_steps'],
    gamma=config['optimizer']['gamma'])
model = train_model(model,
                    optimizer_ft,
                    exp_lr_scheduler,
                    num_epochs=config['train']['epochs'],
                    dataloaders=dataset,
                    device=device)