Exemplo n.º 1
0
  def test_raises_exception_on_invalid_params(self):
    with self.assertRaisesRegex(tf.errors.InvalidArgumentError,
                                'Argument `high` must be greater than `low`'):
      b = tfb.SoftClip(5., 3., validate_args=True)

    with self.assertRaisesOpError('Argument `high` must be greater than `low`'):
      low = tf.Variable(0.)
      self.evaluate(low.initializer)
      b = tfb.SoftClip(low, 3., validate_args=True)
      with tf.control_dependencies([low.assign(5.)]):
        self.evaluate(b.forward(4.))
Exemplo n.º 2
0
 def test_is_nearly_identity_within_range(self):
   xs = tf.convert_to_tensor(np.linspace(-3., 3., 20), dtype=self.dtype)
   b = tfb.SoftClip(tf.convert_to_tensor(-5., self.dtype),
                    tf.convert_to_tensor(5., self.dtype),
                    hinge_softness=0.01)
   ys = self.evaluate(b.forward(xs))
   self.assertAllClose(ys, xs)
   xs_inverted = self.evaluate(b.inverse(ys))
   self.assertAllClose(ys, xs_inverted)
Exemplo n.º 3
0
  def test_raises_exception_on_invalid_input(self):
    b = tfb.SoftClip(3., 5., validate_args=True)
    with self.assertRaisesRegex(tf.errors.InvalidArgumentError,
                                'Input must be greater than `low`'):
      b.inverse(2.)

    with self.assertRaisesOpError('Input must be less than `high`'):
      dynamic_y = tf1.placeholder_with_default(input=7., shape=[])
      x = b.inverse(dynamic_y)
      self.evaluate(x)
Exemplo n.º 4
0
 def test_is_nearly_identity_within_range(self, low, high):
   xs = tf.convert_to_tensor(np.linspace(-3., 3., 20), dtype=self.dtype)
   if low is not None:
     low = tf.convert_to_tensor(low, self.dtype)
   if high is not None:
     high = tf.convert_to_tensor(high, self.dtype)
   b = tfb.SoftClip(low, high, hinge_softness=0.01)
   ys = self.evaluate(b.forward(xs))
   self.assertAllClose(ys, xs)
   xs_inverted = self.evaluate(b.inverse(ys))
   self.assertAllClose(ys, xs_inverted)
Exemplo n.º 5
0
  def test_constraints_are_satisfied(self, low, high, hinge_softness):
    xs = tf.convert_to_tensor(
        [-1e6, -100., -3., 0., 1.3, 4., 123., 1e6], self.dtype)

    low_tensor = (tf.convert_to_tensor(low, self.dtype)
                  if low is not None else low)
    high_tensor = (tf.convert_to_tensor(high, self.dtype)
                   if high is not None else high)
    hinge_softness = tf.convert_to_tensor(hinge_softness, self.dtype)
    b = tfb.SoftClip(low_tensor, high_tensor, hinge_softness)
    ys = self.evaluate(b.forward(xs))
    if low is not None:
      self.assertAllGreaterEqual(ys, low)
      self.assertAllClose(ys[0], low)
    if high is not None:
      self.assertAllLessEqual(ys, high)
      self.assertAllClose(ys[-1], high)
Exemplo n.º 6
0
 def test_variable_gradients(self):
   b = tfb.SoftClip(low=tf.Variable(2.), high=tf.Variable(6.))
   with tf.GradientTape() as tape:
     y = b.forward(.1)
   self.assertAllNotNone(tape.gradient(y, b.trainable_variables))