tf.nn.l2_loss(
t,
name=None
)
Args:
- t: 一个
Tensor
. 其types
为: half, bfloat16, float32, float64. - name: A name for the operation (optional).
- 作用:不使用sqrt计算张量的L2范数的一半:
output = sum(t ** 2) / 2
代码:
x = tf.constant([1,2,3],dtype=tf.float32)
with tf.Session() as sess:
print(sess.run(tf.nn.l2_loss(x)))
输出: 7.0
x = tf.constant([[1,2,3],[4,5,6]],dtype=tf.float32)
with tf.Session() as sess:
print(sess.run(tf.nn.l2_loss(x)))
print(sess.run(tf.reduce_sum(tf.square(x)/2)))
输出一致:
45.5
45.5