tf.reduce_mean (求向量的均值)等价于
1N∑i=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))