Skip to content

Instantly share code, notes, and snippets.

@benoitdescamps
Last active May 23, 2019 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benoitdescamps/a5566afe9fc16dd43cf783d509322a7f to your computer and use it in GitHub Desktop.
Save benoitdescamps/a5566afe9fc16dd43cf783d509322a7f to your computer and use it in GitHub Desktop.
def RosenbrockOpt(optimizer,MAX_EPOCHS = 4000, MAX_STEP = 100):
'''
returns distance of each step*MAX_STEP w.r.t minimum (1,1)
'''
x1_data = tf.Variable(initial_value=tf.random_uniform([1], minval=-3, maxval=3,seed=0),name='x1')
x2_data = tf.Variable(initial_value=tf.random_uniform([1], minval=-3, maxval=3,seed=1), name='x2')
y = tf.add(tf.pow(tf.subtract(1.0, x1_data), 2.0),
tf.multiply(100.0, tf.pow(tf.subtract(x2_data, tf.pow(x1_data, 2.0)), 2.0)), 'y')
global_step_tensor = tf.Variable(0, trainable=False, name='global_step')
train = optimizer.minimize(y,global_step=global_step_tensor)
sess = tf.Session()
init = tf.global_variables_initializer()#tf.initialize_all_variables()
sess.run(init)
minx = 1.0
miny = 1.0
distance = []
xx_ = sess.run(x1_data)
yy_ = sess.run(x2_data)
print(0,xx_,yy_,np.sqrt((minx-xx_)**2+(miny-yy_)**2))
for step in range(MAX_EPOCHS):
_, xx_, yy_, zz_ = sess.run([train,x1_data,x2_data,y])
if step % MAX_STEP == 0:
print(step+1, xx_,yy_, zz_)
distance += [ np.sqrt((minx-xx_)**2+(miny-yy_)**2)]
sess.close()
return distance
@ENate
Copy link

ENate commented Mar 6, 2019

Hi, thanks for the interesting tutorial and example on how to implement an optimizer in Tensorflow. However, I was unable to run the example you provided after calling it as follows:
RosenbrockOpt(optimizer,MAX_EPOCHS = 4000, MAX_STEP = 100)
I got the following message:
that there is not 'minimize' function in the Tensorflow package:
AttributeError: module 'tensorflow.python.training.optimizer' has no attribute 'minimize'
The full error is:
in RosenbrockOpt(optimizer, MAX_EPOCHS, MAX_STEP)
11 global_step_tensor = tf.Variable(0, trainable=False, name='global_step')
12
---> 13 train = optimizer.minimize(y,global_step=global_step_tensor)
14
15 sess = tf.Session()

AttributeError: module 'tensorflow.python.training.optimizer' has no attribute 'minimize'
I am able to find out why this happens. How did you run the example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment