Skip to content

← random package

CreateRandomSeed

Create a (splittable) random seed for deterministic random number generation.

Random operations, such as RandomNormal will by default use a global random state, which makes them non-deterministic in practice. Sometimes this is fine, but for many applications it is necessary to have deterministic random numbers, and to do so even in parallel computing contexts. Therefore all random operations optionally accept a "seed", which ensures that the operation will produce the same results every time it is run. For sequences of random operations one therefore has to generate new seeds for each successive operation from an initial seed to prevent them from all producing the same results. One might naively just use RandomIntegers (randint) to generate multiple fresh integer seeds for this purpose, but this is a flawed approach (the computations can yield shifted but identical random numbers). Instead, NeuroPype provides the "Split Random Seed" node for this purpose, which uses a key splitting algorithm. Additionally there are a number of convenience facilities to avoid having to manually deal with key splitting, including 1) the ability to draw seeds "out of thin air" as needed using the "Draw Random Seed" (DrawRandomSeed) node. This requires that there is a "With Random Seed" (WithRandomSeed) node somewhere downstream in the pipeline, which takes a global randseed and supplies a derived sequence of seeds to nodes that run under its managed context). As a result of using key splitting, NeuroPype's seeds are arrays (which have sufficient entropy) rather than integers, and arrays of keys (generated by some nodes) come as 2d arrays. Lastly, there is also the ability to mix an existing seed and additional data, such as a worker or task ID, to generate a derived seed that will be unique yet deterministic for each parallel task, using the "Derive Random Seed" (DeriveRandomSeed) node. Another handy node is RandomSeedIterator, which can be used just like e.g., ListIterator or PathIterator to iterate over a series of successive seeds without the need for WithRandomSeed. Different compute backends tend to have their own implementation of splittable seeds, and for this reason it is a good practice to use the same backend for both the seed management and the random operations, but this is only relevant if you are overriding compute backends (this is very easy using the With Backend node, which runs all operations under it on a specified backend). Version 0.5.0

Ports/Properties

key

Splittable random seed.

  • verbose name: Key
  • default value: None
  • port type: DataPort
  • value type: AnyArray (can be None)
  • data direction: OUT

seed

Initial random seed.

  • verbose name: Seed
  • default value: 1234
  • port type: IntPort
  • value type: int (can be None)

algorithm

Random number generation algorithm for which to generate a seed. The default depends on the chosen backend, and not all backends support all algorithms. For this reason it is recommended to use the default.

  • verbose name: Algorithm
  • default value: default
  • port type: EnumPort
  • value type: str (can be None)

backend

Optional compute backend for which to generate the seed. Keep is the current default, which is typically numpy, but which can be overridden in contexts that require it. Some backends (notably jax and tensorflow) require that the seed be generated using the same backend as the downstream random operation. For this reason, it is a good idea to use the same backend for both the seed management and the random operation to future-proof one's pipeline.

  • verbose name: Backend
  • default value: keep
  • port type: EnumPort
  • value type: str (can be None)

set_breakpoint

Set a breakpoint on this node. If this is enabled, your debugger (if one is attached) will trigger a breakpoint.

  • verbose name: Set Breakpoint (Debug Only)
  • default value: False
  • port type: BoolPort
  • value type: bool (can be None)

metadata

User-definable meta-data associated with the node. Usually reserved for technical purposes.

  • verbose name: Metadata
  • default value: {}
  • port type: DictPort
  • value type: dict (can be None)