介绍 Eager Execution 模式下的梯度计算,废话不多说,直接上图
1,gradients_function
def square(x):
return x ** 2
print(square(3.))
grad = tfe.gradients_function(square)
print(grad(3.))
grad = tfe.value_and_gradients_function(square)
print(grad(3.))
def f(x, y):
return x ** 2 + y ** 2
print(f(2., 3.))
grad = tfe.gradients_function(f)
print(grad(2., 3.))
grad = tfe.value_and_gradients_function(f)
print(grad(2., 3.))
2,implicit_function
x = tfe.Variable(initial_value = 2.0, name = 'x')
y = tfe.Variable(initial_value = 3.0, name = 'y')
print(x.numpy())
def loss(y):
return (y - x ** 2) ** 2
print(loss(7).numpy())
grad = tfe.implicit_gradients(loss)
print(grad(7.))
grad = tfe.implicit_value_and_gradients(loss)
print(grad(7.))
x = tfe.Variable(initial_value = 2.0, name = 'x')
y = tfe.Variable(initial_value = 3.0, name = 'y')
z = tfe.Variable(initial_value = 3.0, name = 'z')
print(x)
def loss(y):
return (y - x ** 2 + z ** 2) ** 2
print(loss(7).numpy())
grad = tfe.implicit_gradients(loss)
print(grad(7.))
grad = tfe.implicit_value_and_gradients(loss)
print(grad(7.))
在看一个用 Eager Execution写的回归时,看到看到求导函数里面嵌套一个函数,所以有一个示例如下
w = tfe.Variable(initial_value = 3.0, name = 'w')
b = tfe.Variable(initial_value = 3.0, name = 'b')
def aa(x):
return w * x + b
def bb(model, x, y):
return tf.pow(model(x) - y, 2)
grad = tfe.implicit_gradients(bb)
print(grad(aa, 2, 2))
grad = tfe.implicit_value_and_gradients(bb)
print(grad(aa, 2, 2))