def compile(self): if self.logger is not None: self.logger.debug("Compiling TopKEignevaluesBattched Callback") _op = self._construct_laszlo_operator_batched() self._lanczos_tensor = {self.K: lanczos_bidiag(_op, self.K)} if self.K != self.feature_K: self._lanczos_tensor[self.feature_K] = lanczos_bidiag(_op, self.feature_K) self.parameters = self.get_my_model().trainable_weights self.parameter_names = [p.name for p in self.get_my_model().trainable_weights] self.parameter_shapes = [K.int_shape(p) for p in self.get_my_model().trainable_weights] def get_eigenvalue_feature(): biggest_eigenval = self.compute_top(k=self.feature_K, with_vectors=False) return np.float32(biggest_eigenval) def get_eigenvector_features(): biggest_eigenval, biggest_eigenvec = self.compute_top(k=self.feature_K, with_vectors=True) return [np.float32(biggest_eigenval), np.float32(biggest_eigenvec[:, :self.feature_K])] self.spectral_norm_tensor = tf.py_func(get_eigenvalue_feature, [], tf.float32) eigenvector_tensors = tf.py_func(get_eigenvector_features, [], [tf.float32, tf.float32]) self.spectral_norm_tensor_2 = eigenvector_tensors[0] self.eigenvector_tensor = eigenvector_tensors[1]
def test_lanczos_bidiag(self): np.random.seed(1) a_np = np.random.uniform( low=-1.0, high=1.0, size=np.prod(shape_)).reshape(shape_).astype(dtype_) tol = 1e-12 if dtype_ == np.float64 else 1e-5 with self.cached_session() as sess: if use_static_shape_: a = constant_op.constant(a_np) else: a = array_ops.placeholder(dtype_) operator = util.create_operator(a) lbd = lanczos.lanczos_bidiag( operator, steps_, orthogonalize=orthogonalize_) # The computed factorization should satisfy the equations # A * V = U * B # A' * U[:, :-1] = V * B[:-1, :]' av = math_ops.matmul(a, lbd.v) ub = lanczos.bidiag_matmul(lbd.u, lbd.alpha, lbd.beta, adjoint_b=False) atu = math_ops.matmul(a, lbd.u[:, :-1], adjoint_a=True) vbt = lanczos.bidiag_matmul(lbd.v, lbd.alpha, lbd.beta, adjoint_b=True) if use_static_shape_: av_val, ub_val, atu_val, vbt_val = sess.run([av, ub, atu, vbt]) else: av_val, ub_val, atu_val, vbt_val = sess.run([av, ub, atu, vbt], feed_dict={a: a_np}) self.assertAllClose(av_val, ub_val, atol=tol, rtol=tol) self.assertAllClose(atu_val, vbt_val, atol=tol, rtol=tol)
def approximate_log_det(A, m, nv, sess): # Inputs: A a linear operator, m degree, nv = number of samples u = tfp.math.random_rademacher([A.shape[0]]) v = u/tf.linalg.norm(u) T = lanczos_bidiag(A, m, starting_vector=v) e, v = tf.eigh(T) e = tf.reshape(e, [-1]) taus = tf.gather(v, [0], axis=1) taus = tf.reshape(taus, [-1]) Gamma = tf.dot(taus**2, e, axes=[0,0]) return Gamma