Пример #1
0
 def square_dist(self, X, Xs):
     X = aet.mul(X, 1.0 / self.ls)
     X2 = aet.sum(aet.square(X), 1)
     if Xs is None:
         sqd = -2.0 * aet.dot(X, aet.transpose(X)) + (
             aet.reshape(X2, (-1, 1)) + aet.reshape(X2, (1, -1)))
     else:
         Xs = aet.mul(Xs, 1.0 / self.ls)
         Xs2 = aet.sum(aet.square(Xs), 1)
         sqd = -2.0 * aet.dot(X, aet.transpose(Xs)) + (
             aet.reshape(X2, (-1, 1)) + aet.reshape(Xs2, (1, -1)))
     return aet.clip(sqd, 0.0, np.inf)
Пример #2
0
    def __call__(self, X):
        XY = X.dot(X.T)
        x2 = at.sum(X**2, axis=1).dimshuffle(0, "x")
        X2e = at.repeat(x2, X.shape[0], axis=1)
        H = X2e + X2e.T - 2.0 * XY

        V = at.sort(H.flatten())
        length = V.shape[0]
        # median distance
        m = at.switch(
            at.eq((length % 2), 0),
            # if even vector
            at.mean(V[((length // 2) - 1):((length // 2) + 1)]),
            # if odd vector
            V[length // 2],
        )

        h = 0.5 * m / at.log(floatX(H.shape[0]) + floatX(1))

        #  RBF
        Kxy = at.exp(-H / h / 2.0)

        # Derivative
        dxkxy = -at.dot(Kxy, X)
        sumkxy = at.sum(Kxy, axis=-1, keepdims=True)
        dxkxy = at.add(dxkxy, at.mul(X, sumkxy)) / h

        return Kxy, dxkxy
Пример #3
0
    def test_composite_elemwise_float16(self):
        w = aesara.tensor.bvector()
        x = aesara.tensor.vector(dtype="float16")
        y = aesara.tensor.fvector()

        cz = tensor.tanh(x + tensor.cast(y, "float16"))
        o = (cz - cz**2 + tensor.cast(x, "int16") + tensor.cast(x, "float32") +
             tensor.cast(w, "float16") - tensor.constant(np.float16(1.0)))

        aesara.function([w, x, y], o, mode=mode_with_gpu)

        v = aesara.tensor.vector(dtype="uint8")
        w = aesara.tensor.vector(dtype="float16")
        x = aesara.tensor.vector(dtype="float16")
        y = aesara.tensor.vector(dtype="float16")
        z = aesara.tensor.vector(dtype="float16")

        o = tensor.switch(v, tensor.mul(w, x, y), z)
        aesara.function([v, w, x, y, z], o, mode=mode_with_gpu)
Пример #4
0
 def __call__(self, X):
     return at.mul(self.m1(X), self.m2(X))