What is float ref?

Trying to run the following basic example to start a conditional calculation, I received the following error message:

'x' passed by float incompatible with expected value float_ref

what is tenor_ref, and how should the code be changed?

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 
+5
source share
4 answers

this does not explain what float_ref is, but it fixes the problems:

1) variables should be created in the session 2) the op assignment was not what we expected

this fixed code works:

 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 
+2
source

FYI. I have the same error and I had:

node GradientDescent / update_input / ApplyGradientDescent passed float from _arg_input_0_1: 0 is incompatible with the expected value of float_ref.

This happened because somewhere in my node -tree I had tf.Variable instead of t.fplaceholder . After replacing the variable with a placeholder, it worked.

+2
source

float_ref here refers to a reference to float, i.e. your Tensorflow x floating variable.

As explained here , you encountered this error because you cannot simultaneously assign and transfer the variable as a file feed in the same session as you do in this statement:

 val = s.run([calculate], {a: 1., b: 2., x: 0.}) 

This becomes more apparent when you resolve this statement in the end:

 val = s.run([x.assign( a + b)], {a: 1., b: 2., x: 0.}) 
+1
source

change

 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 

so that:

  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 
0
source

Source: https://habr.com/ru/post/1238107/


All Articles