Esempio n. 1
0
                   h2=16,
                   embedding_dim=args.embedding_dim)
    model.to(device=device)

    # output dir
    output_dir = "./results/{:%Y%m%d_%H%M%S}/".format(datetime.now())
    output_path = output_dir + "model.weights"
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # train
    print("Training start ...")
    start = time.time()
    best_dev_score = train(model,
                           data,
                           output_path=output_path,
                           args=args,
                           loss_func=bpr_loss,
                           score_func=AUC)
    print("Training process took {:.2f} seconds\n".format(time.time() - start))

    # load best model and analysis on test set
    model.load_state_dict(torch.load(output_path))
    test_score = evaluate(model, data, AUC, args.batch_size, on='test')
    dev_hit5, dev_hit10 = hit_eval(model, data, on='dev')
    test_hit5, test_hit10 = hit_eval(model, data, on='test')

    # write results
    save_results('./' + args.dataset + '_deepfm.txt', args, best_dev_score,
                 test_score, dev_hit5, dev_hit10, test_hit5, test_hit10)
Esempio n. 2
0
    print('[init criterion]')
    criterion = nn.BCEWithLogitsLoss()

    print('[init engines]')
    trainer = Trainer(model=model,
                      optimizer=optimizer,
                      criterion=criterion,
                      device=args.device)
    evaluator = Evaluator(model=model, criterion=criterion, device=args.device)

    print('[set handlers]')
    set_handlers(trainer=trainer,
                 evaluator=evaluator,
                 valloader=valloader,
                 model=model,
                 optimizer=optimizer,
                 args=args)

    if args.pretrained_path:
        print('[load pretrained checkpoint]')
        ckpt = torch.load(args.pretrained_path, map_location=args.device)
        model.load_state_dict(ckpt['model'])
        optimizer.load_state_dict(ckpt['optimizer'])
        trainer.load_state_dict(ckpt['trainer'])

    print('[start training]')
    trainer.run(trainloader, max_epochs=args.max_epochs)

    print('[done]')