0
我在使用tensorflow的簡單代碼中使用tf.while_loop。它給了我這個錯誤NameError:在封閉範圍內賦值之前引用的自由變量'ar'
ar = tf.cond(tf.less(arg[i], 1), lambda: tf.concat([ar,[1.0]], axis = 0), lambda: tf.concat([ar,[0.0]], axis = 0))
NameError: free variable 'ar' referenced before assignment in enclosing scope
代碼::
a = tf.constant([[[1,2],[4,3]],[[5,6],[8,7]]])
arg = tf.argmax(a, axis =2)
arg = tf.cast(tf.reshape(arg,[4]), tf.int32)
i =tf.constant(0)
c = lambda i: tf.less(i,4)
def body(i):
ar = tf.cond(tf.less(arg[i], 1), lambda: tf.concat([ar,[1.0]], axis = 0), lambda: tf.concat([ar,[0.0]], axis = 0))
print(i)
i = tf.add(i,1)
return i
ar = tf.constant([1])
r = tf.while_loop(c,body,[i])
sess =tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
sess.run(r)
print(sess.run(ar))
謝謝,這是工作 – CCCC