Python Tensorflow:无法将tf.case与输入参数一起使用
我需要创建一个变量epsilon\u n,它根据当前步骤更改定义和值。由于我有两个以上的案例,似乎我不能使用tf.cond。我尝试使用tf.case,如下所示:Python Tensorflow:无法将tf.case与输入参数一起使用,python,tensorflow,piecewise,Python,Tensorflow,Piecewise,我需要创建一个变量epsilon\u n,它根据当前步骤更改定义和值。由于我有两个以上的案例,似乎我不能使用tf.cond。我尝试使用tf.case,如下所示: import tensorflow as tf #### EPSILON_DELTA_PHASE1 = 33e-4 EPSILON_DELTA_PHASE2 = 2.5 #### step = tf.placeholder(dtype=tf.float32, shape=None) def fn1(step): retur
import tensorflow as tf
####
EPSILON_DELTA_PHASE1 = 33e-4
EPSILON_DELTA_PHASE2 = 2.5
####
step = tf.placeholder(dtype=tf.float32, shape=None)
def fn1(step):
return tf.constant([1.])
def fn2(step):
return tf.constant([1.+step*EPSILON_DELTA_PHASE1])
def fn3(step):
return tf.constant([1.+step*EPSILON_DELTA_PHASE2])
epsilon_n = tf.case(
pred_fn_pairs=[
(tf.less(step, 3e4), lambda step: fn1(step)),
(tf.less(step, 6e4), lambda step: fn2(step)),
(tf.less(step, 1e5), lambda step: fn3(step))],
default=lambda: tf.constant([1e5]),
exclusive=False)
但是,我一直收到以下错误消息:
TypeError: <lambda>() missing 1 required positional argument: 'step'
我还是会犯同样的错误。Tensorflow文档中的示例说明了没有输入参数传递给可调用函数的情况。我在网上找不到足够的关于tf.case的信息!请提供帮助?这里有一些您需要做的更改。 为了保持一致性,可以将所有返回值设置为变量
# Since step is a scalar, scalar shape [() or [], not None] much be provided
step = tf.placeholder(dtype=tf.float32, shape=())
def fn1(step):
return tf.constant([1.])
# Here you need to use Variable not constant, since you are modifying the value using placeholder
def fn2(step):
return tf.Variable([1.+step*EPSILON_DELTA_PHASE1])
def fn3(step):
return tf.Variable([1.+step*EPSILON_DELTA_PHASE2])
epsilon_n = tf.case(
pred_fn_pairs=[
(tf.less(step, 3e4), lambda : fn1(step)),
(tf.less(step, 6e4), lambda : fn2(step)),
(tf.less(step, 1e5), lambda : fn3(step))],
default=lambda: tf.constant([1e5]),
exclusive=False)
修正了小错误
# Since step is a scalar, scalar shape [() or [], not None] much be provided
step = tf.placeholder(dtype=tf.float32, shape=())
def fn1(step):
return tf.constant([1.])
# Here you need to use Variable not constant, since you are modifying the value using placeholder
def fn2(step):
return tf.Variable([1.+step*EPSILON_DELTA_PHASE1])
def fn3(step):
return tf.Variable([1.+step*EPSILON_DELTA_PHASE2])
epsilon_n = tf.case(
pred_fn_pairs=[
(tf.less(step, 3e4), lambda : fn1(step)),
(tf.less(step, 6e4), lambda : fn2(step)),
(tf.less(step, 1e5), lambda : fn3(step))],
default=lambda: tf.constant([1e5]),
exclusive=False)