如何編寫分段TensorFlow函數,即其中包含if語句的函數?在TensorFlow中寫入分段函數/如果在TensorFlow中寫入
當前代碼
import tensorflow as tf
my_fn = lambda x : x ** 2 if x > 0 else x + 5
with tf.Session() as sess:
x = tf.Variable(tf.random_normal([100, 1]))
output = tf.map_fn(my_fn, x)
錯誤:
類型錯誤:使用tf.Tensor
爲Python bool
是不允許的。使用if t is not None:
而不是if t:
來測試張量是否已定義,並使用邏輯TensorFlow操作來測試張量的值。