Esempio n. 1
0
def main(args):
    """ main() driver function """

    # Parameters parsing
    if filepath_is_not_valid(args.config):
        logging.error("The path {} is not a file. Aborting..".format(args.config))
        exit()

    configuration, architecture, hyperparameters = parse_config_file(args.config, args.variation)
    dataset_info = prepare_dataset(configuration)
    if (dataset_info is None):
        exit()

    # Initialization
    model = None
    if (args.variation == "VAE"):
        model = VAE(architecture, hyperparameters, dataset_info)
    elif (args.variation == "B-VAE"):
        model = betaVAE(architecture, hyperparameters, dataset_info)

    # here you can change the gpus parameter into the amount of gpus you want the model to use
    trainer = Trainer(max_epochs = hyperparameters["epochs"], gpus=None, fast_dev_run=False)

    # Training and testing
    trainer.fit(model)
    result = trainer.test(model)
    # Model needs to be transferred to the cpu as sample and reconstruct are custom methods
    model = model.cpu()
    model.sample(5)
    model.reconstruct(5)
Esempio n. 2
0
        train(epoch)
        evaluate()
        evaluate('test')
    # test()

    # _, idx = torch.sort(score, 1, True)

    # y_score = idx[:, :args.N]
    # test = T.tocsr()
    # evaluator = Evaluator({'recall'})

    # evaluator.evaluate(run, test)
    # result = evaluator.show(
    #     ['recall_5', 'recall_10', 'recall_15', 'recall_20'])
    # print(result)
    # line = 'cVAE\t{}\t{}\t{}\t{}\t0'.format(args.data, args.alpha, args.beta, len(args.layer))
    # for _, value in result.items():
    #     line += '\t{:.5f}'.format(value)
    # line += '\r\n'
    # file = open('result', 'a')
    # file.write(line)
    # file.close()
    #
    if args.save:
        name = 'cvae' if args.rating else 'fvae'
        path = directory + '/model/' + name
        for l in args.layer:
            path += '_' + str(l)
        model.cpu()
        torch.save(model.state_dict(), path)