Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

python - Minimize a function of one variable in Tensorflow

I am new to Tensorflow and was wondering whether it would be possible to minimize a function of one variable using Tensorflow.

For example, can we use Tensorflow to minimize 2*x^2 - 5^x + 4 using an initial guess (say x = 1)?

I am trying the following:

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)

But this gives the following error:

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

Please help me understand what am I doing wrong here.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to minimize a single parameter you could do the following (I've avoided using a placeholder since you are trying to train a parameter - placeholders are often used for hyper-parameters and input and aren't considered trainable parameters):

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)

This will output the following list of pairs (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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...