Ejemplo n.º 1
0
def noise_accumulator_test_template(test_case, model, num_steps):
    """Tests `model`'s transition_power_noise_accumulator."""
    transition_matrix = ops.convert_to_tensor(model.get_state_transition(),
                                              dtype=model.dtype)
    noise_transform = ops.convert_to_tensor(model.get_noise_transform(),
                                            dtype=model.dtype)
    state_dimension = tensor_shape.dimension_value(transition_matrix.shape[0])
    state_noise_dimension = tensor_shape.dimension_value(
        noise_transform.shape[1])
    gen_noise_addition = math_utils.sign_magnitude_positive_definite(
        raw=random_ops.random_normal(
            shape=[state_noise_dimension, state_noise_dimension],
            dtype=model.dtype))
    gen_starting_noise = math_utils.sign_magnitude_positive_definite(
        random_ops.random_normal(shape=[state_dimension, state_dimension],
                                 dtype=model.dtype))
    starting_noise = array_ops.placeholder(
        shape=[state_dimension, state_dimension], dtype=model.dtype)
    step_number = array_ops.placeholder(shape=[], dtype=dtypes.int64)
    starting_transitioned = math_ops.matmul(math_ops.matmul(
        transition_matrix, starting_noise),
                                            transition_matrix,
                                            adjoint_b=True)
    with test_case.test_session():
        evaled_starting_noise = gen_starting_noise.eval()
        current_starting_noise_transitioned = evaled_starting_noise
        current_noise = evaled_starting_noise
        evaled_noise_addition = gen_noise_addition.eval()
        evaled_noise_addition_transformed = math_ops.matmul(
            math_ops.matmul(noise_transform, evaled_noise_addition),
            noise_transform,
            adjoint_b=True).eval()
        model.state_transition_noise_covariance = evaled_noise_addition
        model._window_initializer(  # pylint: disable=protected-access
            times=math_ops.range(num_steps + 1)[..., None],
            state=(None, None, 0))
        model_update = model.transition_power_noise_accumulator(
            num_steps=step_number)
        for iteration_number in range(num_steps):
            model_new_noise = model_update.eval(
                feed_dict={step_number: iteration_number})
            test_case.assertAllClose(
                current_noise,
                model_new_noise + current_starting_noise_transitioned,
                rtol=1e-8 if current_noise.dtype == numpy.float64 else 1e-3)
            current_starting_noise_transitioned = starting_transitioned.eval(
                feed_dict={
                    starting_noise: current_starting_noise_transitioned
                })
            current_noise = (starting_transitioned.eval(
                feed_dict={starting_noise: current_noise}) +
                             evaled_noise_addition_transformed)
Ejemplo n.º 2
0
def noise_accumulator_test_template(test_case, model, num_steps):
  """Tests `model`'s transition_power_noise_accumulator."""
  transition_matrix = ops.convert_to_tensor(
      model.get_state_transition(), dtype=model.dtype)
  noise_transform = ops.convert_to_tensor(
      model.get_noise_transform(), dtype=model.dtype)
  state_dimension = transition_matrix.get_shape()[0].value
  state_noise_dimension = noise_transform.get_shape()[1].value
  gen_noise_addition = math_utils.sign_magnitude_positive_definite(
      raw=random_ops.random_normal(
          shape=[state_noise_dimension, state_noise_dimension],
          dtype=model.dtype))
  gen_starting_noise = math_utils.sign_magnitude_positive_definite(
      random_ops.random_normal(
          shape=[state_dimension, state_dimension], dtype=model.dtype))
  starting_noise = array_ops.placeholder(
      shape=[state_dimension, state_dimension], dtype=model.dtype)
  step_number = array_ops.placeholder(shape=[], dtype=dtypes.int64)
  starting_transitioned = math_ops.matmul(
      math_ops.matmul(transition_matrix, starting_noise),
      transition_matrix,
      adjoint_b=True)
  with test_case.test_session():
    evaled_starting_noise = gen_starting_noise.eval()
    current_starting_noise_transitioned = evaled_starting_noise
    current_noise = evaled_starting_noise
    evaled_noise_addition = gen_noise_addition.eval()
    evaled_noise_addition_transformed = math_ops.matmul(
        math_ops.matmul(noise_transform, evaled_noise_addition),
        noise_transform,
        adjoint_b=True).eval()
    model.state_transition_noise_covariance = evaled_noise_addition
    model._window_initializer(  # pylint: disable=protected-access
        times=math_ops.range(num_steps + 1)[..., None], state=(None, None, 0))
    model_update = model.transition_power_noise_accumulator(
        num_steps=step_number)
    for iteration_number in range(num_steps):
      model_new_noise = model_update.eval(
          feed_dict={step_number: iteration_number})
      test_case.assertAllClose(
          current_noise,
          model_new_noise + current_starting_noise_transitioned,
          rtol=1e-8 if current_noise.dtype == numpy.float64 else 1e-3)
      current_starting_noise_transitioned = starting_transitioned.eval(
          feed_dict={starting_noise: current_starting_noise_transitioned})
      current_noise = (
          starting_transitioned.eval(
              feed_dict={starting_noise: current_noise})
          + evaled_noise_addition_transformed)
Ejemplo n.º 3
0
 def test_sign_magnitude_positive_definite(self):
   for dtype in [dtypes.float32, dtypes.float64]:
     with self.cached_session():
       matrix_tensor = math_utils.sign_magnitude_positive_definite(
           raw=constant_op.constant([[-1., -2.], [3., 4.]], dtype=dtype),
           off_diagonal_scale=constant_op.constant(-1., dtype=dtype),
           overall_scale=constant_op.constant(1., dtype=dtype))
       matrix_evaled = matrix_tensor.eval()
       self.assertAllClose(matrix_evaled, matrix_evaled.T)
       self.assertTrue(numpy.all(numpy.linalg.eigvals(matrix_evaled) > 0))
Ejemplo n.º 4
0
 def test_zero_size_matrix(self):
   raw = numpy.zeros([0, 0])
   with self.cached_session():
     constructed = math_utils.sign_magnitude_positive_definite(raw=raw).eval()
   self.assertEqual((0, 0), constructed.shape)