Dear Edwar’s users.
I need a custom random variable to incorporate to my model. Specifically a 3D random variable with x and y coordinates distributed uniformly inside the unit disk, and z coordinates like a Normal distribution. Here my code (doing copy-paste from other distributions like the Normal distribution)
from edward.models import RandomVariable
from tensorflow.contrib.distributions import Distribution
import tensorflow as tf
class CustomRandomVariable(RandomVariable, Distribution):
def __init__(self, loc, scale, validate_args=False,
allow_nan_stats=True, name="CustomRandomVariable"):
super(CustomRandomVariable, self).__init__(
loc,
scale,
validate_args=validate_args,
allow_nan_stats=allow_nan_stats,
name=name)
def _log_prob(self, value):
raise NotImplementedError("log_prob is not implemented")
def _sample_n(self, n, seed=None):
a = tf.contrib.distributions.Uniform(low=0.0,high=1.0)
b = tf.contrib.distributions.Uniform(low=0.0,high=6.283185)
x = tf.sqrt(a)*tf.cos(b)
y = tf.sqrt(a)*tf.sin(b)
z = tf.contrib.distributions.Normal(loc=self.loc, scale=self.scale)
return tf.concat([x,y,z],0)
Then I obtain the following error. Please, any help will be welcome!
>>> from Fmodule import CustomRandomVariable
>>> x = CustomRandomVariable(loc=1.0,scale=2.0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "Fmodule.py", line 23, in __init__
name=name)
File "build/bdist.linux-x86_64/egg/edward/models/random_variable.py", line 119, in __init__
File "/home/angel/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/distributions/python/ops/distribution.py", line 659, in sample
return self._call_sample_n(sample_shape, seed, name)
File "/home/angel/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/distributions/python/ops/distribution.py", line 638, in _call_sample_n
samples = self._sample_n(n, seed, **kwargs)
File "Fmodule.py", line 31, in _sample_n
x = tf.sqrt(a)*tf.cos(b)
File "/home/angel/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 434, in sqrt
return gen_math_ops.sqrt(x, name=name)
File "/home/angel/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 2545, in sqrt
result = _op_def_lib.apply_op("Sqrt", x=x, name=name)
File "/home/angel/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 494, in apply_op
raise err
TypeError: Expected binary or unicode string, got <tensorflow.contrib.distributions.python.ops.uniform.Uniform object at 0x7f3992245990>