Exemple #1
0
 def __call__(self, model, X):
     z = X - model.reconstruct(X)
     out = ops.trace(theano.dot(z, z.T)) / X.shape[0] + ops.trace(
         theano.dot(theano.dot(model.weights.T, model.weights), model.hidden_corruptor.cov)
     )
     #    out2 = (z*z).sum(axis=1).mean() + ops.trace(theano.dot(theano.dot(model.weights.T, model.weights), model.hidden_corruptor.cov))
     return out
Exemple #2
0
    def __init__(self, kernel, X=None, Y=None):
        self.kernel = kernel
        self.X = X
        self.Y = Y

        self.th_hyp = self.kernel.th_hyp
        self.th_X = self.kernel.th_X
        self.th_N = self.kernel.th_N
        self.th_D = self.kernel.th_D
        self.th_K = self.kernel.th_K

        self.th_Y = T.matrix('Y')

        prec = sT.matrix_inverse(self.th_K)

        # Calculate the lml in a slow but stable way
        self.th_lml_stable = (
            -0.5 * sT.trace(T.dot(self.th_Y.T, T.dot(prec, self.th_Y))) +
            -T.sum(T.log(sT.diag(sT.cholesky(self.th_K)))) +
            -0.5 * self.th_N * T.log(2.0 * const.pi))
        # or in a fast but unstable way
        self.th_lml = (
            -0.5 * sT.trace(T.dot(self.th_Y.T, T.dot(prec, self.th_Y))) +
            -0.5 * T.log(sT.det(self.th_K)) +
            -0.5 * self.th_N * T.log(2.0 * const.pi))
        self.th_dlml_dhyp = theano.grad(self.th_lml, self.th_hyp)

        # Compile them to functions
        self.lml = theano.function([self.th_hyp, self.th_X, self.th_Y],
                                   self.th_lml)
        self.lml_stable = theano.function([self.th_hyp, self.th_X, self.th_Y],
                                          self.th_lml_stable)
        self.dlml_dhyp = theano.function([self.th_hyp, self.th_X, self.th_Y],
                                         self.th_dlml_dhyp)
Exemple #3
0
def test_trace():
    rng = numpy.random.RandomState(utt.fetch_seed())
    x = theano.tensor.matrix()
    g = trace(x)
    f = theano.function([x], g)

    for shp in [(2, 3), (3, 2), (3, 3)]:
        m = rng.rand(*shp).astype(config.floatX)
        v = numpy.trace(m)
        assert v == f(m)

    xx = theano.tensor.vector()
    ok = False
    try:
        trace(xx)
    except TypeError:
        ok = True
    assert ok
Exemple #4
0
def test_trace():
    rng = numpy.random.RandomState(utt.fetch_seed())
    x = theano.tensor.matrix()
    g = trace(x)
    f = theano.function([x], g)

    for shp in [(2, 3), (3, 2), (3, 3)]:
        m = rng.rand(*shp).astype(config.floatX)
        v = numpy.trace(m)
        assert v == f(m)

    xx = theano.tensor.vector()
    ok = False
    try:
        trace(xx)
    except TypeError:
        ok = True
    assert ok
Exemple #5
0
 def get_J_SH(self, x, y, P):
     length = tensor.cast(tensor.shape(y)[0], 'int32')
     Sv, updates = theano.scan(lambda ind, y, n: self.sgn_yT(y[ind // n], y[ind % n]),
                                   outputs_info=None,
                                   sequences=[tensor.arange(length ** 2)],
                                   non_sequences=[y,length]
                                  )
     S = tensor.reshape(Sv, (length, length))
     JP = tensor.dot(tensor.dot(tensor.dot( tensor.dot(P.T, x.T), S ), x), P)
     ret = trace(JP)
     ret = tensor.sum(ret) / tensor.sum(P ** 2) / self.length / self.length / 2.
     return -ret
Exemple #6
0
 def get_cost_updates(self, contraction_level, learning_rate, switch, proj=True):
     self.J_AE = self.get_J_AE()
     self.J_SH = self.get_J_SH(self.dx, self.dy, self.P)
     self.J3 = self.lam * tensor.sum(tensor.abs_(self.W))
     self.J4 = self.epsilon * trace(tensor.dot(self.P - self.W, (self.P - self.W).T ))
     cost= self.alpha * self.J_AE + (1. - self.alpha) * self.J_SH + self.J3 + self.J4
     gparams = tensor.grad(cost, self.params)
     updates = []
     param=self.params[0]
     gparam=gparams[0]
     pp=param - learning_rate * gparam
     if proj:
         pp=self.update_ApprProj(pp, switch)
     updates.append((param, pp))
     for param, gparam in zip(self.params[1:], gparams[1:]):
         updates.append((param, param - learning_rate * gparam))
     return (cost, updates)