1. 基本
tf.clip_by_value() 截斷,常和對數函數結合使用
# 計算交叉熵crose_ent = -tf.reduce_mean(tf.log(y_*tf.clip_by_value(y, 1e-10, 1.)))
a = tf.reshape(tf.range(6, dtype=tf.float32), [2, 3]) tf.clip_by_value(a, 2.5, 4.5) # 將值限定在 2.5 和 4.5 之間 array([[ 2.5, 2.5, 2.5],[ 3. , 4. , 4.5]], dtype=float32)
2. 條件分支:tf 下的三目運算符
f(x,y)={a(x?y),a(y?x),x>yx≤y
tf.select(tf.greater(v1, v2), a*(v1-v2), a*(v2-v1))
當然上式也可化為:f(x,y)=a|x?y|;