我有以下简单的占位符:
x = tf.placeholder(tf.float32, shape=[1])
y = tf.placeholder(tf.float32, shape=[1])
z = tf.placeholder(tf.float32, shape=[1])
有两个函数fn1
和fn2
,定义如下:
def fn1(a, b):
return tf.mul(a, b)
def fn2(a, b):
return tf.add(a, b)
现在我想根据预测条件计算结果:
pred = tf.placeholder(tf.bool, shape=[1])
result = tf.cond(pred, fn1(x,y), fn2(y,z))
但它会报错,提示fn1和fn2必须可调用
。
我该如何编写fn1
和fn2
,使它们能在运行时接收参数?我想要调用以下内容:
sess.run(result, feed_dict={x:1,y:2,z:3,pred:True})