예제 #1
0
    def test_unconstrained(self):

        dimension = 5
        x = variables.VariableV1(array_ops.zeros(dimension))
        optimizer = external_optimizer.ScipyOptimizerInterface(
            self._objective(x))

        with self.cached_session() as sess:
            sess.run(variables.global_variables_initializer())
            optimizer.minimize(sess)

            self.assertAllClose(np.ones(dimension), sess.run(x))
예제 #2
0
    def test_optimizer_kwargs(self):
        # Checks that the 'method' argument is stil present
        # after running optimizer.minimize().
        # Bug reference: b/64065260
        vector_initial_value = [7., 7.]
        vector = variables.VariableV1(vector_initial_value, 'vector')
        loss = math_ops.reduce_sum(math_ops.square(vector))

        optimizer = external_optimizer.ScipyOptimizerInterface(loss,
                                                               method='SLSQP')

        with self.cached_session() as sess:
            sess.run(variables.global_variables_initializer())
            optimizer.minimize(sess)
            method = optimizer.optimizer_kwargs.get('method')
            self.assertEqual('SLSQP', method)
예제 #3
0
    def test_vector_bounds(self):
        vector_initial_value = [7., 7.]
        vector = variables.VariableV1(vector_initial_value, 'vector')

        # Make norm as small as possible.
        loss = math_ops.reduce_sum(math_ops.square(vector))

        var_to_bounds = {vector: ([None, 2.], None)}

        optimizer = external_optimizer.ScipyOptimizerInterface(
            loss, var_to_bounds=var_to_bounds)

        with self.cached_session() as sess:
            sess.run(variables.global_variables_initializer())
            optimizer.minimize(sess)
            self.assertAllClose([0., 2.], sess.run(vector))
예제 #4
0
    def test_scalar_bounds(self):
        vector_initial_value = [7., 7.]
        vector = variables.VariableV1(vector_initial_value, 'vector')

        # Make norm as small as possible.
        loss = math_ops.reduce_sum(math_ops.square(vector))

        # Make the minimum value of each component be 1.
        var_to_bounds = {vector: (1., np.infty)}

        optimizer = external_optimizer.ScipyOptimizerInterface(
            loss, var_to_bounds=var_to_bounds)

        with self.cached_session() as sess:
            sess.run(variables.global_variables_initializer())
            optimizer.minimize(sess)
            self.assertAllClose(np.ones(2), sess.run(vector))
예제 #5
0
    def _test_optimization_method(self,
                                  method,
                                  options,
                                  rtol=1e-5,
                                  atol=1e-5,
                                  dimension=5):
        x = variables.VariableV1(array_ops.zeros(dimension))
        optimizer = external_optimizer.ScipyOptimizerInterface(
            self._objective(x), method=method, options=options)

        with self.cached_session() as sess:
            sess.run(variables.global_variables_initializer())
            optimizer.minimize(sess)

            self.assertAllClose(np.ones(dimension),
                                sess.run(x),
                                rtol=rtol,
                                atol=atol)
예제 #6
0
    def test_nonlinear_programming(self):
        vector_initial_value = [7., 7.]
        vector = variables.VariableV1(vector_initial_value, 'vector')

        # Make norm as small as possible.
        loss = math_ops.reduce_sum(math_ops.square(vector))
        # Ensure y = 1.
        equalities = [vector[1] - 1.]
        # Ensure x >= 1. Thus optimum should be at (1, 1).
        inequalities = [vector[0] - 1.]

        optimizer = external_optimizer.ScipyOptimizerInterface(
            loss,
            equalities=equalities,
            inequalities=inequalities,
            method='SLSQP')

        with self.cached_session() as sess:
            sess.run(variables.global_variables_initializer())
            optimizer.minimize(sess)
            self.assertAllClose(np.ones(2), sess.run(vector))
예제 #7
0
    def test_callbacks(self):
        vector_val = np.array([7., -2.], dtype=np.float32)
        vector = variables.VariableV1(vector_val, 'vector')

        minimum_location_val = np.arange(2)
        minimum_location = constant_op.constant(minimum_location_val,
                                                dtype=dtypes.float32)

        loss = math_ops.reduce_sum(
            math_ops.square(vector - minimum_location)) / 2.
        loss_val_first = ((vector_val - minimum_location_val)**2).sum() / 2.

        optimizer = external_optimizer.ScipyOptimizerInterface(loss,
                                                               method='SLSQP')

        with self.cached_session() as sess:
            sess.run(variables.global_variables_initializer())

            initial_vector_val = sess.run(vector)

            extra_fetches = [loss]

            step_callback = test.mock.Mock()
            loss_callback = test.mock.Mock()

            optimizer.minimize(sess,
                               fetches=extra_fetches,
                               loss_callback=loss_callback,
                               step_callback=step_callback)

            loss_val_last = sess.run(loss)

            call_first = test.mock.call(loss_val_first)
            call_last = test.mock.call(loss_val_last)
            loss_calls = [call_first, call_last]
            loss_callback.assert_has_calls(loss_calls, any_order=True)

            args, _ = step_callback.call_args
            self.assertAllClose(minimum_location_val, args[0])