Created
January 17, 2018 17:39
-
-
Save ericjang/ba073588dbf5250d113c64528c44b96b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# quite easy to interpret - multiplying by alpha causes a contraction in volume. | |
class LeakyReLU(tfb.Bijector): | |
def __init__(self, alpha=0.5, validate_args=False, name="leaky_relu"): | |
super(LeakyReLU, self).__init__( | |
event_ndims=1, validate_args=validate_args, name=name) | |
self.alpha = alpha | |
def _forward(self, x): | |
return tf.where(tf.greater_equal(x, 0), x, self.alpha * x) | |
def _inverse(self, y): | |
return tf.where(tf.greater_equal(y, 0), y, 1. / self.alpha * y) | |
def _inverse_log_det_jacobian(self, y): | |
event_dims = self._event_dims_tensor(y) | |
I = tf.ones_like(y) | |
J_inv = tf.where(tf.greater_equal(y, 0), I, 1.0 / self.alpha * I) | |
# abs is actually redundant here, since this det Jacobian is > 0 | |
log_abs_det_J_inv = tf.log(tf.abs(J_inv)) | |
return tf.reduce_sum(log_abs_det_J_inv, axis=event_dims) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment