def test_cosine_similarity(self):
    x = tf.fill((10, 10), 1.)
    y = tf.fill((10, 10), 2.)
    self.assertAllClose(tensor_utils.cosine_similarity(x, y), 1.)

    x = tf.fill((10, 10), 1.)
    y = tf.fill((10, 10), -2.)
    self.assertAllClose(tensor_utils.cosine_similarity(x, y), -1.)

    x = tf.concat([tf.random.normal((10, 10)), tf.fill((10, 10), 0.)], axis=0)
    y = tf.concat([tf.fill((10, 10), 0.), tf.random.normal((10, 10))], axis=0)
    self.assertAllClose(tensor_utils.cosine_similarity(x, y), 0.)
示例#2
0
def estimate_scaling(eigvec, matmul_fn, num_samples):
    """Assuming eigvec is an eigenvector of matmul_fn, estimate its eigenvalue.

  Args:
    eigvec (vector): a vector.
    matmul_fn (function): a function defining a matrix-vector product.
    num_samples (int): how many samples to estimate the eigenvalue over.
  Returns:
    total_eigval (float): the estimated eigenvalue for eigvec.
  """
    total_eigval = 0.
    total_eigvec_hvp = 0.
    for _ in range(num_samples):
        eigvec_hvp = matmul_fn(eigvec)
        estimated_eigval = tf.math.reduce_euclidean_norm(eigvec_hvp)
        estimated_eigval = (estimated_eigval if tf.reduce_all(
            tf.reduce_sum(tf.multiply(eigvec, eigvec_hvp)) > 0.) else
                            -estimated_eigval)

        total_eigval += estimated_eigval
        total_eigvec_hvp += eigvec_hvp
    total_eigval /= num_samples
    total_eigvec_hvp /= num_samples
    logging.info('Eigenvector estimation: difference in norms is %.3lf',
                 tf.linalg.norm(eigvec - total_eigvec_hvp / total_eigval))
    logging.info(
        'Eigenvector estimation: cosine is %.3lf',
        tensor_utils.cosine_similarity(eigvec,
                                       total_eigvec_hvp / total_eigval))
    return total_eigval
示例#3
0
    def test_deterministic_largest_eigenvalue_estimation_correct(self):
        tf.compat.v1.random.set_random_seed(0)
        d_ev = 10
        mat = tf.random.normal((d_ev, d_ev), stddev=1)
        mat = tf.abs((mat + tf.transpose(mat)) / 2.)
        e, v = tf.linalg.eigh(mat)

        def matmul_fn_largest(v):
            return tf.transpose(tf.matmul(mat, v, transpose_b=True))

        largest_ev, largest_evec = eigenvalues.iterated_ev(matmul_fn_largest,
                                                           200,
                                                           d_ev,
                                                           print_freq=50)
        self.assertAllClose(largest_ev, e[-1], atol=0.01)
        self.assertAllClose(tf.abs(largest_evec),
                            tf.abs(tf.reshape(v[:, -1], [1, -1])),
                            atol=0.01)

        evec_product = matmul_fn_largest(largest_evec)
        self.assertAllClose(tensor_utils.cosine_similarity(
            evec_product, largest_evec),
                            1.,
                            atol=0.1)
        self.assertAllClose(evec_product, largest_ev * largest_evec, atol=0.01)