
[英]Why the random value has different values in the same session after setting Tensorflow graph-level seed?
[英]How to assign value to one graph with the other graph who has the same structure in tensorflow?
我正在尝试在张量流中实现DQN。 在这里,我有一个目标网络和一个培训网络,它们彼此具有相同的结构。 在每10000个训练步骤的开始,我要将值从检查点加载到目标网络和训练网络,然后将stop_gradient加载到目标网络。 但是,我尝试了这些方法,但没有一个起作用:
1,将两个网络放在一张图中。 但是,每次加载它们时,我都不知道如何将训练网络部分的值分配给目标网络部分(它们以不同的值保存,因为一个是停止梯度。)
2,使用tf.graph()定义两个图,并分别运行两个会话。 但是,即使它们具有相同的结构,也无法将一个图的检查点加载到另一个图。 毕竟,它们是两个不同的图。
那么,谁能给我一些建议呢? 非常感激!
典型的方法是将所有内容放在一个图中,将两个网络放在两个名称范围内 ,然后为一个范围内的每个变量创建tf.assign ops到另一个范围,然后使用tf.group构造最终的“复制”操作。 假设函数create_net()
建立一个单一网络
with tf.name_scope('main_network'):
main_net = create_net()
with tf.name_scope('target_network):
target_network = create_net()
main_variables = tf.get_collection(tf.GraphKeys.VARIABLES, scope='main_network')
target_variables = tf.get_collection(tf.GraphKeys.VARIABLES, scope='target_network')
# I am assuming get_collection returns variables in the same order, please double
# check this is actually happening
assign_ops = []
for main_var, target_var in zip(main_variables, target_variables):
assign_ops.append(tf.assign(target_var, tf.identity(main_var)))
copy_operation = tf.group(*assign_ops)
现在,在session.run中执行copy_operation会将您的主要网络参数复制到目标网络。 上面的代码应被视为伪代码,而不是可以复制粘贴的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.