Пример #1
0
 def sample(self, n=1):
     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()
     nl = self.parameters["nonlinearity"].get_numpy_f()
     samples = np.zeros((self.n_visible, n))
     for s in xrange(n):
         # Sample an ordering
         ordering = self.orderings[np.random.randint(len(self.orderings))]
         a = np.zeros((self.n_hidden, ))  # H
         input_mask_contribution = np.zeros((self.n_hidden))
         for j in xrange(self.n_visible):
             i = ordering[j]
             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  # B
             input_mask_contribution += Wflags[i]
             a += np.dot(samples[i, s][np.newaxis, np.newaxis],
                         W1[i][np.newaxis, :])
             samples[i, s] = np.random.random() < p_xi_is_one
     return samples
Пример #2
0
 def sample(self, n=1):
     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()
     nl = self.parameters["nonlinearity"].get_numpy_f()
     samples = np.zeros((self.n_visible, n))
     for s in xrange(n):
         # Sample an ordering
         ordering = self.orderings[np.random.randint(len(self.orderings))]
         a = np.zeros((self.n_hidden,))  # H
         input_mask_contribution = np.zeros((self.n_hidden))
         for j in xrange(self.n_visible):
             i = ordering[j]
             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  # B
             input_mask_contribution += Wflags[i]
             a += np.dot(samples[i, s][np.newaxis, np.newaxis], W1[i][np.newaxis, :])
             samples[i, s] = np.random.random() < p_xi_is_one
     return samples
Пример #3
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))
Пример #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 = 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))