Esempio n. 1
0
    def logdensity(self, x):
        """ x is a matrix of column datapoints (VxB) V = n_visible, B = batch size """
        B = x.shape[1]
        nl = self.parameters["nonlinearity"].get_numpy_f()
        lp = np.zeros((B, self.n_orderings))

        W1 = self.W1.get_value()
        b1 = self.b1.get_value()
        Wflags = self.Wflags.get_value()
        if self.n_layers > 1:
            Ws = self.Ws.get_value()
            bs = self.bs.get_value()
        V = self.V.get_value()
        c = self.c.get_value()

        for o_index, o in enumerate(self.orderings):
            a = np.zeros((B, self.n_hidden))
            input_mask_contribution = np.zeros((B, self.n_hidden))
            for j in xrange(self.n_visible):
                i = o[j]
                x_i = x[i]
                h = nl(input_mask_contribution + a + b1)
                for l in xrange(self.n_layers - 1):
                    h = nl(np.dot(h, Ws[l]) + bs[l])
                t = np.dot(h, V[i]) + c[i]
                p_xi_is_one = sigmoid(t) * 0.9999 + 0.0001 * 0.5
                lp[:, o_index] += x_i * np.log(p_xi_is_one) + (
                    1 - x_i) * np.log(1 - p_xi_is_one)
                a += np.dot(x[i][:, np.newaxis], W1[i][np.newaxis, :])
                input_mask_contribution += Wflags[i]
        return logsumexp(lp + np.log(1 / self.n_orderings))
Esempio n. 2
0
    def logdensity(self, x):
        """ x is a matrix of column datapoints (VxB) V = n_visible, B = batch size """
        B = x.shape[1]
        nl = self.parameters["nonlinearity"].get_numpy_f()
        lp = np.zeros((B, self.n_orderings))

        W1 = self.W1.get_value()
        b1 = self.b1.get_value()
        Wflags = self.Wflags.get_value()
        if self.n_layers > 1:
            Ws = self.Ws.get_value()
            bs = self.bs.get_value()
        V = self.V.get_value()
        c = self.c.get_value()

        for o_index, o in enumerate(self.orderings):
            a = np.zeros((B, self.n_hidden))
            input_mask_contribution = np.zeros((B, self.n_hidden))
            for j in xrange(self.n_visible):
                i = o[j]
                x_i = x[i]
                h = nl(input_mask_contribution + a + b1)
                for l in xrange(self.n_layers - 1):
                    h = nl(np.dot(h, Ws[l]) + bs[l])
                t = np.dot(h, V[i]) + c[i]
                p_xi_is_one = sigmoid(t) * 0.9999 + 0.0001 * 0.5
                lp[:, o_index] += x_i * np.log(p_xi_is_one) + (1 - x_i) * np.log(1 - p_xi_is_one)
                a += np.dot(x[i][:, np.newaxis], W1[i][np.newaxis, :])
                input_mask_contribution += Wflags[i]
        return logsumexp(lp + np.log(1 / self.n_orderings))
Esempio n. 3
0
    def logdensity(self, x):
        """
        Get log density (log likelihood) of this sample x. 
        """
        """ x is a matrix of column datapoints (VxB) V = n_visible, B = batch size """
        B = x.shape[1]
        nl = self.parameters["nonlinearity"].get_numpy_f()
        lp = np.zeros((B, self.n_orderings))

        W1 = self.W1.get_value()
        b1 = self.b1.get_value()
        Wflags = self.Wflags.get_value()
        if self.n_layers > 1:
            Ws = self.Ws.get_value()
            bs = self.bs.get_value()
        V_alpha = self.V_alpha.get_value()
        b_alpha = self.b_alpha.get_value()
        V_mu = self.V_mu.get_value()
        b_mu = self.b_mu.get_value()
        V_sigma = self.V_sigma.get_value()
        b_sigma = self.b_sigma.get_value()

        for o_index, o in enumerate(self.orderings):
            a = np.zeros((B, self.n_hidden)) + b1
            for j in xrange(self.n_visible):
                i = o[j]
                h = nl(a)
                for l in xrange(self.n_layers - 1):
                    h = nl(np.dot(h, Ws[l]) + bs[l])
                z_alpha = np.dot(h, V_alpha[i]) + b_alpha[i]
                z_mu = np.dot(h, V_mu[i]) + b_mu[i]
                z_sigma = np.dot(h, V_sigma[i]) + b_sigma[i]
                Alpha = Utils.nnet.softmax(z_alpha)  # C
                Mu = z_mu  # C
                Sigma = np.exp(z_sigma)
                lp[:, o_index] += logsumexp(-0.5 * (
                    (Mu - x[i][:, np.newaxis]) / Sigma)**2 - np.log(Sigma) -
                                            0.5 * np.log(2 * np.pi) +
                                            np.log(Alpha))
                if j < self.n_visible - 1:
                    a += np.dot(x[i][:, np.newaxis],
                                W1[i][np.newaxis, :]) + Wflags[i]
        return logsumexp(lp + np.log(1 / self.n_orderings))
Esempio n. 4
0
    def logdensity(self, x):
        """ x is a matrix of column datapoints (VxB) V = n_visible, B = batch size """
        B = x.shape[1]
        nl = self.parameters["nonlinearity"].get_numpy_f()
        lp = np.zeros((B, self.n_orderings))

        W1 = self.W1.get_value()
        b1 = self.b1.get_value()
        Wflags = self.Wflags.get_value()
        if self.n_layers > 1:
            Ws = self.Ws.get_value()
            bs = self.bs.get_value()
        V_alpha = self.V_alpha.get_value()
        b_alpha = self.b_alpha.get_value()
        V_mu = self.V_mu.get_value()
        b_mu = self.b_mu.get_value()
        V_sigma = self.V_sigma.get_value()
        b_sigma = self.b_sigma.get_value()

        for o_index, o in enumerate(self.orderings):
            a = np.zeros((B, self.n_hidden)) + b1
            for j in xrange(self.n_visible):
                i = o[j]
                h = nl(a)
                for l in xrange(self.n_layers - 1):
                    h = nl(np.dot(h, Ws[l]) + bs[l])
                z_alpha = np.dot(h, V_alpha[i]) + b_alpha[i]
                z_mu = np.dot(h, V_mu[i]) + b_mu[i]
                z_sigma = np.dot(h, V_sigma[i]) + b_sigma[i]
                Alpha = Utils.nnet.softmax(z_alpha)  # C
                Mu = z_mu  # C
                Sigma = np.exp(z_sigma)
                lp[:, o_index] += logsumexp(
                    -0.5 * ((Mu - x[i][:, np.newaxis]) / Sigma) ** 2
                    - np.log(Sigma)
                    - 0.5 * np.log(2 * np.pi)
                    + np.log(Alpha)
                )
                if j < self.n_visible - 1:
                    a += np.dot(x[i][:, np.newaxis], W1[i][np.newaxis, :]) + Wflags[i]
        return logsumexp(lp + np.log(1 / self.n_orderings))
Esempio n. 5
0
 def conditional_logdensities(self, x, ys):
     """ n is the number of samples """
     W1 = self.W1.get_value()
     b1 = self.b1.get_value()
     Wflags = self.Wflags.get_value()
     if self.n_layers > 1:
         Ws = self.Ws.get_value()
         bs = self.bs.get_value()
     V_alpha = self.V_alpha.get_value()
     b_alpha = self.b_alpha.get_value()
     V_mu = self.V_mu.get_value()
     b_mu = self.b_mu.get_value()
     V_sigma = self.V_sigma.get_value()
     b_sigma = self.b_sigma.get_value()
     nl = self.parameters["nonlinearity"].get_numpy_f()
     # Sample an order of the dimensions (from the list of orders that conform this MoNADE, which can be just one BTW)
     conditionals = np.zeros((self.n_orderings, self.n_visible, len(ys)))
     for o_index, o in enumerate(self.orderings):
         a = np.zeros((self.n_hidden,)) + b1
         for j in xrange(self.n_visible):
             i = o[j]
             h = nl(a)
             for l in xrange(self.n_layers - 1):
                 h = nl(np.dot(h, Ws[l]) + bs[l])
             z_alpha = np.dot(h, V_alpha[i]) + b_alpha[i]
             z_mu = np.dot(h, V_mu[i]) + b_mu[i]
             z_sigma = np.dot(h, V_sigma[i]) + b_sigma[i]
             Alpha = Utils.nnet.softmax(z_alpha)  # C
             Mu = z_mu  # C
             Sigma = np.exp(z_sigma)
             conditionals[o_index, i, :] += logsumexp(
                 -0.5 * ((Mu[np.newaxis, :] - ys[:, np.newaxis]) / Sigma) ** 2
                 - np.log(Sigma)
                 - 0.5 * np.log(2 * np.pi)
                 + np.log(Alpha),
                 axis=1,
             )
             a += x[i] * W1[i] + Wflags[i]
     return logsumexp(conditionals + np.log(1 / self.n_orderings), axis=0)
Esempio n. 6
0
 def conditional_logdensities(self, x, ys):
     """ n is the number of samples """
     W1 = self.W1.get_value()
     b1 = self.b1.get_value()
     Wflags = self.Wflags.get_value()
     if self.n_layers > 1:
         Ws = self.Ws.get_value()
         bs = self.bs.get_value()
     V_alpha = self.V_alpha.get_value()
     b_alpha = self.b_alpha.get_value()
     V_mu = self.V_mu.get_value()
     b_mu = self.b_mu.get_value()
     V_sigma = self.V_sigma.get_value()
     b_sigma = self.b_sigma.get_value()
     nl = self.parameters["nonlinearity"].get_numpy_f()
     # Sample an order of the dimensions (from the list of orders that conform
     # this MoNADE, which can be just one BTW)
     conditionals = np.zeros((self.n_orderings, self.n_visible, len(ys)))
     for o_index, o in enumerate(self.orderings):
         a = np.zeros((self.n_hidden, )) + b1
         for j in xrange(self.n_visible):
             i = o[j]
             h = nl(a)
             for l in xrange(self.n_layers - 1):
                 h = nl(np.dot(h, Ws[l]) + bs[l])
             z_alpha = np.dot(h, V_alpha[i]) + b_alpha[i]
             z_mu = np.dot(h, V_mu[i]) + b_mu[i]
             z_sigma = np.dot(h, V_sigma[i]) + b_sigma[i]
             Alpha = Utils.nnet.softmax(z_alpha)  # C
             Mu = z_mu  # C
             Sigma = np.exp(z_sigma)
             conditionals[o_index, i, :] += logsumexp(
                 -0.5 *
                 ((Mu[np.newaxis, :] - ys[:, np.newaxis]) / Sigma)**2 -
                 np.log(Sigma) - 0.5 * np.log(2 * np.pi) + np.log(Alpha),
                 axis=1)
             a += x[i] * W1[i] + Wflags[i]
     return logsumexp(conditionals + np.log(1 / self.n_orderings), axis=0)