Ejemplo n.º 1
0
    def __init__(self, n_inputs, n_outputs, n_hiddens, act_fun, n_layers, n_comps, batch_norm=True, output_order='sequential', mode='sequential', input=None, output=None, rng=np.random):
        """
        Constructor.
        :param n_inputs: number of (conditional) inputs
        :param n_outputs: number of outputs
        :param n_hiddens: list with number of hidden units for each hidden layer
        :param act_fun: name of activation function
        :param n_layers: number of layers in the flow
        :param n_comps: number of gaussians for each conditional of target made
        :param batch_norm: whether to use batch normalization between mades in the flow
        :param output_order: order of outputs of last made
        :param mode: strategy for assigning degrees to hidden nodes: can be 'random' or 'sequential'
        :param input: theano variable to serve as input; if None, a new variable is created
        :param output: theano variable to serve as output; if None, a new variable is created
        """

        # save input arguments
        self.n_inputs = n_inputs
        self.n_outputs = n_outputs
        self.n_hiddens = n_hiddens
        self.act_fun = act_fun
        self.n_layers = n_layers
        self.n_comps = n_comps
        self.batch_norm = batch_norm
        self.mode = mode

        self.input = tt.matrix('x', dtype=dtype) if input is None else input
        self.y = tt.matrix('y', dtype=dtype) if output is None else output
        self.parms = []

        # maf
        self.maf = ConditionalMaskedAutoregressiveFlow(n_inputs, n_outputs, n_hiddens, act_fun, n_layers, batch_norm, output_order, mode, self.input, self.y, rng)
        self.bns = self.maf.bns
        self.parms += self.maf.parms
        self.output_order = self.maf.output_order

        # mog made
        output_order = output_order if output_order == 'random' else self.maf.mades[-1].output_order[::-1]
        self.made = mades.ConditionalMixtureOfGaussiansMade(n_inputs, n_outputs, n_hiddens, act_fun, n_comps, output_order, mode, self.input, self.maf.u, rng)
        self.parms += self.made.parms

        # log likelihoods
        self.L = self.made.L + self.maf.logdet_dudy
        self.L.name = 'L'

        # train objective
        self.trn_loss = -tt.mean(self.L)
        self.trn_loss.name = 'trn_loss'

        # theano evaluation functions, will be compiled when first needed
        self.eval_lprob_f = None
        self.eval_grad_f = None
        self.eval_score_f = None
        self.eval_us_f = None
Ejemplo n.º 2
0
def train_mog_made_cond(n_hiddens, act_fun, n_comps, mode):

    assert is_data_loaded(), 'Dataset hasn\'t been loaded'
    model = mades.ConditionalMixtureOfGaussiansMade(data.n_labels,
                                                    data.n_dims,
                                                    n_hiddens,
                                                    act_fun,
                                                    n_comps,
                                                    mode=mode)
    train_cond(model, a_made)
    save_model(model, 'made_cond', mode, n_hiddens, act_fun, n_comps, False)