I build a model of neural network, and i want to change the value of a certain tensor within a session in tensorflow.

For example, if we ignore the model to simplify, but we have this tensor to optimize:

# construct an optimizer
train_op = tf.train.AdamOptimizer(learning_rate=0.05).minimize(cost)  

After i can run my model in a session to train it.

But i want to open a session and change the value of tensor train_op, for example i have this:

with tf.Session() as sess: 
    #initialize all variables
    tf.initialize_all_variables().run()

    for i in range(iteraciones):
    #Prepare input(minibach) to feed model
        input_ = trainCluster0[0:len(train)]
        # train model
        sess.run(train_op, feed_dict={X: input_})
        print(i, sess.run(cost, feed_dict={X: train}))
        #Save model in last epoch
        if(i == iteraciones-1):
            save_path = saver.save(sess, "/tmp/model.ckpt")
            print("Model saved.")

I want something as this:

with tf.Session() as sess: 
    #initialize all variables
    tf.initialize_all_variables().run()

    #Change value of tensor train_op
    # train_op = tf.train.AdamOptimizer(learning_rate=value).minimize(cost)
    ...
    ...

    for i in range(iteraciones):
        #Prepare input(minibach) to feed model
        input_ = trainCluster0[0:len(train)]
        # train model
        sess.run(train_op, feed_dict={X: input_})
        print(i, sess.run(cost, feed_dict={X: train}))
        #Save last epoch and test
        if(i == iteraciones-1):
            save_path = saver.save(sess, "/tmp/model.ckpt")
            print("Model saved.")

How can i do this? that is, reuse the model with different optimization parameters.

Thanks in advance.

1 Answers

0
Alejandro Reina On Best Solutions

Solved: thanks to @jdehesa

The solutions is add this placeholder to the model:

#Tensor placeholder to parametizer learning rate of optimizer
learning = tf.placeholder("float", name='learning')

# construct an optimizer
train_op = tf.train.AdamOptimizer(learning).minimize(cost)

And this for the sess.run:

with tf.Session() as sess:
    # we need to initialize all variables
    tf.initialize_all_variables().run()
    RATIO = 0.001
    ITERATIONS = 1000

    for i in range(ITERATIONS):
        #Prepare input(minibach) from feed autoencoder     
        input_ = trainCluster0[0:len(trainCluster0)]
        # train autoencoder
        sess.run(train_op, feed_dict={X: input_, learning: RATIO})
        print(i, sess.run(cost, feed_dict={X: input_}))
        #Save last epoch and test
        if(i == ITERATIONS-1):
            save_path = saver.save(sess, "/tmp/modelCluster0.ckpt")
            print("Model saved.")