gpt4 book ai didi

python - 最小化 Tensorflow 中一个变量的函数

转载 作者:太空狗 更新时间:2023-10-29 18:19:48 24 4
gpt4 key购买 nike

我是 Tensorflow 的新手,想知道是否可以使用 Tensorflow 最小化一个变量的函数。

例如,我们可以使用 Tensorflow 使用初始猜测(比如 x = 1)来最小化 2*x^2 - 5^x + 4 吗?

我正在尝试以下操作:

import tensorflow as tf
import numpy as np

X = tf.placeholder(tf.float32, shape = ())
xvar = tf.Variable(np.random.randn())
f = 2*mul(X,X) - 5*X + 4

opt = tf.train.GradientDescentOptimizer(0.5).minimize(f)

with tf.Session() as sess:
tf.global_variables_initializer().run()
y = sess.run(opt, feed_dict = {X : 5.0}) #initial guess = 5.0
print(y)

但这会产生以下错误:

ValueError: No gradients provided for any variable, check your graph for ops that do not support gradients, between variables

请帮助我理解我在这里做错了什么。

最佳答案

如果您想最小化单个参数,您可以执行以下操作(我避免使用占位符,因为您正在尝试训练参数 - 占位符通常用于超参数和输入,不被视为可训练参数):

import tensorflow as tf

x = tf.Variable(10.0, trainable=True)
f_x = 2 * x* x - 5 *x + 4

loss = f_x
opt = tf.train.GradientDescentOptimizer(0.1).minimize(f_x)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(100):
print(sess.run([x,loss]))
sess.run(opt)

这将输出以下对 (x,loss) 的列表:

[10.0, 154.0]
[6.5, 56.0]
[4.4000001, 20.720001]
[3.1400001, 8.0192013]
[2.3840001, 3.4469128]
[1.9304, 1.8008881]
[1.65824, 1.2083197]
[1.494944, 0.99499512]
[1.3969663, 0.91819811]
[1.3381798, 0.89055157]
[1.3029079, 0.88059855]
[1.2817447, 0.87701511]
[1.2690468, 0.87572551]
[1.2614281, 0.87526155]
[1.2568569, 0.87509394]
[1.2541142, 0.87503386]
[1.2524685, 0.87501216]
[1.2514811, 0.87500429]
[1.2508886, 0.87500143]
[1.2505331, 0.87500048]
[1.2503198, 0.875]
[1.2501919, 0.87500024]
[1.2501152, 0.87499976]
[1.2500691, 0.875]
[1.2500415, 0.875]
[1.2500249, 0.87500024]
[1.2500149, 0.87500024]
[1.2500089, 0.875]
[1.2500054, 0.87500024]
[1.2500032, 0.875]
[1.2500019, 0.875]
[1.2500012, 0.87500024]
[1.2500007, 0.87499976]
[1.2500005, 0.875]
[1.2500002, 0.87500024]
[1.2500001, 0.87500024]
[1.2500001, 0.87500024]
[1.2500001, 0.87500024]
[1.2500001, 0.87500024]
[1.2500001, 0.87500024]
[1.2500001, 0.87500024]
[1.2500001, 0.87500024]
[1.2500001, 0.87500024]
[1.2500001, 0.87500024]
[1.2500001, 0.87500024]
[1.2500001, 0.87500024]
[1.2500001, 0.87500024]
[1.2500001, 0.87500024]
[1.2500001, 0.87500024]
[1.2500001, 0.87500024]

关于python - 最小化 Tensorflow 中一个变量的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41918795/

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