Tensorflow:向变量提供值是否永久覆盖其值? [英] Tensorflow: does feeding a value to a variable override its value permanently?
问题描述
我有一个带有一些权重的 NN 模型,我需要能够使用有时使用一组权重 A 有时使用一组权重 B 进行推理(和学习).我知道我可以将值提供给变量,而不仅仅是占位符,但如果我这样做,我会在请求的操作节点的执行窗口内永久覆盖变量值还是临时值?
I have a NN model with some weights, and I need to be able to make inference (and learning) using sometiems a set of weights A and sometimes a set of weights B. I know I can feed values to Variables and not only to placeholders, but if I do so I permanently override the variable value or is it temporary, within the execution window of the op nodes requested?
例如,假设我构建了图形并使用权重集 A 学习了一个模型.如果我想使用另一个权重集 B 进行推理,我可以这样做:
For example, Let's assume I built the graph and learnt a model using weight set A. If I want to make inference using another weight set, set B, I can do:
sess.run(output, feed_dict={input:input, weights:weightsB}) #inference
我的问题是:如果我提供值 weightsB,它们会永久覆盖网络的值吗?如果下次我会跑
My question is: If I feed the values weightsB, will they permanently override the values of the network? If next time I will run
sess.run(output, feed_dict={input:input}) #inference
它会使用 weightsA 还是 weightsB?
will it use weightsA or weightsB?
推荐答案
在 TensorFlow 中,在一个步骤中为变量提供一个值不会影响存储在该变量中的值.在后续步骤中,如果您使用该变量,它将继续保持其先前的值.
In TensorFlow, feeding a value for a variable in one step does not affect the value stored in that variable. In subsequent steps, if you use the variable, it will continue to have its previous value.
请注意,如果您希望提要在以后的步骤中更改变量的值,您应该使用如下结构将该值显式分配给变量:
Note that if you want a feed to change the value of a variable in future steps, you should explicitly assign that value to the variable, using a construction like:
v = tf.Variable(...)
v_update_placeholder = tf.placeholder(v.dtype, shape=v.shape)
v_update_op = v.assign(v_update_placeholder).op
# ...
sess.run(v_update_op, feed_dict={v_update_placeholder: ...})
这篇关于Tensorflow:向变量提供值是否永久覆盖其值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!