示例#1
0
  def test_custom_root_errors(self):
    with self.assertRaisesRegex(TypeError, re.escape("f() output pytree")):
      lax.custom_root(lambda x: (x, x), 0.0, lambda f, x: x, lambda f, x: x)
    with self.assertRaisesRegex(TypeError, re.escape("solve() output pytree")):
      lax.custom_root(lambda x: x, 0.0, lambda f, x: (x, x), lambda f, x: x)

    def dummy_root_usage(x):
      f = lambda y: x - y
      return lax.custom_root(f, 0.0, lambda f, x: x, lambda f, x: (x, x))

    with self.assertRaisesRegex(
        TypeError, re.escape("tangent_solve() output pytree")):
      api.jvp(dummy_root_usage, (0.0,), (0.0,))
示例#2
0
 def linear_solve(a, b):
   f = lambda x: np.dot(a, x) - b
   factors = jsp.linalg.cho_factor(a)
   cho_solve = lambda f, b: jsp.linalg.cho_solve(factors, b)
   def pos_def_solve(g, b):
     return lax.custom_linear_solve(g, b, cho_solve, symmetric=True)
   return lax.custom_root(f, b, cho_solve, pos_def_solve)
示例#3
0
        def root_aux(a, b):
            f = lambda x: high_precision_dot(a, x) - b
            factors = jsp.linalg.cho_factor(a)
            cho_solve = lambda f, b: (jsp.linalg.cho_solve(factors, b),
                                      orig_aux)

            def pos_def_solve(g, b):
                # prune aux to allow use as tangent_solve
                cho_solve_noaux = lambda f, b: cho_solve(f, b)[0]
                return lax.custom_linear_solve(g,
                                               b,
                                               cho_solve_noaux,
                                               symmetric=True)

            return lax.custom_root(f,
                                   b,
                                   cho_solve,
                                   pos_def_solve,
                                   has_aux=True)
示例#4
0
 def dummy_root_usage(x):
   f = lambda y: x - y
   return lax.custom_root(f, 0.0, lambda f, x: x, lambda f, x: (x, x))
示例#5
0
 def linear_solve(a, b):
   f = lambda y: high_precision_dot(a, y) - b
   x0 = np.zeros_like(b)
   solution = np.linalg.solve(a, b)
   oracle = lambda func, x0: solution
   return lax.custom_root(f, x0, oracle, vector_solve)
示例#6
0
 def sqrt_cubed(x, tangent_solve=scalar_solve):
   f = lambda y: y ** 2. - np.array(x) ** 3.
   return lax.custom_root(f, 0.0, binary_search, tangent_solve)
示例#7
0
 def f3():
     return lax.custom_root(err, 0., solve, solve)
示例#8
0
 def f2():
     return lax.custom_root(g, 0., solve, err)
示例#9
0
 def f1():
     return lax.custom_root(g, 0., err, solve)
示例#10
0
 def sqrt_cubed(x, tangent_solve=scalar_solve):
     f = lambda y: y**2 - x**3
     return lax.custom_root(f, 0.0, binary_search, tangent_solve)
示例#11
0
 def sqrt_cubed(x, tangent_solve=scalar_solve):
     f = lambda y: y**2 - x**3
     # Note: Nonzero derivative at x0 required for newton_raphson
     return lax.custom_root(f, 1.0, solve_method, tangent_solve)
示例#12
0
 def nonlinear_solve(y):
     f = lambda x: nonlinear_func(x, y)
     x0 = -jnp.ones_like(y)
     return lax.custom_root(f, x0, newton_raphson, tangent_solve)