tf.while_loop
接受泛型可调用函数(用
def
因此,可以使用
logical operators
喜欢
tf.logical_and
,
tf.logical_or
, ...
body
这样的做法完全可以接受,而且效果很好:
import tensorflow as tf
import numpy as np
def body(x):
a = tf.random_uniform(shape=[2, 2], dtype=tf.int32, maxval=100)
b = tf.constant(np.array([[1, 2], [3, 4]]), dtype=tf.int32)
c = a + b
return tf.nn.relu(x + c)
def condition(x):
x = tf.Print(x, [x])
return tf.logical_or(tf.less(tf.reduce_sum(x), 1000), tf.equal(x[0, 0], 15))
x = tf.Variable(tf.constant(0, shape=[2, 2]))
result = tf.while_loop(condition, body, [x])
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run(result))