tensorflow 常用简单的函数收集 cond expand_dims squeeze map_fn

本文介绍了在TensorFlow中如何使用条件函数`tf.cond`替代if else语句,以及如何通过`expand_dims`和`squeeze`进行维度变换。此外,还详细解释了如何运用`map_fn`对四维张量进行降维处理和批量操作,以便于对每个batch进行单独操作,并在完成后恢复原始形状。
摘要由CSDN通过智能技术生成

1.条件函数,替代 if else

在tensorflow里面不能使用if else对bool型tensor进行判断。所以需要使用tf.cond

https://blog.csdn.net/u013249853/article/details/101012523

x = tf.constant(2)
y = tf.constant(5)
def f1(): return tf.multiply(x, 17)
def f2(): return tf.add(y, 23)
r = tf.cond(tf.less(x, y), f1, f2)
# r is set to f1().
# Operations in f2 (e.g., tf.add) are not executed.

2.维度变换,expand_dims squeeze

有些tf core的op可以处理四维,(batch,weidth,height,channels),比如:tf.ResizeBilinear:https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/resize-bilinear

但是有些op你是用三维写的那就需要改变维度。

(1,512,412,23)->(512,412,23)

tf.expand_dims(feature_map,axis=0)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值