gpt4 book ai didi

tensorflow - 如何使用 tensorflow 占位符在 get_collection 中使用

转载 作者:行者123 更新时间:2023-12-03 09:49:35 26 4
gpt4 key购买 nike

所以,我在 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

那么,有什么方法可以改变 session 中的训练变量吗?

谢谢。

最佳答案

尝试以下方法:

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)

关于tensorflow - 如何使用 tensorflow 占位符在 get_collection 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38720128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com