shared_randomstreams – Friendly random numbers

Guide

Since Theano uses a functional design, producing pseudo-random numbers in agraph is not quite as straightforward as it is in numpy.

The way to think about putting randomness into Theano’s computations is toput random variables in your graph. Theano will allocate a numpy RandomStateobject for each such variable, and draw from it as necessary. We will call this sort of sequence ofrandom numbers a random stream.

For an example of how to use random numbers, seeUsing Random Numbers.

Reference

  • class theano.tensor.sharedrandomstreams.RandomStreams(_raw_random.RandomStreamsBase)[source]
  • This is a symbolic stand-in for numpy.random.RandomState.Random variables of various distributions are instantiated by calls toparent class raw_random.RandomStreamsBase.

Returns:a list of all the (state, new_state) update pairs for therandom variables created by this object

This can be a convenient shortcut to enumerating all the randomvariables in a large graph in the update parameter of function.

  • seed(meta_seed)[source]
  • meta_seed will be used to seed a temporary random number generator,that will in turn generate seeds for all random variablescreated by this object (via gen).

Returns:None

  • gen(op, *args, **kwargs)[source]
  • Return the random variable from op(args, *kwargs), butalso install special attributes (.rng and update, seeRandomVariable ) into it.

This function also adds the returned variable to an internal list sothat it can be seeded later by a call to seed.

  • uniform, normal, binomial, multinomial, random_integers, …
  • See raw_random.RandomStreamsBase.
  • class theano.tensor.sharedrandomstreams.RandomVariable(_object)[source]
    • rng[source]
    • The shared variable whose .value is the numpy RandomStategenerator feeding this random variable.

    • update[source]

    • A pairwhose first element is a shared variable whose value is a numpy RandomState,and whose second element is an [symbolic] expression for the next value of thatRandomState after drawing samples.Including this pair in theupdates list to function will cause thefunction to update the random number generator feeding this variable.