def testMinimalRun(self): x = basic.TrainableVariable(shape=(), initializers={'w': tf.ones_initializer()})() x2 = x**2.0 min_value = 0.5 constr = optimization_constraints.OptimizationConstraints().add( x > min_value) self.assertFalse(constr._is_connected) loss = moving_average.MovingAverage()(x2 + tf.random.normal( (), stddev=1.0)) + constr() self.assertTrue(constr._is_connected) with self.assertRaisesRegexp(ValueError, 'Cannot add further constraints'): constr.add(x > min_value) with self.assertRaisesRegexp(ValueError, 'Cannot add further constraints'): constr.add_geq(x, min_value) with self.assertRaisesRegexp(ValueError, 'Cannot add further constraints'): constr.add_leq(min_value < x) opt = tf.train.AdamOptimizer(1e-2, beta1=0.0) update = opt.minimize(loss) with tf.control_dependencies([update]): x2 = tf.identity(x2) with tf.train.MonitoredSession() as sess: for _ in range(500): v, _ = sess.run([x2, update]) self.assertAllClose(v, min_value**2)
def testOpIdentification(self, mocked_add_leq, mocked_add_geq): calls_to_add_leq = [0] def mock_add_leq(*args, **kwargs): del args del kwargs calls_to_add_leq[0] += 1 mocked_add_leq.side_effect = mock_add_leq calls_to_add_geq = [0] def mock_add_geq(*args, **kwargs): del args del kwargs calls_to_add_geq[0] += 1 mocked_add_geq.side_effect = mock_add_geq cons = optimization_constraints.OptimizationConstraints() lhs = tf.zeros_like(1.0) rhs = tf.ones_like(1.0) self.assertEqual(calls_to_add_leq[0], 0) self.assertEqual(calls_to_add_geq[0], 0) cons.add(lhs < rhs) self.assertEqual(calls_to_add_leq[0], 1) self.assertEqual(calls_to_add_geq[0], 0) cons.add(lhs <= rhs) self.assertEqual(calls_to_add_leq[0], 2) self.assertEqual(calls_to_add_geq[0], 0) cons.add(lhs > rhs) self.assertEqual(calls_to_add_geq[0], 1) self.assertEqual(calls_to_add_leq[0], 2) cons.add(lhs >= rhs) self.assertEqual(calls_to_add_geq[0], 2) self.assertEqual(calls_to_add_leq[0], 2)
def testValidRangeOverrides(self): cons = optimization_constraints.OptimizationConstraints() lhs = tf.zeros_like(1.0) rhs = tf.ones_like(1.0) valid_range = (1.0, 2.0) cons.add(lhs < rhs, initializer=3.0, valid_range=valid_range)() with tf.train.MonitoredSession() as sess: lag_mul = sess.run(cons.lagrange_multipliers[0]) self.assertAllClose(lag_mul, valid_range[1])
def testLagrangeMultInit(self, initializer, exp_lag_mul): cons = optimization_constraints.OptimizationConstraints() lhs = tf.zeros_like(1.0) rhs = tf.ones_like(1.0) cons.add(lhs > rhs, initializer=initializer)() l = cons.lagrange_multipliers[0] with tf.train.MonitoredSession() as sess: lag_mul = sess.run(l) self.assertAllClose(lag_mul, exp_lag_mul)
def testRateOverrides(self, mocked_parametrized): mocked_parametrized.side_effect = ( lambda x, rate: scale_gradient.scale_gradient(x, -rate)) rate = 7.3 cons = optimization_constraints.OptimizationConstraints() lhs = tf.zeros_like(1.0) rhs = tf.ones_like(1.0) x = cons.add(lhs < rhs, rate=rate)() v = tf.all_variables()[0] dxdl = tf.gradients(x, v) with tf.train.MonitoredSession() as sess: grads = sess.run(dxdl) self.assertAllClose(grads[0], rate)