Esempio n. 1
0
def _mmd2_and_variance(K_XX, K_XY, K_YY, const_diagonal=False, biased=False):
    m = tf.cast(K_XX.get_shape()[0], tf.float32)  # Assumes X, Y are same shape

    ### Get the various sums of kernels that we'll use
    # Kts drop the diagonal, but we don't need to compute them explicitly
    if const_diagonal is not False:
        const_diagonal = tf.cast(const_diagonal, tf.float32)
        diag_X = diag_Y = const_diagonal
        sum_diag_X = sum_diag_Y = m * const_diagonal
        sum_diag2_X = sum_diag2_Y = m * const_diagonal ** 2
    else:
        diag_X = tf.diag_part(K_XX)
        diag_Y = tf.diag_part(K_YY)

        sum_diag_X = tf.reduce_sum(diag_X)
        sum_diag_Y = tf.reduce_sum(diag_Y)

        sum_diag2_X = sq_sum(diag_X)
        sum_diag2_Y = sq_sum(diag_Y)

    Kt_XX_sums = tf.reduce_sum(K_XX, 1) - diag_X
    Kt_YY_sums = tf.reduce_sum(K_YY, 1) - diag_Y
    K_XY_sums_0 = tf.reduce_sum(K_XY, 0)
    K_XY_sums_1 = tf.reduce_sum(K_XY, 1)

    Kt_XX_sum = tf.reduce_sum(Kt_XX_sums)
    Kt_YY_sum = tf.reduce_sum(Kt_YY_sums)
    K_XY_sum = tf.reduce_sum(K_XY_sums_0)

    Kt_XX_2_sum = sq_sum(K_XX) - sum_diag2_X
    Kt_YY_2_sum = sq_sum(K_YY) - sum_diag2_Y
    K_XY_2_sum = sq_sum(K_XY)

    if biased:
        mmd2 = ((Kt_XX_sum + sum_diag_X) / (m * m)
                + (Kt_YY_sum + sum_diag_Y) / (m * m)
                - 2 * K_XY_sum / (m * m))
    else:
        mmd2 = ((Kt_XX_sum + sum_diag_X) / (m * (m - 1))
                + (Kt_YY_sum + sum_diag_Y) / (m * (m - 1))
                - 2 * K_XY_sum / (m * m))

    var_est = (
            2 / (m ** 2 * (m - 1) ** 2) * (
            2 * sq_sum(Kt_XX_sums) - Kt_XX_2_sum
            + 2 * sq_sum(Kt_YY_sums) - Kt_YY_2_sum)
            - (4 * m - 6) / (m ** 3 * (m - 1) ** 3) * (Kt_XX_sum ** 2 + Kt_YY_sum ** 2)
            + 4 * (m - 2) / (m ** 3 * (m - 1) ** 2) * (
                    sq_sum(K_XY_sums_1) + sq_sum(K_XY_sums_0))
            - 4 * (m - 3) / (m ** 3 * (m - 1) ** 2) * K_XY_2_sum
            - (8 * m - 12) / (m ** 5 * (m - 1)) * K_XY_sum ** 2
            + 8 / (m ** 3 * (m - 1)) * (
                    1 / m * (Kt_XX_sum + Kt_YY_sum) * K_XY_sum
                    - dot(Kt_XX_sums, K_XY_sums_1)
                    - dot(Kt_YY_sums, K_XY_sums_0))
    )

    return mmd2, var_est
Esempio n. 2
0
    def loadModel(self, filename, translate, scale, normobj=False):
        model = obj(filename)
        light = [0, 0, 1]
        for face in model.faces:

            vertCount = len(face)
            if normobj:
                for vert in range(vertCount):

                    v0 = model.vertices[int(face[vert][0]) - 1]
                    v1 = model.vertices[int(face[(vert + 1) % vertCount][0]) -
                                        1]

                    x0 = int(v0[0] * scale[0] + translate[0])
                    y0 = int(v0[1] * scale[1] + translate[1])
                    x1 = int(v1[0] * scale[0] + translate[0])
                    y1 = int(v1[1] * scale[1] + translate[1])

                    self.glLineWin(x0, y0, x1, y1)
            else:
                v0 = model.vertices[face[0][0] - 1]
                v1 = model.vertices[face[1][0] - 1]
                v2 = model.vertices[face[2][0] - 1]

                v0 = self.transform1(v0, translate, scale)
                v1 = self.transform1(v1, translate, scale)
                v2 = self.transform1(v2, translate, scale)

                #polycolor = color(random.randint(0,255) / 255,
                #                  random.randint(0,255) / 255,
                #                  random.randint(0,255) / 255)

                normal = op.cross(op.subtract(v1, v0), op.subtract(v2, v0))

                normal = op.divide(normal, op.norm(normal))
                intensity = op.dot(normal, light)

                if intensity >= 0:
                    self.triangle_bc(
                        v0, v1, v2,
                        self.glColor(intensity, intensity, intensity))

                if vertCount > 3:  #asumamos que 4, un cuadrado
                    v3 = model.vertices[face[3][0] - 1]

                    if intensity >= 0:
                        self.triangle_bc(
                            v0, v2, v3, color(intensity, intensity, intensity))
