tf.map_fn(
fn,
elems,
dtype=None,
parallel_iterations=10,
back_prop=True,
swap_memory=False,
infer_shape=True,
name=None
)
fn:映射函数,参数就是Tensor里的每一个元素
elems:Tensor或者一系列Tensors
该函数的功能是对Tensor里的每一个元素进行映射
示例代码:
x=tf.constant(value=['1','2','3','4','5'],dtype=tf.string)
y=tf.map_fn(lambda a:a+'*',x)
或者
def f(x):
return x+'*'
x=tf.constant(value=['1','2','3','4','5'],dtype=tf.string)
y=tf.map_fn(f,x)
tf.cond(
pred,
true_fn=None,
false_fn=None,
strict=False,
name=None,
fn1=None,
fn2=None
)
pred:表示条件,一个Tensor,取值为True或者False
true_fn:如果pred为真,则执行该函数
false_fn:如果pred为假,则执行该函数
true_fn和false_fn可以使用lambda和def来实现
a = tf.constant(2)
b = tf.constant(5)
def t():
return 'true'
def f():
return 'false'
result=tf.cond(tf.less(a,b),t,f)
with tf.Session() as sess:
print(sess.run(result))