Python tensorflow.random_gamma() Examples

The following are 14 code examples of tensorflow.random_gamma(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow , or try the search function .
Example #1
Source File: random_gamma_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testShape(self):
    # Fully known shape.
    rnd = tf.random_gamma([150], 2.0)
    self.assertEqual([150], rnd.get_shape().as_list())
    rnd = tf.random_gamma([150], 2.0, beta=[3.0, 4.0])
    self.assertEqual([150, 2], rnd.get_shape().as_list())
    rnd = tf.random_gamma([150], tf.ones([1, 2, 3]))
    self.assertEqual([150, 1, 2, 3], rnd.get_shape().as_list())
    rnd = tf.random_gamma([20, 30], tf.ones([1, 2, 3]))
    self.assertEqual([20, 30, 1, 2, 3], rnd.get_shape().as_list())
    rnd = tf.random_gamma([123], tf.placeholder(tf.float32, shape=(2,)))
    self.assertEqual([123, 2], rnd.get_shape().as_list())
    # Partially known shape.
    rnd = tf.random_gamma(tf.placeholder(tf.int32, shape=(1,)), tf.ones([7, 3]))
    self.assertEqual([None, 7, 3], rnd.get_shape().as_list())
    rnd = tf.random_gamma(tf.placeholder(tf.int32, shape=(3,)), tf.ones([9, 6]))
    self.assertEqual([None, None, None, 9, 6], rnd.get_shape().as_list())
    # Unknown shape.
    rnd = tf.random_gamma(tf.placeholder(tf.int32), tf.placeholder(tf.float32))
    self.assertIs(None, rnd.get_shape().ndims)
    rnd = tf.random_gamma([50], tf.placeholder(tf.float32))
    self.assertIs(None, rnd.get_shape().ndims) 
Example #2
Source File: multivariate.py    From zhusuan with MIT License 5 votes vote down vote up
def _sample(self, n_samples):
        samples = tf.random_gamma([n_samples], self.alpha,
                                  beta=1, dtype=self.dtype)
        return samples / tf.reduce_sum(samples, -1, keepdims=True) 
Example #3
Source File: univariate.py    From zhusuan with MIT License 5 votes vote down vote up
def _sample(self, n_samples):
        return tf.random_gamma([n_samples], self.alpha,
                               beta=self.beta, dtype=self.dtype) 
Example #4
Source File: univariate.py    From zhusuan with MIT License 5 votes vote down vote up
def _sample(self, n_samples):
        alpha, beta = maybe_explicit_broadcast(
            self.alpha, self.beta, 'alpha', 'beta')
        x = tf.random_gamma([n_samples], alpha, beta=1, dtype=self.dtype)
        y = tf.random_gamma([n_samples], beta, beta=1, dtype=self.dtype)
        return x / (x + y) 
Example #5
Source File: univariate.py    From zhusuan with MIT License 5 votes vote down vote up
def _sample(self, n_samples):
        gamma = tf.random_gamma([n_samples], self.alpha,
                                beta=self.beta, dtype=self.dtype)
        return 1 / gamma 
Example #6
Source File: random_gamma_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def _Sampler(self, num, alpha, beta, dtype, use_gpu, seed=None):

    def func():
      with self.test_session(use_gpu=use_gpu, graph=tf.Graph()) as sess:
        rng = tf.random_gamma([num], alpha, beta=beta, dtype=dtype, seed=seed)
        ret = np.empty([10, num])
        for i in xrange(10):
          ret[i, :] = sess.run(rng)
      return ret

    return func 
Example #7
Source File: random_gamma_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testNoCSE(self):
    """CSE = constant subexpression eliminator.

    SetIsStateful() should prevent two identical random ops from getting
    merged.
    """
    for dtype in tf.float16, tf.float32, tf.float64:
      for use_gpu in [False, True]:
        with self.test_session(use_gpu=use_gpu):
          rnd1 = tf.random_gamma([24], 2.0, dtype=dtype)
          rnd2 = tf.random_gamma([24], 2.0, dtype=dtype)
          diff = rnd2 - rnd1
          self.assertGreater(np.linalg.norm(diff.eval()), 0.1) 
Example #8
Source File: tensorboard_output.py    From gym-sawyer with MIT License 5 votes vote down vote up
def _get_histogram_var_by_type(self,
                                   histogram_type,
                                   shape,
                                   name=None,
                                   **kwargs):
        with tf.name_scope(name, "get_hist_{}".format(histogram_type)):
            if histogram_type == "normal":
                # Make a normal distribution, with a shifting mean
                mean = tf.Variable(kwargs['mean'])
                stddev = tf.Variable(kwargs['stddev'])
                return tf.random_normal(
                    shape=shape, mean=mean, stddev=stddev), [mean, stddev]
            elif histogram_type == "gamma":
                # Add a gamma distribution
                alpha = tf.Variable(kwargs['alpha'])
                return tf.random_gamma(shape=shape, alpha=alpha), [alpha]
            elif histogram_type == "poisson":
                lam = tf.Variable(kwargs['lam'])
                return tf.random_poisson(shape=shape, lam=lam), [lam]
            elif histogram_type == "uniform":
                # Add a uniform distribution
                maxval = tf.Variable(kwargs['maxval'])
                return tf.random_uniform(shape=shape, maxval=maxval), [maxval]

            raise Exception('histogram type error %s' % histogram_type,
                            'builtin type', self._histogram_distribute_list) 
Example #9
Source File: tensorflow.py    From deepx with MIT License 5 votes vote down vote up
def random_gamma(self, shape, alpha, beta=None):
        return tf.random_gamma(shape, alpha, beta=beta)
        pass 
Example #10
Source File: elementary.py    From elbow with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _sample(self, alpha, beta):
        gammas = tf.random_gamma(shape=self.shape, alpha=alpha, beta=beta)
        return gammas 
Example #11
Source File: elementary.py    From elbow with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _sample(self, alpha, beta):
        X = tf.random_gamma(shape=self.shape, alpha=alpha, beta=1)
        Y = tf.random_gamma(shape=self.shape, alpha=beta, beta=1)
        Z = X/(X+Y)
        return Z 
Example #12
Source File: elementary.py    From elbow with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _sample(self, alpha):

        # broadcast alpha from scalar to vector, if necessary
        alpha = alpha * tf.ones(shape=self.shape, dtype=self.dtype)

        gammas = tf.squeeze(tf.random_gamma(shape=(1,), alpha=alpha, beta=1))
        sample = RowNormalize.transform(gammas)
        return sample 
Example #13
Source File: pcrl.py    From cornac with Apache License 2.0 5 votes vote down vote up
def sample_pi(self, alpha, beta):
        Gam = tf.random_gamma([1], alpha=alpha, beta=beta, name="Gam", seed=None)[0]
        return self.G_inv(Gam, alpha, beta)

    # shape augmentation 
Example #14
Source File: utils_tf.py    From cleverhans with MIT License 5 votes vote down vote up
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.
  """
  return tf.random_gamma(shape, alpha=1, beta=1. / rate, dtype=dtype, seed=seed)