gpt4 book ai didi

python - TensorFlow简单示例帮助-自定义渐变

转载 作者:行者123 更新时间:2023-11-30 09:03:58 24 4
gpt4 key购买 nike

如何将自定义梯度传递到 TensorFlow 中的梯度优化函数中。

我已经用一个简单的例子说明了我正在尝试做的事情(尝试最小化 z = 2x^2 + y^2 + 2)。

我一直在看: https://www.tensorflow.org/api_docs/python/tf/train/Optimizer

如果您传入 optimizer = tf.train.GradientDescentOptimizer(0.55)train = optimizationr.minimize(z)

,问题似乎就可以解决。

此代码有效:

import tensorflow as tf

x = tf.Variable(11, name='x', dtype=tf.float32)
y = tf.Variable(11, name='x', dtype=tf.float32)
const = tf.constant(2.0, dtype=tf.float32)

z = x**2 + y**2 + const


optimizer = tf.train.GradientDescentOptimizer(0.55)
train = optimizer.minimize(z)

init = tf.global_variables_initializer()

def optimize():
with tf.Session() as session:
session.run(init)
print("starting at", "x:", session.run(x), "y:", session.run(y), "z:", session.run(z))
for step in range(10):
session.run(train)
print("step", step, "x:", session.run(x), "y:", session.run(y), "z:", session.run(z))


optimize()

但我想指定问题中的梯度。又名我正在尝试这样做:

def function_to_minimize(x,y, const):
# z = 2x^2 + y^2 + constant
z = 2*x**2 + y**2 + const
return z

def calc_grad(x,y):
# z = 2x^2 + y^2 + constant
dz_dx = 4*x
dz_dy = 2*y
return [(dz_dx, x), (dz_dy, y)]

x = tf.Variable(3, name='x', dtype=tf.float32)
y = tf.Variable(3, name='y', dtype=tf.float32)
const = tf.constant(2.0, dtype=tf.float32)


z = function_to_minimize(x,y, const)
grad = calc_grad(x,y)


init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
print(sess.run(z))
print(sess.run(grad))


optimizer = tf.train.GradientDescentOptimizer(0.5)

grads_and_vars = calc_grad(x,y)

optimizer.apply_gradients(grads_and_vars)

# minimize() takes care of both computing the gradients and applying them to the variables.
#If you want to process the gradients before applying them you can instead use the optimizer in three steps:
# 1. Compute the gradients with compute_gradients().
# 2. Process the gradients as you wish.
# 3. Apply the processed gradients with apply_gradients()

你如何正确地做到这一点?

最佳答案

apply_gradients 返回可用于应用渐变的操作。换句话说,您只需执行 train = optimizer.apply_gradients(grads_and_vars) ,其余部分将像第一个代码片段一样工作。我,即:

optimizer = tf.train.GradientDescentOptimizer(0.55)
grads_and_vars = calc_grad(x,y)
train = optimizer.apply_gradients(grads_and_vars)

init = tf.global_variables_initializer()

def optimize():
with tf.Session() as session:
session.run(init)
print("starting at", "x:", session.run(x), "y:", session.run(y), "z:", session.run(z))
for step in range(10):
session.run(train)
print("step", step, "x:", session.run(x), "y:", session.run(y), "z:", session.run(z))


optimize()

关于python - TensorFlow简单示例帮助-自定义渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57392510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com