So, I have some problem with feed variables. I want freeze weights and biases of my model over epoch. I have next variables:
wc1 = tf.Variable(tf.random_normal([f1, f1, _channel, n1], mean=0, stddev=0.01), name="wc1")
wc2 = tf.Variable(tf.random_normal([f2, f2, n1, n2], mean=0, stddev=0.01), name="wc2")
wc3 = tf.Variable(tf.random_normal([f3, f3, n2, _channel], mean=0, stddev=0.01), name="wc3")
bc1 = tf.Variable(tf.random_normal(shape=[n1], mean=0, stddev=0.01), name="bc1")
bc2 = tf.Variable(tf.random_normal(shape=[n2], mean=0, stddev=0.01), name="bc2")
bc3 = tf.Variable(tf.random_normal(shape=[_channel], mean=0, stddev=0.01), name="bc3")
For example I want train [wc1, bc1] over first 10 epoch, then [wc2, bc2] over next epoch and so on. For this purposes I created variables collection:
tf.add_to_collection('wc1', wc1)
tf.add_to_collection('wc1', bc1)
tf.add_to_collection('wc2', wc2)
tf.add_to_collection('wc2', bc2)
And create placeholder for collection name:
trainable_name = tf.placeholder(tf.string, shape=[])
Next I try get it in my optimizer:
opt = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
train_op = opt.minimize(cost, var_list=tf.get_collection(trainable_name))
Feed data:
sess.run(train_op, feed_dict={ ... , trainable_name: "wc1"})
And I get error:
Traceback (most recent call last):
File "/home/keeper121/PycharmProjects/super/sp_train.py", line 292, in <module>
train(tiles_names, "model.ckpt")
File "/home/keeper121/PycharmProjects/super/sp_train.py", line 123, in train
train_op = opt.minimize(cost, var_list=tf.get_collection(trainable_name))
File "/home/keeper121/anaconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/training/optimizer.py", line 193, in minimize
grad_loss=grad_loss)
File "/home/keeper121/anaconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/training/optimizer.py", line 244, in compute_gradients
raise ValueError("No variables to optimize")
ValueError: No variables to optimize
So, any ways to change training variables over session?
Thanks.
Give the following a try:
train_op_wc1 = opt.minimize(cost, var_list=tf.get_collection("wc1"))
train_op_wc2 = opt.minimize(cost, var_list=tf.get_collection("wc2"))
And then when you feed the data:
#define your samples as you would always do
input_feed = ...
#then use the training op that addresses the correct layers, as you defined above
if first_10_epoch:
sess.run(train_op_wc1, feed_dict=input_feed)
else:
sess.run(train_op_wc2, feed_dict=input_feed)