tensorflow API:tf.nn.l2_loss

tf.nn.l2_loss(
    t,
    name=None
)

Args:

  • t: one Tensor. It typesis: half, bfloat16, float32, float64.
  • name: A name for the operation (optional).
  • Effect: Calculate half of the L2 norm of a tensor without using sqrt:
output = sum(t ** 2) / 2

Code:

x = tf.constant([1,2,3],dtype=tf.float32)
with tf.Session() as sess:
    print(sess.run(tf.nn.l2_loss(x)))

output: 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)))

The output is the same:

45.5
45.5

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325837989&siteId=291194637