Ejemplo n.º 1
0
def train_mnist_classification_model(args, train_loader, val_loader):
    # short names
    seq_len = args[Synthetic.seq_len]
    n_features, hidden_size = args[AutoEncoder.n_features], args[
        AutoEncoder.hidden_size]
    optimizer_name, lr, momentum = args[AutoEncoder.optimizer], args[
        AutoEncoder.lr], args[AutoEncoder.momentum]

    # init auto encoder
    ae_lstm = LstmAutoencoderMnistClassification(seq_len=seq_len,
                                                 n_features=n_features,
                                                 hidden_size=hidden_size)

    # init optim, crtitertion and loaders
    optimizer = auxlariy.get_optim(optim_name=optimizer_name,
                                   ae=ae_lstm,
                                   lr=lr,
                                   momentum=momentum)
    criterion_ce = auxlariy.get_criterion(args[AutoEncoder.criterion])
    criterion_mse = auxlariy.get_criterion(Criterion.mse)

    # training
    train(train_loader, val_loader, criterion_ce, criterion_mse, optimizer,
          ae_lstm, args)

    torch.save(ae_lstm.state_dict(),
               r'..\models\MNIST\lstm_ae_mnist_classification_model.pt'
               )  # save the best model

    return ae_lstm
Ejemplo n.º 2
0
def test_mnist_classification_model(args, test_loader, model=None):
    # short names
    seq_len = args[Synthetic.seq_len]
    n_features, hidden_size = args[AutoEncoder.n_features], args[
        AutoEncoder.hidden_size]

    # load trained model
    if model is None:
        model = LstmAutoencoderMnistClassification(seq_len=seq_len,
                                                   n_features=n_features,
                                                   hidden_size=hidden_size)
        model.load_state_dict(
            torch.load(
                r'..\models\MNIST\lstm_ae_mnist_classification_model.pt'))

    # get criterion
    criterion_ce = auxlariy.get_criterion(args[AutoEncoder.criterion])
    criterion_mse = auxlariy.get_criterion(Criterion.mse)

    # test
    loss = test(criterion_ce=criterion_ce,
                criterion_mse=criterion_mse,
                test_loader=test_loader,
                model=model)
    # print('test loss: %.3f' % loss)

    return loss
Ejemplo n.º 3
0
def train_toy_model(args, train_loader, val_loader):
    # short names
    seq_range, seq_len, num_samples = args[Synthetic.seq_range], args[
        Synthetic.seq_len], args[Synthetic.num_samples]
    n_features, hidden_size = args[AutoEncoder.n_features], args[
        AutoEncoder.hidden_size]
    optimizer_name, lr, momentum = args[AutoEncoder.optimizer], args[
        AutoEncoder.lr], args[AutoEncoder.momentum]

    # init auto encoder
    ae_lstm = LstmAutoencoderToy(seq_len=seq_len,
                                 n_features=n_features,
                                 hidden_size=hidden_size)

    # init optim, crtitertion and loaders
    optimizer = auxlariy.get_optim(optim_name=optimizer_name,
                                   ae=ae_lstm,
                                   lr=lr,
                                   momentum=momentum)
    criterion = auxlariy.get_criterion(args[AutoEncoder.criterion])

    # training
    train(train_loader, val_loader, criterion, optimizer, ae_lstm, args)

    return ae_lstm
Ejemplo n.º 4
0
def train_snp500_model(args, train_loader, val_loader, ae):
    # short names
    optimizer_name, lr, momentum = args[AutoEncoder.optimizer], args[
        AutoEncoder.lr], args[AutoEncoder.momentum]

    # init optim, crtitertion and loaders
    optimizer = auxlariy.get_optim(optim_name=optimizer_name,
                                   ae=ae,
                                   lr=lr,
                                   momentum=momentum)
    criterion = auxlariy.get_criterion(args[AutoEncoder.criterion])

    # training
    train(train_loader, val_loader, criterion, optimizer, ae, args)

    return ae
Ejemplo n.º 5
0
def test_snp500_model(args, test_loader, model=None):
    # short names
    seq_len = args[Synthetic.seq_len]
    n_features, hidden_size = args[AutoEncoder.n_features], args[
        AutoEncoder.hidden_size]

    # # load trained model
    if model is None:
        model = LstmAutoencoderSnp500FC(seq_len=seq_len,
                                        n_features=n_features,
                                        hidden_size=hidden_size)
        model.load_state_dict(torch.load(r'..\models\SP500\lstm_ae_snp500.pt'))

    # get criterion
    criterion = auxlariy.get_criterion(args[AutoEncoder.criterion])

    # test
    loss = test(criterion=criterion, test_loader=test_loader, model=model)
    # print('test loss: %.3f' % loss)

    return loss