-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathlosses.py
50 lines (36 loc) · 1.54 KB
/
losses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
""" Loss functions.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
# TODO: check to see if we can separate generator
class GanTypes:
jsd = "jsd" # Jensen-Shannon Divergence aka Maximum Likelihood Estimate.
emd = "emd" # Earth Mover's Distance aka Wasserstein Distance.
ls = "ls" # Least Squares Distance.
def gan_loss(d_logits_real, d_logits_fake, gan_type="jsd"):
# TODO: add namespace
if gan_type == "jsd":
# TODO: use official tensorflow ops (sigmoid_cross_entropy)
d_probs_real = tf.sigmoid(d_logits_real)
d_probs_fake = tf.sigmoid(d_logits_fake)
d_loss_real = tf.log(d_probs_real)
d_loss_fake = tf.log(1. - d_probs_fake)
d_loss = -tf.reduce_mean(d_loss_real + d_loss_fake)
g_loss = -tf.reduce_mean(d_loss_fake)
elif gan_type == "emd":
# batch size should be first dim
# d_mean_real = tf.reduce_mean(d_logits_real, axis=-1)
# d_mean_fake = tf.reduce_mean(d_logits_fake, axis=-1)
d_loss = tf.reduce_mean(d_logits_real - d_logits_fake)
g_loss = -tf.reduce_mean(d_logits_fake)
elif gan_type == "ls":
# TODO: averaging over batch?
d_sq_real = (d_logits_real - 1.) ** 2.
d_sq_fake = d_logits_fake ** 2.
d_loss = 0.5 * (tf.reduce_mean(d_sq_real) + tf.reduce_mean(d_sq_fake))
g_loss = 0.5 * tf.reduce_mean((d_logits_fake - 1.) ** 2.)
else:
raise ValueError("Wrong gan_type.")
return d_loss, g_loss