我想传递一个布尔值,它可以在我评估图形时改变。如下图所示张量流程:在创建和评估图形时传递bool变量
import tensorflow as tf
from tensorflow.python.ops import control_flow_ops
def my_model(input, is_freeze):
trainable = tf.cond(is_freeze, lambda: tf.constant(False),
lambda: tf.constant(True)) #Try 3
#trainable = tf.cond(is_freeze, lambda: False, lambda: True) # Try 1
#trainable = tf.logical_not(is_freeze) # Try 2
fc1_W = tf.get_variable('fc1_W', trainable = trainable,
initializer = tf.zeros(100, 2))
fc1 = tf.matmul(input, fc1_W)
return fc1
tf.reset_default_graph()
X = tf.placeholder(tf.float32)
is_freeze = tf.placeholder(tf.bool)
my_model_graph = my_model(X, is_freeze) # Getting error while creating model
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(my_optimizer, feed_dict = {is_freeze: False, X: np.zeros((50, 100))})
我收到的错误:对于#Try1
AttributeError: 'bool' object has no attribute 'name'
对于#Try3
:我张贴我下面的代码
TypeError: Using a tf.Tensor as a Python bool is not allowed.
Use if t is not None: instead of if t: to test if a tensor is defined,
and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on
the value of a tensor.`
请能有人给我解释一下我应该如何克服这个问题。如果可能的话,任何人都可以告诉我在我所有的方法中哪里出了问题。
基本上我在做什么是我有两个模型。首先,我训练一个模型,然后使用第一个模型的学习值,我必须训练第二个模型冻结第一个模型的值。
在此先感谢。
您可以通过将所有节点绘制到图中来实现它,但只返回一部分结果来通过正确的路径只运行图的一部分? – lerner