Esempio n. 3
0
def _diff_mmd2_and_ratio_from_sums(Y_related_sums, Z_related_sums, m, const_diagonal=False):
    Kt_YY_sums, Kt_YY_2_sum, K_XY_sums_0, K_XY_sums_1, K_XY_2_sum = Y_related_sums
    Kt_ZZ_sums, Kt_ZZ_2_sum, K_XZ_sums_0, K_XZ_sums_1, K_XZ_2_sum = Z_related_sums

    Kt_YY_sum = tf.reduce_sum(Kt_YY_sums)
    Kt_ZZ_sum = tf.reduce_sum(Kt_ZZ_sums)

    K_XY_sum = tf.reduce_sum(K_XY_sums_0)
    K_XZ_sum = tf.reduce_sum(K_XZ_sums_0)

    # TODO: turn these into dot products?
    # should figure out if that's faster or not on GPU / with theano...

    ### Estimators for the various terms involved
    muY_muY = Kt_YY_sum / (m * (m - 1))
    muZ_muZ = Kt_ZZ_sum / (m * (m - 1))

    muX_muY = K_XY_sum / (m * m)
    muX_muZ = K_XZ_sum / (m * m)

    E_y_muY_sq = (sq_sum(Kt_YY_sums) - Kt_YY_2_sum) / (m * (m - 1) * (m - 2))
    E_z_muZ_sq = (sq_sum(Kt_ZZ_sums) - Kt_ZZ_2_sum) / (m * (m - 1) * (m - 2))

    E_x_muY_sq = (sq_sum(K_XY_sums_1) - K_XY_2_sum) / (m * m * (m - 1))
    E_x_muZ_sq = (sq_sum(K_XZ_sums_1) - K_XZ_2_sum) / (m * m * (m - 1))

    E_y_muX_sq = (sq_sum(K_XY_sums_0) - K_XY_2_sum) / (m * m * (m - 1))
    E_z_muX_sq = (sq_sum(K_XZ_sums_0) - K_XZ_2_sum) / (m * m * (m - 1))

    E_y_muY_y_muX = dot(Kt_YY_sums, K_XY_sums_0) / (m * m * (m - 1))
    E_z_muZ_z_muX = dot(Kt_ZZ_sums, K_XZ_sums_0) / (m * m * (m - 1))

    E_x_muY_x_muZ = dot(K_XY_sums_1, K_XZ_sums_1) / (m * m * m)

    E_kyy2 = Kt_YY_2_sum / (m * (m - 1))
    E_kzz2 = Kt_ZZ_2_sum / (m * (m - 1))

    E_kxy2 = K_XY_2_sum / (m * m)
    E_kxz2 = K_XZ_2_sum / (m * m)

    ### Combine into overall estimators
    mmd2_diff = muY_muY - 2 * muX_muY - muZ_muZ + 2 * muX_muZ

    first_order = 4 * (m - 2) / (m * (m - 1)) * (
            E_y_muY_sq - muY_muY ** 2
            + E_x_muY_sq - muX_muY ** 2
            + E_y_muX_sq - muX_muY ** 2
            + E_z_muZ_sq - muZ_muZ ** 2
            + E_x_muZ_sq - muX_muZ ** 2
            + E_z_muX_sq - muX_muZ ** 2
            - 2 * E_y_muY_y_muX + 2 * muY_muY * muX_muY
            - 2 * E_x_muY_x_muZ + 2 * muX_muY * muX_muZ
            - 2 * E_z_muZ_z_muX + 2 * muZ_muZ * muX_muZ
    )
    second_order = 2 / (m * (m - 1)) * (
            E_kyy2 - muY_muY ** 2
            + 2 * E_kxy2 - 2 * muX_muY ** 2
            + E_kzz2 - muZ_muZ ** 2
            + 2 * E_kxz2 - 2 * muX_muZ ** 2
            - 4 * E_y_muY_y_muX + 4 * muY_muY * muX_muY
            - 4 * E_x_muY_x_muZ + 4 * muX_muY * muX_muZ
            - 4 * E_z_muZ_z_muX + 4 * muZ_muZ * muX_muZ
    )
    var_est = first_order + second_order

    ratio = mmd2_diff / mysqrt(tf.maximum(var_est, _eps))
    return mmd2_diff, ratio
Esempio n. 4
0
def neural_net(x):
    h = x
    for w in (w1, w2):
        h = dot(h, w)
    return h, softmax(h)