Beispiel #1
0
 def test_gpu_eigh_opt(self):
     A = theano.tensor.fmatrix("A")
     fn = theano.function([A], eigh(A), mode=mode_with_gpu)
     assert any([
         isinstance(node.op, GpuMagmaEigh)
         for node in fn.maker.fgraph.toposort()
     ])
Beispiel #2
0
 def test_gpu_eigh_opt(self):
     A = theano.tensor.fmatrix("A")
     fn = theano.function([A], eigh(A), mode=mode_with_gpu)
     assert any([
         isinstance(node.op, GpuMagmaEigh)
         for node in fn.maker.fgraph.toposort()
     ])
    def __init__(self, x, components=None):
        self.x = x

        # zero mean
        self.x -= tensor.mean(self.x, axis=0)

        # calculate covariance matrix
        n, dim = self.x.shape
        self.cov = tensor.dot(tensor.transpose(self.x), self.x) / (dim - 1)

        # compute eigenvectors
        v, w = linalg.eigh(self.cov)

        v, w = v[::-1], w[:, ::-1]


        if components != None:
            v = v[:components]
            w = w[:, :components]

        self.w = w
        self.v = v

        # compute pca of input
        self.output = tensor.dot(self.x, self.w)
Beispiel #4
0
    def call(self, X, training=None):

        X0 = K.dot(X, self.U)
        if training in {0, False}:
            return X0

        nd = K.shape(X)[1]
        n = K.shape(X)[0]
        C = K.dot(K.transpose(X), X) / K.cast(n - 1, 'float32')
        self.C = self.momentum * self.C + (1 - self.momentum) * C

        C = C + self.r * eye_like(C)

        [D, V] = eigh(C)

        # Added to increase stability
        if BACKEND == 'theano':
            posInd = K.greater(D, eps).nonzero()[0]
            D = D[posInd]
            V = V[:, posInd]
        else:
            posBool = K.greater(D, eps)
            D = tf.boolean_mask(D, posBool)
            V = tf.boolean_mask(V, posBool, axis=1)

        U = K.dot(K.dot(V, diag(reciprocal(K.sqrt(D)))), K.transpose(V))
        U = K.transpose(U)

        self.add_update([(self.U, U)], X)

        X_updated = K.dot(X, U)

        return K.in_train_phase(X_updated, X0, training=training)
Beispiel #5
0
    def logp(self, X):
        n = self.n
        p = self.p
        V = self.V

        IVI = det(V)
        IXI = det(X)

        return bound(
            ((n - p - 1) * log(IXI) - trace(matrix_inverse(V).dot(X)) -
             n * p * log(2) - n * log(IVI) - 2 * multigammaln(n / 2., p)) / 2,
            gt(n, (p - 1)), all(gt(eigh(X)[0], 0)), eq(X, X.T))
Beispiel #6
0
    def logp(self, X):
        n = self.n
        p = self.p
        V = self.V

        IVI = det(V)
        IXI = det(X)

        return bound(
            ((n - p - 1) * T.log(IXI) - trace(matrix_inverse(V).dot(X)) -
             n * p * T.log(2) - n * T.log(IVI) - 2 * multigammaln(n / 2., p)) /
            2, T.all(eigh(X)[0] > 0), T.eq(X, X.T), n > (p - 1))
Beispiel #7
0
    def logp(self, X):
        n = self.n
        p = self.p
        V = self.V

        IVI = det(V)
        IXI = det(X)

        return bound(
            ((n - p - 1) * log(IXI) - trace(matrix_inverse(V).dot(X)) -
                n * p * log(2) - n * log(IVI) - 2 * multigammaln(n / 2., p)) / 2,
            gt(n, (p - 1)),
            all(gt(eigh(X)[0], 0)),
            eq(X, X.T)
        )
Beispiel #8
0
    def logp(self, X):
        n = self.n
        p = self.p
        V = self.V

        IVI = det(V)
        IXI = det(X)

        return bound(
            (
                (n - p - 1) * T.log(IXI)
                - trace(matrix_inverse(V).dot(X))
                - n * p * T.log(2)
                - n * T.log(IVI)
                - 2 * multigammaln(n / 2.0, p)
            )
            / 2,
            T.all(eigh(X)[0] > 0),
            T.eq(X, X.T),
            n > (p - 1),
        )