Esempio n. 1
0
def _value_and_gradient(fn, *args):
    """Calls `fn` and computes the gradient of the result wrt `arg`."""
    if tfe_context.executing_eagerly():
        v, g = tfe_backprop.val_and_grad_function(fn)(args)
    else:
        v = fn(*args)
        g = gradients_impl.gradients(v, args)
    return v, g
def _value_and_gradient(fn, *args):
  """Calls `fn` and computes the gradient of the result wrt `arg`."""
  if tfe_context.executing_eagerly():
    v, g = tfe_backprop.val_and_grad_function(fn)(args)
  else:
    v = fn(*args)
    g = gradients_impl.gradients(v, args)
  return v, g
Esempio n. 3
0
  def testNonEmptyParamsForValueAndGradFunction(self):
    def fn(a, b):
      return a * b
    val_and_grad_fn = backprop.val_and_grad_function(fn, params=[1])

    x = 2.0
    y = 3.0
    val, grads = val_and_grad_fn(x, y)
    self.assertAllClose(val, x * y)
    self.assertEqual(1, len(grads))
    self.assertAllEqual(grads[0], x)
Esempio n. 4
0
  def testEmptyParamsForValueAndGradFunction(self):
    def fn(a, b):
      return a * b
    val_and_grads_fn = backprop.val_and_grad_function(fn)

    x = 2.0
    y = 3.0
    val, (dx, dy) = val_and_grads_fn(x, y)
    self.assertAllClose(val, x * y)
    self.assertAllEqual(dx, y)
    self.assertAllEqual(dy, x)
Esempio n. 5
0
  def testNonEmptyParamsForValueAndGradFunction(self):
    def fn(a, b):
      return a * b
    val_and_grad_fn = backprop.val_and_grad_function(fn, params=[1])

    x = 2.0
    y = 3.0
    val, grads = val_and_grad_fn(x, y)
    self.assertAllClose(val, x * y)
    self.assertEqual(1, len(grads))
    self.assertAllEqual(grads[0], x)
Esempio n. 6
0
  def testEmptyParamsForValueAndGradFunction(self):
    def fn(a, b):
      return a * b
    val_and_grads_fn = backprop.val_and_grad_function(fn)

    x = 2.0
    y = 3.0
    val, (dx, dy) = val_and_grads_fn(x, y)
    self.assertAllClose(val, x * y)
    self.assertAllEqual(dx, y)
    self.assertAllEqual(dy, x)
    def testBasicFunctionalWithValue(self):
        def forward(a, b):
            mm = math_ops.matmul(a, b)
            return math_ops.reduce_sum(mm)

        aa = constant_op.constant([[1., 0.], [0., 1.]])
        bb = constant_op.constant([[1., 2.], [3., 4.]])
        val, (da, ) = backprop.val_and_grad_function(forward, ['a'])(aa, bb)
        self.assertAllEqual(
            da,
            math_ops.matmul(array_ops.ones_like(aa), array_ops.transpose(bb)))
        self.assertAllEqual(val, forward(aa, bb))
Esempio n. 8
0
  def testBasicFunctionalWithValue(self):

    def forward(a, b):
      mm = math_ops.matmul(a, b)
      return math_ops.reduce_sum(mm)

    aa = constant_op.constant([[1., 0.], [0., 1.]])
    bb = constant_op.constant([[1., 2.], [3., 4.]])
    val, (da,) = backprop.val_and_grad_function(forward, ['a'])(aa, bb)
    self.assertAllEqual(da,
                        math_ops.matmul(
                            array_ops.ones_like(aa),
                            array_ops.transpose(bb)))
    self.assertAllEqual(val, forward(aa, bb))
Esempio n. 9
0
  def testDifferentiatingFunctionThatReturnsNone(self):

    def fn(x, y):
      result = x*y  # pylint: disable=unused-variable

    x = constant_op.constant(1)
    y = constant_op.constant(2)

    loss_grads_fn = backprop.implicit_val_and_grad(fn)
    with self.assertRaisesRegexp(
        ValueError, 'Cannot differentiate a function that returns None; '
        'did you forget to return a value from fn?'):
      loss_grads_fn(x, y)

    val_and_grads_fn = backprop.val_and_grad_function(fn)
    with self.assertRaisesRegexp(
        ValueError, 'Cannot differentiate a function that returns None; '
        'did you forget to return a value from fn?'):
      val_and_grads_fn(x, y)
  def testDifferentiatingFunctionThatReturnsNone(self):

    def fn(x, y):
      result = x*y  # pylint: disable=unused-variable

    x = constant_op.constant(1)
    y = constant_op.constant(2)

    loss_grads_fn = backprop.implicit_val_and_grad(fn)
    with self.assertRaisesRegexp(
        ValueError, 'Cannot differentiate a function that returns None; '
        'did you forget to return a value from fn?'):
      loss_grads_fn(x, y)

    val_and_grads_fn = backprop.val_and_grad_function(fn)
    with self.assertRaisesRegexp(
        ValueError, 'Cannot differentiate a function that returns None; '
        'did you forget to return a value from fn?'):
      val_and_grads_fn(x, y)