Esempio n. 1
0
    def __init__(self,
                 types_list=None,
                 learn_std=False,
                 activation_layer='ReLU',
                 K=1,
                 M=1):
        """
        Initialize BaseMissVAE.
        Args:
            types_list (list of dictionaries): Each dictionary contains: name, type, dim, nclass, index; for every
            attribute.
            learn_std (boolean): Learn the :math:`\sigma` for the real and positive distributions.
            activation_layer (string): Choose "relu", "tanh" or "sigmoid".
            K: number of importance weights for IWAE model (see: https://arxiv.org/abs/1509.00519)
            M: number of Monte Carlo samples for ELBO estimation
        """
        super(BaseMissVAE, self).__init__()

        # Heterogeneous vars
        assert types_list is not None
        self.types_list = utils.reindex_types_list(types_list)
        self.transform_idx = utils.get_idx_transform(self.types_list)

        self.device = set_device()
        self.learn_std = learn_std
        self.activation = utils.set_activation_layer(activation_layer)

        # Sampler
        self.sampler = samplers.Sampler()

        # Loss
        self.loss = Loss()
        self.K = K
        self.M = M
Esempio n. 2
0
    def __init__(self, test_loader, scaler, model, save_dir, args):
        r"""

        Args:
            test_loader (Dataloader): Dataloader for the test set.
            scaler (Object): Scaler object for the input-output normalization strategy.
            model (Object): Shi-VAE model.
            save_dir (string): Path to save files.
            args (args): Arguments.
        """
        self.device = set_device()
        self.model = model
        self.dataset = args.dataset

        if self.device == 'cuda':
            self.model.to(self.device)

        self.save_dir = save_dir
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)

        # Result
        self.result = ShiVAEResult(test_loader, scaler, model, args)
Esempio n. 3
0
    def __init__(self,
                 h_dim,
                 x_dim,
                 s_dim,
                 types_list=None,
                 learn_std=False,
                 fixed_std=0.1,
                 activation_layer='ReLU'):
        r"""
        Init
        Args:
            h_dim (int): Dimensionality of the embedding space for the LSTM.
            x_dim (int): Dimensionality of x.
            s_dim (int): Dimensionality of :math:`s`.
            types_list (list of dictionaries): Each dictionary contains: name, type, dim, nclass, index; for every
            attribute.
            learn_std (boolean): If true, learn the :math:`\sigma` for the real and positive distributions.
            fixed_std (float): If :attr:learn_std is False, then use this as :math:`\sigma`.
            activation_layer (string): Choose "relu", "tanh" or "sigmoid".
        """
        super(HeterDecoder, self).__init__()
        # dimensions
        self.h_dim = h_dim
        self.x_dim = x_dim
        self.s_dim = s_dim

        # std options
        self.learn_std = learn_std
        self.fixed_std = fixed_std
        self.device = set_device()

        self.decoder_dict = nn.ModuleDict(
        )  # need for parameters in state_dict()

        self.activation = utils.set_activation_layer(activation_layer)

        assert types_list is not None
        self.types_list = types_list

        # Init Dimensional Decoder:
        for type_dict in self.types_list:
            var = type_dict['name']
            type = type_dict['type']

            d_dict = nn.ModuleDict()
            # Common to any type of data
            d_dict["dec"] = nn.Sequential(
                nn.Linear(h_dim + h_dim + s_dim,
                          h_dim + h_dim), self.activation,
                nn.Linear(h_dim + h_dim, h_dim), self.activation,
                nn.Linear(h_dim, h_dim), self.activation)

            # Real
            if type == 'real':
                d_dict["dec_mean"] = nn.Linear(h_dim, 1)
                if self.learn_std:
                    d_dict["dec_std"] = nn.Sequential(nn.Linear(h_dim, 1),
                                                      nn.Softplus())
                else:
                    pass
            # Positive
            elif type == 'pos':
                d_dict["dec_mean"] = nn.Linear(h_dim, 1)

                if self.learn_std:
                    d_dict["dec_std"] = nn.Sequential(nn.Linear(h_dim, 1),
                                                      nn.Softplus())
                else:
                    pass
            # Bernoulli
            elif type == 'bin':
                d_dict["dec_p"] = nn.Sequential(nn.Linear(h_dim, h_dim),
                                                self.activation,
                                                nn.Linear(h_dim, 1))

            # Categorical
            elif type == 'cat':
                # Categorical parameters are the output of 1 DNN.
                C = int(type_dict['nclass'])  # number of categories
                # First parameter is set to 0 for identifiability
                d_dict["dec_p_cat"] = nn.Sequential(nn.Linear(h_dim, h_dim),
                                                    self.activation,
                                                    nn.Linear(h_dim, C - 1))

            else:
                pass

            self.decoder_dict[var] = d_dict
Esempio n. 4
0
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

import os
import sys

import torch

from lib import utils, datasets as dset
from lib.aux import set_device
from lib.process_args import get_args, save_args
from lib.scalers import HeterogeneousScaler

# CPU or GPU Run
args = get_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
device = set_device()
print("DEVICE: {}".format(device))

dataset = "hmm_heter_1000_1real_1pos_1bin_1cat_3_100"
# args.dataset = dataset
# args.train = -1
# args.n_epochs = 1
# args.z_dim = 2
# args.K = 3
# args.plot_every = 10
# args.kl_annealing_epochs = 20
# args.percent_miss = 50

# Shi-VAE
args.model_name = '{}_{}_{}z_{}h_{}s_{}miss'.format(args.model, args.dataset,
                                                    args.z_dim, args.h_dim,
Esempio n. 5
0
 def __init__(self):
     self.device = set_device()
     self.eps = 1e-6