Skip to content

Commit

Permalink
Merge pull request #1188 from cleverhans-lab/laplace_bug
Browse files Browse the repository at this point in the history
Fix Laplace distribution and document params
  • Loading branch information
alkaet authored Jan 22, 2021
2 parents 8c095c9 + f875a69 commit f5adfdd
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions cleverhans/utils_tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,11 @@ def random_exponential(shape, rate=1.0, dtype=tf.float32, seed=None):
"""
Helper function to sample from the exponential distribution, which is not
included in core TensorFlow.
:shape: shape of the sampled tensor.
:rate: (optional) rate parameter of the exponential distribution, defaults to 1.0.
:dtype: (optional) data type of the sempled tensor, defaults to tf.float32.
:seed: (optional) custom seed to be used for sampling.
"""
return tf.random_gamma(shape, alpha=1, beta=1. / rate, dtype=dtype, seed=seed)

Expand All @@ -452,10 +457,16 @@ def random_laplace(shape, loc=0.0, scale=1.0, dtype=tf.float32, seed=None):
"""
Helper function to sample from the Laplace distribution, which is not
included in core TensorFlow.
"""
z1 = random_exponential(shape, loc, dtype=dtype, seed=seed)
z2 = random_exponential(shape, scale, dtype=dtype, seed=seed)
return z1 - z2
:shape: shape of the sampled tensor.
:loc: (optional) mean of the laplace distribution, defaults to 0.0.
:scale: (optional) scale parameter of the laplace diustribution, defaults to 1.0.
:dtype: (optional) data type of the sempled tensor, defaults to tf.float32.
:seed: (optional) custom seed to be used for sampling.
"""
z1 = random_exponential(shape, 1. / scale, dtype=dtype, seed=seed)
z2 = random_exponential(shape, 1. / scale, dtype=dtype, seed=seed)
return z1 - z2 + loc


def random_lp_vector(shape, ord, eps, dtype=tf.float32, seed=None):
Expand Down

0 comments on commit f5adfdd

Please sign in to comment.