如何在 get_collection 中使用 tensorflow 占位符
how use tensorflow placeholder to use in get_collection
所以,我在 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")
例如,我想在前 10 个时期训练 [wc1, bc1],然后在下一个时期训练 [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))
供稿数据:
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
那么,有什么方法可以在会话期间更改训练变量?
谢谢。
试试下面的方法:
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)
所以,我在 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")
例如,我想在前 10 个时期训练 [wc1, bc1],然后在下一个时期训练 [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))
供稿数据:
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
那么,有什么方法可以在会话期间更改训练变量?
谢谢。
试试下面的方法:
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)