所以,我对feed变量有一些问题。我希望我的模型的冻结重量和偏差超过时代。我有下一个变量:
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")
例如,我希望火车[wc1,bc1]超过前10个时期,然后[wc2,bc2]超过下一个纪元,依此类推。为此,我创建了变量集合:
tf.add_to_collection('wc1', wc1)
tf.add_to_collection('wc1', bc1)
tf.add_to_collection('wc2', wc2)
tf.add_to_collection('wc2', bc2)
为集合名称创建占位符:
trainable_name = tf.placeholder(tf.string, shape=[])
接下来我尝试在我的优化器中获取它:
opt = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
train_op = opt.minimize(cost, var_list=tf.get_collection(trainable_name))
Feed数据:
sess.run(train_op, feed_dict={ ... , trainable_name: "wc1"})
我得到错误:
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
那么,在会话中改变训练变量的方法是什么?
感谢。
答案 0 :(得分:0)
尝试以下内容:
train_op_wc1 = opt.minimize(cost, var_list=tf.get_collection("wc1"))
train_op_wc2 = opt.minimize(cost, var_list=tf.get_collection("wc2"))
然后当您提供数据时:
#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)