1 """Implements various tensorflow loss layer.
4 from __future__ import absolute_import
5 from __future__ import division
6 from __future__ import print_function
8 import tensorflow as tf
9 import tensorflow.contrib.slim as slim
11 def l1_loss(predictions, targets):
12 """Implements tensorflow l1 loss.
16 total_elements = (tf.shape(targets)[0] * tf.shape(targets)[1] * tf.shape(targets)[2]
17 * tf.shape(targets)[3])
18 total_elements = tf.to_float(total_elements)
20 loss = tf.reduce_sum(tf.abs(predictions- targets))
21 loss = tf.div(loss, total_elements)
24 def l2_loss(predictions, targets):
25 """Implements tensorflow l2 loss, normalized by number of elements.
29 total_elements = (tf.shape(targets)[0] * tf.shape(targets)[1] * tf.shape(targets)[2]
30 * tf.shape(targets)[3])
31 total_elements = tf.to_float(total_elements)
33 loss = tf.reduce_sum(tf.square(predictions-targets))
34 loss = tf.div(loss, total_elements)
40 def vae_loss(z_mean, z_logvar, prior_weight=1.0):
41 """Implements the VAE reguarlization loss.
43 total_elements = (tf.shape(z_mean)[0] * tf.shape(z_mean)[1] * tf.shape(z_mean)[2]
44 * tf.shape(z_mean)[3])
45 total_elements = tf.to_float(total_elements)
47 vae_loss = -0.5 * tf.reduce_sum(1.0 + z_logvar - tf.square(z_mean) - tf.exp(z_logvar))
48 vae_loss = tf.div(vae_loss, total_elements)