Python 什么是tensorflow浮动参考?
尝试运行以下基本示例以运行条件计算时,我收到以下错误消息: 传递的“x”浮点值与预期的浮点值\u ref不兼容 什么是tensorflow float_ref?代码必须如何修改Python 什么是tensorflow浮动参考?,python,tensorflow,Python,Tensorflow,尝试运行以下基本示例以运行条件计算时,我收到以下错误消息: 传递的“x”浮点值与预期的浮点值\u ref不兼容 什么是tensorflow float_ref?代码必须如何修改 import tensorflow as tf from tensorflow.python.ops.control_flow_ops import cond a = tf.Variable(tf.constant(0.),name="a") b = tf.Variable(tf.constant(0.),name="
import tensorflow as tf
from tensorflow.python.ops.control_flow_ops import cond
a = tf.Variable(tf.constant(0.),name="a")
b = tf.Variable(tf.constant(0.),name="b")
x = tf.Variable(tf.constant(0.),name="x")
def add():
x.assign( a + b)
return x
def last():
return x
calculate= cond(x==0.,add,last)
with tf.Session() as s:
val = s.run([calculate], {a: 1., b: 2., x: 0.})
print(val) # 3
val=s.run([calculate],{a:4.,b:5.,x:val})
print(val) # 3
这并不能解释什么是float_ref,但它解决了以下问题: 1) 需要在会话中创建变量 2) 任务op不是我们所期望的 这个固定的代码工作:
def add():
print("add")
x = a + b
return x
def last():
print("last")
return x
with tf.Session() as s:
a = tf.Variable(tf.constant(0.),name="a")
b = tf.Variable(tf.constant(0.),name="b")
x = tf.constant(-1.)
calculate= cond(x.eval()==-1.,add,last)
val = s.run([calculate], {a: 1., b: 2.})
print(val) # 3
print(s.run([calculate],{a:3.,b:4.})) # 7
print(val) # 3
仅供参考。
我也犯了类似的错误,我的错误是:
节点GradientDescent/update_input/ApplyGradientDescent从_arg_input_0_1:0传递的浮点值与预期的浮点值_ref不兼容
这是因为在我的节点树的某个地方,我有一个
tf.Variable
,而不是t.fplaceholder
。在用占位符替换变量后,它就工作了。float\u ref
这里指的是对一个float的引用,即您的Tensorflow float变量x
如前所述,您面临此错误是因为您不能像在本语句中那样在同一会话运行中同时分配和传递变量作为提要:
val = s.run([calculate], {a: 1., b: 2., x: 0.})
当您将该语句解析为以下结果时,它会变得更加明显:
val = s.run([x.assign( a + b)], {a: 1., b: 2., x: 0.})
改变
致:
a = tf.Variable(tf.constant(0.),name="a")
b = tf.Variable(tf.constant(0.),name="b")
x = tf.Variable(tf.constant(0.),name="x")
def add():
x.assign( a + b)
return x
a = tf.placeholder(dtype=tf.float32,name="a")
b = tf.placeholder(dtype=tf.float32,name="b")
x = tf.placeholder(dtype=tf.float32,name="x")
def add():
x= a + b
return x