def test_bad_kernel_approximation(self, initializer, scale, exact_kernel_fn): """Approximation is bad when output dimension is small.""" # Two distinct inputs. x = tf.constant([[1.0, -1.0, 0.5]]) y = tf.constant([[-1.0, 1.0, 1.0]]) small_output_dim = 10 tf.compat.v1.set_random_seed(1234) # Initialize layer. rff_layer = kernel_layers.RandomFourierFeatures( output_dim=small_output_dim, kernel_initializer=initializer, scale=scale, name='random_fourier_features') # Apply layer to both inputs. output_x = math.sqrt(2.0 / small_output_dim) * rff_layer(x) output_y = math.sqrt(2.0 / small_output_dim) * rff_layer(y) # The inner products of the outputs (on inputs x and y) approximates the # real value of the RBF kernel but poorly since the output dimension of the # layer is small. exact_kernel_value = exact_kernel_fn(x, y) approx_kernel_value = kernelized_utils.inner_product(output_x, output_y) abs_error = tf.abs(exact_kernel_value - approx_kernel_value) if not tf.executing_eagerly(): with self.cached_session() as sess: keras_backend._initialize_variables(sess) abs_error_eval = sess.run([abs_error]) self.assertGreater(abs_error_eval[0][0], 0.01) self.assertLess(abs_error_eval[0][0], 0.5) else: self.assertGreater(abs_error, 0.01) self.assertLess(abs_error, 0.5)
def _assert_all_close(self, expected, actual): if not tf.executing_eagerly(): with self.cached_session() as sess: backend._initialize_variables(sess) self.assertAllClose(expected, actual) else: self.assertAllClose(expected, actual)
def init_restore_or_wait_for_variables(): """Initialize or restore variables or wait for variables to be initialized.""" session = backend._get_session() # pylint: disable=protected-access if not multi_worker_util.has_worker_context( ) or multi_worker_util.should_load_checkpoint(): # TODO(yuefengz): if checkpoints exist, restore from checkpoint. backend._initialize_variables(session) # pylint: disable=protected-access else: _wait_for_variable_initialization(session)
def _build(self, shape): self._shape = tf.TensorShape(shape) self._build_input_shape = self._shape # Create new state variables self._total = self.add_weight( name='total', shape=shape, initializer='zeros') self._count = self.add_weight( name='count', shape=shape, initializer='zeros') with tf.init_scope(): if not tf.executing_eagerly(): backend._initialize_variables(backend._get_session()) # pylint: disable=protected-access self._built = True
def _build(self, shape): self._shape = tf.TensorShape(shape) self._build_input_shape = self._shape # Create new state variables self._total = self.add_weight( name="total", shape=shape, initializer="zeros" ) self._count = self.add_weight( name="count", shape=shape, initializer="zeros" ) with tf.init_scope(): if not tf.executing_eagerly(): backend._initialize_variables(backend._get_session()) self._built = True
def init_restore_or_wait_for_variables(): """Initialize or restore variables or wait for variables to be initialized.""" backend._initialize_variables(backend._get_session()) # pylint: disable=protected-access
def init_restore_or_wait_for_variables(): """Initialize or restore variables or wait for variables to be initialized.""" backend._initialize_variables(backend._get_session())