]> git.sesse.net Git - voxel-flow/blob - utils/loss_utils.py
Initial commit
[voxel-flow] / utils / loss_utils.py
1 """Implements various tensorflow loss layer.
2 """
3
4 from __future__ import absolute_import
5 from __future__ import division
6 from __future__ import print_function
7
8 import tensorflow as tf
9 import tensorflow.contrib.slim as slim
10
11 def l1_loss(predictions, targets):
12   """Implements tensorflow l1 loss.
13   Args:
14   Returns:
15   """
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)
19
20   loss = tf.reduce_sum(tf.abs(predictions- targets))
21   loss = tf.div(loss, total_elements)
22   return loss
23
24 def l2_loss(predictions, targets):
25   """Implements tensorflow l2 loss, normalized by number of elements.
26   Args:
27   Returns:
28   """
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)
32
33   loss = tf.reduce_sum(tf.square(predictions-targets))
34   loss = tf.div(loss, total_elements)
35   return loss
36
37 def tv_loss():
38   #TODO
39   pass
40 def vae_loss(z_mean, z_logvar, prior_weight=1.0):
41   """Implements the VAE reguarlization loss.
42   """
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)
46
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)
49   return vae_loss
50
51 def bilateral_loss():
52   #TODO
53   pass