def test_jit(self): f_jax = jax.jit(lambda x: jnp.sin(jnp.cos(x))) self.ConvertAndCompare(f_jax, 0.7)
def f2(x): y = 2 * np.sin(x) z = api.remat(lambda x: np.cos(x) * np.sin(y))(x) return z
# Create the loss function: ur = egrad(u, 1) u2r = egrad(ur, 1) u2th = egrad(egrad(u, 2), 2) L = lambda xi: u2r(xi, *x) + 1. / x[0] * ur(xi, *x) + 1. / x[0]**2 * u2th( xi, *x) # Solve the problem: xi = np.zeros(H(*x).shape[1]) xi, it, time = NLLS(xi, L, timer=True) # Print out statistics: print("Solution time: {0} seconds".format(time)) # Plot the solution: R, Th = np.meshgrid(np.linspace(r0, rf, 50), np.linspace(th0, thf, 200)) dark = (R.flatten(), Th.flatten()) X = R * np.cos(Th) Y = R * np.sin(Th) U = u(xi, *dark).reshape((200, 50)) p = MakePlot("x", "y", zlabs="u(x,y,g(x,y))") p.Surface(x=X, y=Y, z=U, showscale=False) p.show() # Plot the error err = np.abs(realSoln(R, Th) - U) p = MakePlot("x", "y", zlabs="Error") p.Surface(x=X, y=Y, z=err, showscale=False) p.show()
def f(c, a): a1, a2 = a c1, c2 = c b = np.sum(np.cos(a1)) * np.sum(np.tan(c2 * a2)) c = c1 * np.sin(np.sum(a1 * a2)), c2 * np.cos(np.sum(a1)) return c, b
def _cosine_interpolate(start: float, end: float, pct: float): return end + (start - end) / 2.0 * (jnp.cos(jnp.pi * pct) + 1)
def bar(y): return np.cos(y) * x
def schedule(step): t = step / total_steps return 0.5 * init_lr * (1 + jnp.cos(t * onp.pi))
def standard_students_t_4_icdf(u): """Inverse cumulative distribution function of standard Student's t with dof = 4.""" sqrt_α = np.sqrt(4 * u * (1 - u)) return 2 * np.sign(u - 0.5) * np.sqrt( np.cos(np.arccos(sqrt_α) / 3) / sqrt_α - 1)
def ackley_1d(x, y=0): out = (-20 * jnp.exp(-0.2 * jnp.sqrt(0.5 * (x**2 + y**2))) - jnp.exp(0.5 * (jnp.cos(2 * jnp.pi * x) + jnp.cos(2 * jnp.pi * y))) + jnp.e + 20) return out
def f1(x, y): return np.sin(x) * np.cos(y) * np.sin(x) * np.cos(y)
def h(x): return np.sin(np.cos(x))
def f(z): return np.cos(np.linalg.norm(2 * z))
def f(z): return np.sum(np.cos(np.abs(z)))
def test_variable_input(self): f_jax = lambda x: jnp.sin(jnp.cos(x)) f_tf = jax2tf.convert(f_jax) v = tf.Variable(0.7) self.assertIsInstance(f_tf(v), tf.Tensor) self.assertAllClose(f_jax(0.7), f_tf(v))
def schedule(step): t = jnp.minimum(step / burnin_steps, 1.) coef = (1 + jnp.cos(t * onp.pi)) * 0.5 return coef * init_lr + (1 - coef) * final_lr
def test_function(self): f_jax = jax.jit(lambda x: jnp.sin(jnp.cos(x))) self.ConvertAndCompare(f_jax, 0.7, with_function=True)
def schedule(step): t = jnp.maximum(step - burnin_steps - 1, 0.) t = (t % cycle_length) / cycle_length return 0.5 * init_lr * (1 + jnp.cos(t * onp.pi))
def schedule(count): count = jnp.minimum(count, decay_steps) cosine_decay = 0.5 * (1 + jnp.cos(jnp.pi * count / decay_steps)) decayed = (1 - alpha) * cosine_decay + alpha return init_value * decayed
def log_prob(self, value): return -(jnp.log(2 * jnp.pi) + jnp.log(i0e(self.concentration))) + \ self.concentration * (jnp.cos((value - self.loc) % (2 * jnp.pi)) - 1)
def fun_with_call_closure(x): def foo(y, z): return (x * x) * np.sin(y) * z return call(foo, x, np.cos(x)) + x
def test_function(self): f_jax = jax.jit(lambda x: jnp.sin(jnp.cos(x))) self.ConvertAndCompare(f_jax, jnp.float_(0.7))
def foo(x): return np.sin(x) + np.cos(x)
def test_variable_input(self): f_jax = lambda x: jnp.sin(jnp.cos(x)) f_tf = jax2tf.convert(f_jax) v = tf.Variable(0.7, dtype=dtypes.canonicalize_dtype(jnp.float_)) self.assertIsInstance(f_tf(v), tf.Tensor) self.assertAllClose(f_jax(0.7), f_tf(v))
def f(c, a): b = np.sin(c * np.sum(np.cos(d * a))) c = 0.9 * np.cos(d * np.sum(np.sin(c * a))) return c, b
def cosine_decay(lr, step, total_steps): ratio = jnp.maximum(0., step / total_steps) mult = 0.5 * (1. + jnp.cos(jnp.pi * ratio)) return mult * lr
def fn(a): return np.cos(a)
def test_basics(self): f_jax = lambda x: jnp.sin(jnp.cos(x)) _, res_tf = self.ConvertAndCompare(f_jax, 0.7) self.assertIsInstance(res_tf, tf.Tensor)
def f_callee(x): return jnp.cos(x)
def f1(x): y = 2 * np.sin(x) z = np.cos(x) * np.sin(y) return z