TensorFlow 学习(十)—— 工具函数

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.54.5 之间
    array([[ 2.5,  2.5,  2.5],
           [ 3. ,  4. ,  4.5]], dtype=float32)

2. 条件分支:tf 下的三目运算符

f(x,y)={a(xy),a(yx),x>yxy

tf.select(tf.greater(v1, v2), a*(v1-v2), a*(v2-v1))

当然上式也可化为:f(x,y)=a|xy|

原文地址:https://www.cnblogs.com/mtcnn/p/9421958.html