本文整理汇总了Python中autograd.grad方法的典型用法代码示例。如果您正苦于以下问题:Python autograd.grad方法的具体用法?Python autograd.grad怎么用?Python autograd.grad使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类autograd
的用法示例。
在下文中一共展示了autograd.grad方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _test_gradgrad_array
# 需要导入模块: import autograd [as 别名]
# 或者: from autograd import grad [as 别名]
def _test_gradgrad_array(func, optimized, *args):
"""Test gradients of functions with NumPy-compatible signatures."""
def tangent_func():
func.__globals__['np'] = np
df = tangent.grad(func, optimized=optimized, verbose=True)
ddf = tangent.grad(df, optimized=optimized, verbose=True)
return ddf(*args)
def reference_func():
func.__globals__['np'] = ag_np
return ag_grad(ag_grad(func))(*args)
def backup_reference_func():
return utils.numeric_grad(utils.numeric_grad(func))(*args)
utils.assert_result_matches_reference(
tangent_func, reference_func, backup_reference_func,
tolerance=1e-2) # extra loose bounds for 2nd order grad
示例2: test_logistic_regression
# 需要导入模块: import autograd [as 别名]
# 或者: from autograd import grad [as 别名]
def test_logistic_regression(motion, optimized):
func = logistic_regression
w = np.random.randn(3, 5)
b = np.random.randn(5)
input_ = np.random.rand(3)
label = np.zeros(5)
label[1] = 1
func.__globals__['np'] = np
df = tangent.autodiff(
func,
wrt=(2, 3),
motion=motion,
optimized=optimized,
verbose=True,
input_derivative=INPUT_DERIVATIVE.DefaultOne)
dw, db = df(input_, label, w, b)
func.__globals__['np'] = ag_np
ag_dw = ag_grad(func, argnum=2)(input_, label, w, b)
ag_db = ag_grad(func, argnum=3)(input_, label, w, b)
assert np.allclose(ag_dw, dw)
assert np.allclose(ag_db, db)
示例3: test_inlining_contextmanager
# 需要导入模块: import autograd [as 别名]
# 或者: from autograd import grad [as 别名]
def test_inlining_contextmanager(motion, optimized, a):
func = inlining_contextmanager
func = tangent.tangent(func)
func.__globals__['np'] = np
df = tangent.autodiff(
func,
motion=motion,
optimized=optimized,
verbose=True,
input_derivative=INPUT_DERIVATIVE.DefaultOne)
dx = df(a)
func.__globals__['np'] = ag_np
df_ag = ag_grad(func)
df_ag(a)
assert np.allclose(dx, 2.9 * a**2)
示例4: rearrange_dict_grad
# 需要导入模块: import autograd [as 别名]
# 或者: from autograd import grad [as 别名]
def rearrange_dict_grad(fun):
"""
Decorator that allows us to save memory on the forward pass,
by precomputing the gradient
"""
@primitive
def wrapped_fun_helper(xdict, dummy):
## ag.value_and_grad() to avoid second forward pass
## ag.checkpoint() ensures hessian gets properly checkpointed
val, grad = ag.checkpoint(ag.value_and_grad(fun))(xdict)
assert len(val.shape) == 0
dummy.cache = grad
return val
def wrapped_fun_helper_grad(ans, xdict, dummy):
def grad(g):
#print("foo")
return {k:g*v for k,v in dummy.cache.items()}
return grad
defvjp(wrapped_fun_helper, wrapped_fun_helper_grad, None)
@functools.wraps(fun)
def wrapped_fun(xdict):
return wrapped_fun_helper(ag.dict(xdict), lambda:None)
return wrapped_fun
示例5: check_gradient
# 需要导入模块: import autograd [as 别名]
# 或者: from autograd import grad [as 别名]
def check_gradient(f, x):
print(x, "\n", f(x))
print("# grad2")
grad2 = Gradient(f)(x)
print("# building grad1")
g = grad(f)
print("# computing grad1")
grad1 = g(x)
print("gradient1\n", grad1, "\ngradient2\n", grad2)
np.allclose(grad1, grad2)
# check Hessian vector product
y = np.random.normal(size=x.shape)
gdot = lambda u: np.dot(g(u), y)
hess1, hess2 = grad(gdot)(x), Gradient(gdot)(x)
print("hess1\n", hess1, "\nhess2\n", hess2)
np.allclose(hess1, hess2)
示例6: __init__
# 需要导入模块: import autograd [as 别名]
# 或者: from autograd import grad [as 别名]
def __init__(self, next_state, running_cost, final_cost,
umax, state_dim, pred_time=50):
self.pred_time = pred_time
self.umax = umax
self.v = [0.0 for _ in range(pred_time + 1)]
self.v_x = [np.zeros(state_dim) for _ in range(pred_time + 1)]
self.v_xx = [np.zeros((state_dim, state_dim)) for _ in range(pred_time + 1)]
self.f = next_state
self.lf = final_cost
self.lf_x = grad(self.lf)
self.lf_xx = jacobian(self.lf_x)
self.l_x = grad(running_cost, 0)
self.l_u = grad(running_cost, 1)
self.l_xx = jacobian(self.l_x, 0)
self.l_uu = jacobian(self.l_u, 1)
self.l_ux = jacobian(self.l_u, 0)
self.f_x = jacobian(self.f, 0)
self.f_u = jacobian(self.f, 1)
self.f_xx = jacobian(self.f_x, 0)
self.f_uu = jacobian(self.f_u, 1)
self.f_ux = jacobian(self.f_u, 0)
示例7: compute_grad
# 需要导入模块: import autograd [as 别名]
# 或者: from autograd import grad [as 别名]
def compute_grad(objective_fn, x, grad_fn=None):
r"""Compute gradient of the objective_fn at the point x.
Args:
objective_fn (function): the objective function for optimization
x (array): NumPy array containing the current values of the variables to be updated
grad_fn (function): Optional gradient function of the
objective function with respect to the variables ``x``.
If ``None``, the gradient function is computed automatically.
Returns:
array: NumPy array containing the gradient :math:`\nabla f(x^{(t)})`
"""
if grad_fn is not None:
g = grad_fn(x) # just call the supplied grad function
else:
# default is autograd
g = autograd.grad(objective_fn)(x) # pylint: disable=no-value-for-parameter
return g
示例8: apply_grad
# 需要导入模块: import autograd [as 别名]
# 或者: from autograd import grad [as 别名]
def apply_grad(self, grad, x):
r"""Update the variables x to take a single optimization step. Flattens and unflattens
the inputs to maintain nested iterables as the parameters of the optimization.
Args:
grad (array): The gradient of the objective
function at point :math:`x^{(t)}`: :math:`\nabla f(x^{(t)})`
x (array): the current value of the variables :math:`x^{(t)}`
Returns:
array: the new values :math:`x^{(t+1)}`
"""
x_flat = _flatten(x)
grad_flat = _flatten(grad)
x_new_flat = [e - self._stepsize * g for g, e in zip(grad_flat, x_flat)]
return unflatten(x_new_flat, x)
示例9: test_rotation_gradient
# 需要导入模块: import autograd [as 别名]
# 或者: from autograd import grad [as 别名]
def test_rotation_gradient(self, theta, tol):
"""Tests that the automatic gradient of a phase space rotation is correct."""
def circuit(y):
qml.Displacement(alpha, 0., wires=[0])
qml.Rotation(y, wires=[0])
return qml.expval(qml.X(0))
dev = qml.device('default.gaussian', wires=1)
circuit = to_autograd(QubitQNode(circuit, dev))
grad_fn = autograd.grad(circuit)
autograd_val = grad_fn(theta)
# qfunc evalutes to hbar * alpha * cos(theta)
manualgrad_val = - hbar * alpha * np.sin(theta)
assert autograd_val == pytest.approx(manualgrad_val, abs=tol)
示例10: test_displacement_gradient
# 需要导入模块: import autograd [as 别名]
# 或者: from autograd import grad [as 别名]
def test_displacement_gradient(self, mag, theta, tol):
"""Tests that the automatic gradient of a phase space displacement is correct."""
def circuit(r, phi):
qml.Displacement(r, phi, wires=[0])
return qml.expval(qml.X(0))
dev = qml.device('default.gaussian', wires=1)
circuit = to_autograd(CVQNode(circuit, dev))
grad_fn = autograd.grad(circuit)
#alpha = mag * np.exp(1j * theta)
autograd_val = grad_fn(mag, theta)
# qfunc evalutes to hbar * Re(alpha)
manualgrad_val = hbar * np.cos(theta)
assert autograd_val == pytest.approx(manualgrad_val, abs=tol)
示例11: test_squeeze_gradient
# 需要导入模块: import autograd [as 别名]
# 或者: from autograd import grad [as 别名]
def test_squeeze_gradient(self, r, tol):
"""Tests that the automatic gradient of a phase space squeezing is correct."""
def circuit(y):
qml.Displacement(alpha, 0., wires=[0])
qml.Squeezing(y, 0., wires=[0])
return qml.expval(qml.X(0))
dev = qml.device('default.gaussian', wires=1)
circuit = to_autograd(CVQNode(circuit, dev))
grad_fn = autograd.grad(circuit)
autograd_val = grad_fn(r)
# qfunc evaluates to -exp(-r) * hbar * Re(alpha)
manualgrad_val = -np.exp(-r) * hbar * alpha
assert autograd_val == pytest.approx(manualgrad_val, abs=tol)
示例12: test_no_differentiable_parameters
# 需要导入模块: import autograd [as 别名]
# 或者: from autograd import grad [as 别名]
def test_no_differentiable_parameters(self):
"""If there are no differentiable parameters, the output of the gradient
function is an empty tuple, and a warning is emitted."""
dev = qml.device("default.qubit", wires=2)
@qml.qnode(dev, interface="autograd")
def circuit(data1):
qml.templates.AmplitudeEmbedding(data1, wires=[0, 1])
return qml.expval(qml.PauliZ(0))
grad_fn = qml.grad(circuit)
data1 = qml.numpy.array([0, 1, 1, 0], requires_grad=False) / np.sqrt(2)
with pytest.warns(UserWarning, match="Output seems independent of input"):
res = grad_fn(data1)
assert res == tuple()
示例13: test_cv_gradients_parameters_inside_array
# 需要导入模块: import autograd [as 别名]
# 或者: from autograd import grad [as 别名]
def test_cv_gradients_parameters_inside_array(self, gaussian_dev, tol):
"Tests that free parameters inside an array passed to an Operation yield correct gradients."
par = [0.4, 1.3]
def qf(x, y):
qml.Displacement(0.5, 0, wires=[0])
qml.Squeezing(x, 0, wires=[0])
M = np.zeros((5, 5), dtype=object)
M[1,1] = y
M[1,2] = 1.0
M[2,1] = 1.0
return qml.expval(qml.PolyXP(M, [0, 1]))
q = qml.QNode(qf, gaussian_dev)
grad = q.jacobian(par)
grad_F = q.jacobian(par, method='F')
grad_A = q.jacobian(par, method="best")
grad_A2 = q.jacobian(par, method="best", options={"force_order2": True})
# par[0] can use the 'A' method, par[1] cannot
assert q.par_to_grad_method == {0:'A', 1:'F'}
# the different methods agree
assert grad == pytest.approx(grad_F, abs=tol)
示例14: test_Rot
# 需要导入模块: import autograd [as 别名]
# 或者: from autograd import grad [as 别名]
def test_Rot(self, qubit_device_1_wire, tol):
"Tests that the automatic gradient of a arbitrary Euler-angle-parameterized gate is correct."
@qml.qnode(qubit_device_1_wire)
def circuit(x,y,z):
qml.Rot(x,y,z, wires=[0])
return qml.expval(qml.PauliZ(0))
grad_fn = autograd.grad(circuit, argnum=[0,1,2])
eye = np.eye(3)
for theta in thetas:
angle_inputs = np.array([theta, theta ** 3, np.sqrt(2) * theta])
autograd_val = grad_fn(*angle_inputs)
for idx in range(3):
onehot_idx = eye[idx]
param1 = angle_inputs + np.pi / 2 * onehot_idx
param2 = angle_inputs - np.pi / 2 * onehot_idx
manualgrad_val = (circuit(*param1) - circuit(*param2)) / 2
assert autograd_val[idx] == pytest.approx(manualgrad_val, abs=tol)
示例15: test_U2
# 需要导入模块: import autograd [as 别名]
# 或者: from autograd import grad [as 别名]
def test_U2(self, tol):
"""Tests that the gradient of an arbitrary U2 gate is correct"""
dev = qml.device("default.qubit", wires=1)
@qml.qnode(dev)
def circuit(x, y):
qml.QubitStateVector(1j*np.array([1, -1])/np.sqrt(2), wires=[0])
qml.U2(x, y, wires=[0])
return qml.expval(qml.PauliX(0))
phi = -0.234
lam = 0.654
res = circuit(phi, lam)
expected = np.sin(lam)*np.sin(phi)
assert np.allclose(res, expected, atol=tol, rtol=0)
grad_fn = autograd.grad(circuit, argnum=[0, 1])
res = grad_fn(phi, lam)
expected = np.array([
np.sin(lam)*np.cos(phi),
np.cos(lam)*np.sin(phi)
])
assert np.allclose(res, expected, atol=tol, rtol=0)