TensorFlow 实战(三)—— 实现常见公式

  • tf.reduce_mean (求向量的均值)等价于

    1Ni=1Nxi

1. 对权值矩阵进行 l2 正则

def variable_with_weight_loss(shape, stddev, w1):
    var = tf.Variable(tf.truncated_normal(shape, stddev=stddev))
    if w1 is not None:
        weight_loss = tf.multiply(tf.nn.l2_loss(var), w1, name='weight_loss')
        tf.add_to_collections('losses', weight_loss)
    return var

2. binary cross entropy

def bin_cross_entropy(preds, targets):
    eps = 1e-12
    return tf.reduce_mean(-targets*tf.log(preds+eps)-(1-targets)*tf.log(1-preds+eps))

转载于:https://www.cnblogs.com/mtcnn/p/9422050.html

你可能感兴趣的:(TensorFlow 实战(三)—— 实现常见公式)