Exemplo n.º 1
0
  def test_minimize_callback(self):
    """Callback should run at the end of every iteration."""
    variables = np.random.uniform(low=-1.0, high=1.0, size=[1])
    saved_objective = [None]
    saved_iteration = [None]
    iteration_count = [0]

    def callback(iteration, objective_value, variables):
      del variables

      def save(iteration, objective_value):
        saved_objective[0] = objective_value
        saved_iteration[0] = iteration
        iteration_count[0] += 1

      return tf.py_function(save, [iteration, objective_value], [])

    final_objective, _ = levenberg_marquardt.minimize(
        lambda x: x, variables, max_iterations=5, callback=callback)
    final_saved_objective = tf.py_function(lambda: saved_objective[0], [],
                                           tf.float64)
    final_saved_iteration = tf.py_function(lambda: saved_iteration[0], [],
                                           tf.int32)
    final_iteration_count = tf.py_function(lambda: iteration_count[0], [],
                                           tf.int32)

    with self.subTest(name="objective_value"):
      self.assertAllClose(final_objective, final_saved_objective)

    with self.subTest(name="iterations"):
      self.assertAllEqual(final_saved_iteration, final_iteration_count)
Exemplo n.º 2
0
  def test_minimize_preset(self, residuals, variables, max_iterations,
                           final_objective_value, final_variables):
    """Tests the output of the minimization for some presets."""
    objective_value, output_variables = levenberg_marquardt.minimize(
        residuals, variables, max_iterations)

    with self.subTest(name="objective_value"):
      self.assertAllClose(objective_value, final_objective_value)

    with self.subTest(name="variables"):
      for output_variable, final_variable in zip(output_variables,
                                                 final_variables):
        self.assertAllClose(output_variable, final_variable)
Exemplo n.º 3
0
  def test_minimize_linear_residuals_random(self):
    """Optimizing linear residuals should give the minimum in 1 step."""
    tensor_size = np.random.randint(1, 3)
    tensor_shape = np.random.randint(1, 10, size=(tensor_size)).tolist()
    variables = np.random.uniform(low=-1.0, high=1.0, size=tensor_shape)
    objective_value, variables = levenberg_marquardt.minimize(
        lambda x: x, variables, max_iterations=1)

    with self.subTest(name="objective_value"):
      self.assertAllClose(objective_value, tf.zeros_like(objective_value))

    with self.subTest(name="variables"):
      for variable in variables:
        self.assertAllClose(variable, tf.zeros_like(variable))
    def test_minimize_ill_conditioned_not_raised(self):
        """Optimizing an ill conditioned problem should not raise an exception."""
        if not tf.executing_eagerly():
            return

        def f1(x, y):
            return x * y * 10000.0

        def f2(x, y):
            return x * y * 0.0001

        x = (1., )
        y = (1., )
        try:
            self.evaluate(
                levenberg_marquardt.minimize(residuals=(f1, f2),
                                             variables=(x, y),
                                             max_iterations=1,
                                             regularizer=1e-20))
        except Exception as e:  # pylint: disable=broad-except
            self.fail("Exception raised: %s" % str(e))
Exemplo n.º 5
0
 def optimize(*variables):
   levenberg_marquardt.minimize(lambda x: x, variables, max_iterations)