(xs, fx)
return df_dx
Computes df/dx matrix.

  We assume x and fx are both batched, so the shape of the Jacobian is:
  [fx.shape[0]] + fx.shape[1:] + xs.shape[1:]

  This function computes the grads inside a TF loop so that we don't
  end up storing many extra copies of the function we are taking the
  Jacobian of.

  Args:
    xs: input tensor(s) of arbitrary shape.
    fx: f(x) tensor of arbitrary shape.

  Returns:
    df/dx tensor of shape [fx.shape[0], fx.shape[1], xs.shape[1]].
Computes df/dx matrix.
Computes df / dx matrix .
def compute_jacobian(xs, fx):
  """Computes df/dx matrix.

  We assume x and fx are both batched, so the shape of the Jacobian is:
  [fx.shape[0]] + fx.shape[1:] + xs.shape[1:]

  This function computes the grads inside a TF loop so that we don't
  end up storing many extra copies of the function we are taking the
  Jacobian of.

  Args:
    xs: input tensor(s) of arbitrary shape.
    fx: f(x) tensor of arbitrary shape.

  Returns:
    df/dx tensor of shape [fx.shape[0], fx.shape[1], xs.shape[1]].
  """
  # Declares an iterator and tensor array loop variables for the gradients.
  n = fx.get_shape().as_list()[1]
  loop_vars = [tf.constant(0, tf.int32), tf.TensorArray(xs.dtype, n)]

  def accumulator(j, result):
    return (j + 1, result.write(j, tf.gradients(fx[:, j], xs)[0]))

  # Iterates over all elements of the gradient and computes all partial
  # derivatives.
  _, df_dxs = tf.while_loop(lambda j, _: j < n, accumulator, loop_vars)

  df_dx = df_dxs.stack()
  df_dx = tf.transpose(df_dx, perm=[1, 0, 2])

  return df_dx
def compute_jacobian ( xs , fx ) : # Declares an iterator and tensor array loop variables for the gradients. n = fx . get_shape ( ) . as_list ( ) [ 1 ] loop_vars = [ tf . constant ( 0 , tf . int32 ) , tf . TensorArray ( xs . dtype , n ) ] def accumulator ( j , result ) : return ( j + 1 , result . write ( j , tf . gradients ( fx [ : , j ] , xs ) [ 0 ] ) ) # Iterates over all elements of the gradient and computes all partial # derivatives. _ , df_dxs = tf . while_loop ( lambda j , _ : j < n , accumulator , loop_vars ) df_dx = df_dxs . stack ( ) df_dx = tf . transpose ( df_dx , perm = [ 1 , 0 , 2 ] ) return df_dx