Skip to content

tenzr::random

Classes

Name
class tenzr::random::Generator
Mutex-protected pseudo-random number generator built on a 64-bit Mersenne twister.
struct tenzr::random::PRNGKey
Functional pseudo-random key formed by two 32-bit words.
struct tenzr::random::SplitKeys
Two independently derived PRNG keys.

Functions

Name
Generator & DefaultGenerator()
TENZR_APIPRNGKey Key(uint64_t seed)
TENZR_API std::pair< uint32_t, uint32_t > Threefry2x32(uint32_t k0, uint32_t k1, uint32_t c0, uint32_t c1)
TENZR_APISplitKeys Split(const PRNGKey & k)
TENZR_API std::vector< PRNGKey > Split(const PRNGKey & k, size_t num)
TENZR_APIPRNGKey FoldIn(const PRNGKey & k, uint32_t data)
TENZR_API std::vector< uint32_t > ThreefryRandomBits(const PRNGKey & k, size_t count)
TENZR_APIArray Uniform(const PRNGKey & key, const Shape & shape, float low =0.0f, float high =1.0f, DType dtype =DType::kFloat32)
TENZR_APIArray Normal(const PRNGKey & key, const Shape & shape, float mean =0.0f, float stddev =1.0f, DType dtype =DType::kFloat32)
TENZR_APIArray Bernoulli(const PRNGKey & key, const Shape & shape, float p =0.5f)
TENZR_APIArray Rademacher(const PRNGKey & key, const Shape & shape, DType dtype =DType::kFloat32)
TENZR_APIArray Categorical(const PRNGKey & key, const Array & logits, const Shape & shape)
TENZR_APIArray GlorotUniform(const PRNGKey & key, const Shape & shape, int64_t fan_in, int64_t fan_out, DType dtype =DType::kFloat32)
TENZR_APIArray GlorotNormal(const PRNGKey & key, const Shape & shape, int64_t fan_in, int64_t fan_out, DType dtype =DType::kFloat32)
TENZR_APIArray KaimingUniform(const PRNGKey & key, const Shape & shape, int64_t fan_in, DType dtype =DType::kFloat32)
TENZR_APIArray KaimingNormal(const PRNGKey & key, const Shape & shape, int64_t fan_in, DType dtype =DType::kFloat32)
void Seed(uint64_t s)
void Seed()
Reseed the default generator from a non-deterministic source.
Tensor Uniform(Shape shape, float low =0.0f, float high =1.0f, DType dtype =DType::kFloat32, Generator & gen =DefaultGenerator())
Tensor Normal(Shape shape, float mean =0.0f, float stddev =1.0f, DType dtype =DType::kFloat32, Generator & gen =DefaultGenerator())
Tensor Bernoulli(Shape shape, float p =0.5f, Generator & gen =DefaultGenerator())
Tensor GlorotUniform(Shape shape, int64_t fan_in, int64_t fan_out, DType dtype, Generator & gen =DefaultGenerator())
Glorot uniform initialization with an explicit dtype.
Tensor GlorotUniform(Shape shape, int64_t fan_in, int64_t fan_out, Generator & gen =DefaultGenerator())
Glorot uniform initialization.
Tensor GlorotNormal(Shape shape, int64_t fan_in, int64_t fan_out, DType dtype, Generator & gen =DefaultGenerator())
Glorot normal initialization with an explicit dtype.
Tensor GlorotNormal(Shape shape, int64_t fan_in, int64_t fan_out, Generator & gen =DefaultGenerator())
Glorot normal initialization.
Tensor KaimingUniform(Shape shape, int64_t fan_in, DType dtype, Generator & gen =DefaultGenerator())
Kaiming uniform initialization with an explicit dtype.
Tensor KaimingUniform(Shape shape, int64_t fan_in, Generator & gen =DefaultGenerator())
Kaiming uniform initialization.
Tensor KaimingNormal(Shape shape, int64_t fan_in, DType dtype, Generator & gen =DefaultGenerator())
Kaiming normal initialization with an explicit dtype.
Tensor KaimingNormal(Shape shape, int64_t fan_in, Generator & gen =DefaultGenerator())
Kaiming normal initialization.
Tensor Rand(Shape shape, Generator & gen =DefaultGenerator())
Tensor Rand(Shape shape, float low, float high, Generator & gen =DefaultGenerator())
Tensor Randn(Shape shape, Generator & gen =DefaultGenerator())
Tensor Randn(Shape shape, float mean, float stddev, Generator & gen =DefaultGenerator())

Functions Documentation

function DefaultGenerator

Generator & DefaultGenerator()

Return:

  • The process-wide default generator.
  • Reference to the singleton Generator.

Get the process-wide default generator.

function Key

TENZR_APIPRNGKey Key(
    uint64_t seed
)

Parameters:

  • seed Seed value.
  • seed_val Seed value.

Return:

  • The derived key.
  • The derived key.

Create a PRNGKey from a 64-bit integer seed. Deterministically derive a PRNG key from a 64-bit seed.

function Threefry2x32

TENZR_API std::pair< uint32_t, uint32_t > Threefry2x32(
    uint32_t k0,
    uint32_t k1,
    uint32_t c0,
    uint32_t c1
)

Parameters:

  • k0 Key high 32 bits.
  • k1 Key low 32 bits.
  • c0 Counter high 32 bits.
  • c1 Counter low 32 bits.
  • k0 First key word.
  • k1 Second key word.
  • c0 First counter word.
  • c1 Second counter word.

Return:

  • The (high, low) hashed word.
  • The two generated 32-bit words.

Evaluate the 20-round Threefry 2x32 counter-based PRNG hash. Apply the 20-round Threefry 2x32 counter-based PRNG to a key and counter pair.

function Split

TENZR_APISplitKeys Split(
    const PRNGKey & k
)

Parameters:

  • k Key to split.
  • k Key to split.

Return:

  • The pair of derived keys.
  • Pair of derived keys.

Split a key into two fresh independent keys. Split a key into two independent keys.

function Split

TENZR_API std::vector< PRNGKey > Split(
    const PRNGKey & k,
    size_t num
)

Parameters:

  • k Key to split.
  • num Number of keys to produce.
  • k Key to split.
  • num Number of keys to derive; must be positive.

Return:

  • The derived keys.
  • The derived keys.

Split a key into an arbitrary number of fresh independent sub-keys. Split a key into num independent keys.

function FoldIn

TENZR_APIPRNGKey FoldIn(
    const PRNGKey & k,
    uint32_t data
)

Parameters:

  • k Key to fold into.
  • data 32-bit data word.
  • k Base key.
  • data Data word mixed into the counter.

Return:

  • The folded key.
  • The derived key.

Deterministically fold arbitrary 32-bit integer data into an existing key. Fold integer data into a key to derive a new key.

function ThreefryRandomBits

TENZR_API std::vector< uint32_t > ThreefryRandomBits(
    const PRNGKey & k,
    size_t count
)

Parameters:

  • k Key to draw from.
  • count Number of words to produce.
  • k Key to draw from.
  • count Number of words needed.

Return:

  • The generated words.
  • The generated words.

Generate count random 32-bit unsigned words from a key using Threefry2x32. Generate count pseudorandom 32-bit words from a key.

function Uniform

TENZR_APIArray Uniform(
    const PRNGKey & key,
    const Shape & shape,
    float low =0.0f,
    float high =1.0f,
    DType dtype =DType::kFloat32
)

Parameters:

  • key PRNG key.
  • shape Output shape.
  • low Inclusive lower bound.
  • high Exclusive upper bound.
  • dtype Output dtype.
  • key Key to draw from.
  • shape Output shape.
  • low Inclusive lower bound.
  • high Exclusive upper bound.
  • dtype Result dtype (kFloat32 or Float64).

Return:

  • Sampled array.
  • The sampled array.

Sample uniform random values in [low, high). Draw a uniform sample over [low, high) using a functional key.

function Normal

TENZR_APIArray Normal(
    const PRNGKey & key,
    const Shape & shape,
    float mean =0.0f,
    float stddev =1.0f,
    DType dtype =DType::kFloat32
)

Parameters:

  • key PRNG key.
  • shape Output shape.
  • mean Distribution mean.
  • stddev Distribution standard deviation.
  • dtype Output dtype.
  • key Key to draw from.
  • shape Output shape.
  • mean Distribution mean.
  • stddev Distribution standard deviation, which must be positive.
  • dtype Result dtype (kFloat32 or Float64).

Return:

  • Sampled array.
  • The sampled array.

Sample normal (Gaussian) values with the given mean and standard deviation. Draw a normal sample via Box-Muller using a functional key.

function Bernoulli

TENZR_APIArray Bernoulli(
    const PRNGKey & key,
    const Shape & shape,
    float p =0.5f
)

Parameters:

  • key PRNG key.
  • shape Output shape.
  • p Probability of true.
  • key Key to draw from.
  • shape Output shape.
  • p Success probability, in [0, 1].

Return:

  • Boolean array.
  • The sampled boolean array.

Sample booleans that are true with probability p. Draw a boolean Bernoulli sample using a functional key.

function Rademacher

TENZR_APIArray Rademacher(
    const PRNGKey & key,
    const Shape & shape,
    DType dtype =DType::kFloat32
)

Parameters:

  • key PRNG key.
  • shape Output shape.
  • dtype Output dtype.
  • key Key to draw from.
  • shape Output shape.
  • dtype Result dtype (kFloat32 or Float64).

Return:

  • Sampled array.
  • The sampled array.

Sample Rademacher values (+1 or -1 with equal probability). Draw a Rademacher sample of +1/-1 using a functional key.

function Categorical

TENZR_APIArray Categorical(
    const PRNGKey & key,
    const Array & logits,
    const Shape & shape
)

Parameters:

  • key PRNG key.
  • logits Unnormalized class scores.
  • shape Output shape.
  • key Key to draw from.
  • logits Unnormalized class scores; the last dimension is the class axis.
  • shape Output shape; its element count is the number of samples.

Return:

  • Sampled class indices.
  • Int32 array of sampled class indices.

Draw categorical samples from log-probabilities via the Gumbel-max trick. Draw class indices from a categorical distribution over logits.

function GlorotUniform

TENZR_APIArray GlorotUniform(
    const PRNGKey & key,
    const Shape & shape,
    int64_t fan_in,
    int64_t fan_out,
    DType dtype =DType::kFloat32
)

Parameters:

  • key PRNG key.
  • shape Output shape.
  • fan_in Number of input units.
  • fan_out Number of output units.
  • dtype Output dtype.
  • key Key to draw from.
  • shape Output shape.
  • fan_in Input fan size.
  • fan_out Output fan size.
  • dtype Result dtype.

Return:

  • Initialized array.
  • The initialized array.

Glorot/Xavier uniform initialization. Glorot (Xavier) uniform initialization using a functional key.

function GlorotNormal

TENZR_APIArray GlorotNormal(
    const PRNGKey & key,
    const Shape & shape,
    int64_t fan_in,
    int64_t fan_out,
    DType dtype =DType::kFloat32
)

Parameters:

  • key PRNG key.
  • shape Output shape.
  • fan_in Number of input units.
  • fan_out Number of output units.
  • dtype Output dtype.
  • key Key to draw from.
  • shape Output shape.
  • fan_in Input fan size.
  • fan_out Output fan size.
  • dtype Result dtype.

Return:

  • Initialized array.
  • The initialized array.

Glorot/Xavier normal initialization. Glorot (Xavier) normal initialization using a functional key.

function KaimingUniform

TENZR_APIArray KaimingUniform(
    const PRNGKey & key,
    const Shape & shape,
    int64_t fan_in,
    DType dtype =DType::kFloat32
)

Parameters:

  • key PRNG key.
  • shape Output shape.
  • fan_in Number of input units.
  • dtype Output dtype.
  • key Key to draw from.
  • shape Output shape.
  • fan_in Input fan size.
  • dtype Result dtype.

Return:

  • Initialized array.
  • The initialized array.

Kaiming/He uniform initialization. Kaiming (He) uniform initialization using a functional key.

function KaimingNormal

TENZR_APIArray KaimingNormal(
    const PRNGKey & key,
    const Shape & shape,
    int64_t fan_in,
    DType dtype =DType::kFloat32
)

Parameters:

  • key PRNG key.
  • shape Output shape.
  • fan_in Number of input units.
  • dtype Output dtype.
  • key Key to draw from.
  • shape Output shape.
  • fan_in Input fan size.
  • dtype Result dtype.

Return:

  • Initialized array.
  • The initialized array.

Kaiming/He normal initialization. Kaiming (He) normal initialization using a functional key.

function Seed

void Seed(
    uint64_t s
)

Parameters:

  • s New seed.
  • s New seed value.

Reseed the default generator. Reseed the default generator deterministically.

function Seed

void Seed()

Reseed the default generator from a non-deterministic source.

Reseed the default generator from system randomness.

function Uniform

Tensor Uniform(
    Shape shape,
    float low =0.0f,
    float high =1.0f,
    DType dtype =DType::kFloat32,
    Generator & gen =DefaultGenerator()
)

Parameters:

  • shape Output shape.
  • low Inclusive lower bound.
  • high Exclusive upper bound.
  • dtype Output dtype.
  • gen Generator to draw from.
  • shape Output shape.
  • low Inclusive lower bound.
  • high Exclusive upper bound.
  • dtype Result dtype (kFloat32 or Float64).
  • gen Stateful generator to draw from.

Return:

  • Sampled array.
  • The sampled tensor.

Sample uniform values in [low, high) from gen. Draw a uniform tensor from a stateful generator.

function Normal

Tensor Normal(
    Shape shape,
    float mean =0.0f,
    float stddev =1.0f,
    DType dtype =DType::kFloat32,
    Generator & gen =DefaultGenerator()
)

Parameters:

  • shape Output shape.
  • mean Distribution mean.
  • stddev Distribution standard deviation.
  • dtype Output dtype.
  • gen Generator to draw from.
  • shape Output shape.
  • mean Distribution mean.
  • stddev Distribution standard deviation, which must be positive.
  • dtype Result dtype (kFloat32 or Float64).
  • gen Stateful generator to draw from.

Return:

  • Sampled array.
  • The sampled tensor.

Sample normal values from gen. Draw a normal tensor from a stateful generator.

function Bernoulli

Tensor Bernoulli(
    Shape shape,
    float p =0.5f,
    Generator & gen =DefaultGenerator()
)

Parameters:

  • shape Output shape.
  • p Probability of true.
  • gen Generator to draw from.
  • shape Output shape.
  • p Success probability, in [0, 1].
  • gen Stateful generator to draw from.

Return:

  • Boolean array.
  • The sampled 0.0/1.0 tensor.

Sample booleans from gen. Draw a kFloat32 Bernoulli mask from a stateful generator.

function GlorotUniform

Tensor GlorotUniform(
    Shape shape,
    int64_t fan_in,
    int64_t fan_out,
    DType dtype,
    Generator & gen =DefaultGenerator()
)

Glorot uniform initialization with an explicit dtype.

Parameters:

  • shape Output shape.
  • fan_in Input fan size.
  • fan_out Output fan size.
  • dtype Result dtype.
  • gen Stateful generator to draw from.

Return: The initialized tensor.

Glorot uniform initialization from a stateful generator.

function GlorotUniform

Tensor GlorotUniform(
    Shape shape,
    int64_t fan_in,
    int64_t fan_out,
    Generator & gen =DefaultGenerator()
)

Glorot uniform initialization.

Parameters:

  • shape Output shape.
  • fan_in Input fan size.
  • fan_out Output fan size.
  • gen Stateful generator to draw from.

Return: The initialized tensor.

Glorot uniform initialization with a kFloat32 default dtype.

function GlorotNormal

Tensor GlorotNormal(
    Shape shape,
    int64_t fan_in,
    int64_t fan_out,
    DType dtype,
    Generator & gen =DefaultGenerator()
)

Glorot normal initialization with an explicit dtype.

Parameters:

  • shape Output shape.
  • fan_in Input fan size.
  • fan_out Output fan size.
  • dtype Result dtype.
  • gen Stateful generator to draw from.

Return: The initialized tensor.

Glorot normal initialization from a stateful generator.

function GlorotNormal

Tensor GlorotNormal(
    Shape shape,
    int64_t fan_in,
    int64_t fan_out,
    Generator & gen =DefaultGenerator()
)

Glorot normal initialization.

Parameters:

  • shape Output shape.
  • fan_in Input fan size.
  • fan_out Output fan size.
  • gen Stateful generator to draw from.

Return: The initialized tensor.

Glorot normal initialization with a kFloat32 default dtype.

function KaimingUniform

Tensor KaimingUniform(
    Shape shape,
    int64_t fan_in,
    DType dtype,
    Generator & gen =DefaultGenerator()
)

Kaiming uniform initialization with an explicit dtype.

Parameters:

  • shape Output shape.
  • fan_in Input fan size.
  • dtype Result dtype.
  • gen Stateful generator to draw from.

Return: The initialized tensor.

Kaiming uniform initialization from a stateful generator.

function KaimingUniform

Tensor KaimingUniform(
    Shape shape,
    int64_t fan_in,
    Generator & gen =DefaultGenerator()
)

Kaiming uniform initialization.

Parameters:

  • shape Output shape.
  • fan_in Input fan size.
  • gen Stateful generator to draw from.

Return: The initialized tensor.

Kaiming uniform initialization with a kFloat32 default dtype.

function KaimingNormal

Tensor KaimingNormal(
    Shape shape,
    int64_t fan_in,
    DType dtype,
    Generator & gen =DefaultGenerator()
)

Kaiming normal initialization with an explicit dtype.

Parameters:

  • shape Output shape.
  • fan_in Input fan size.
  • dtype Result dtype.
  • gen Stateful generator to draw from.

Return: The initialized tensor.

Kaiming normal initialization from a stateful generator.

function KaimingNormal

Tensor KaimingNormal(
    Shape shape,
    int64_t fan_in,
    Generator & gen =DefaultGenerator()
)

Kaiming normal initialization.

Parameters:

  • shape Output shape.
  • fan_in Input fan size.
  • gen Stateful generator to draw from.

Return: The initialized tensor.

Kaiming normal initialization with a kFloat32 default dtype.

function Rand

inline Tensor Rand(
    Shape shape,
    Generator & gen =DefaultGenerator()
)

Parameters:

  • shape Output shape.
  • gen Generator to draw from.

Return: Sampled array.

Sample uniform values in [0, 1).

function Rand

inline Tensor Rand(
    Shape shape,
    float low,
    float high,
    Generator & gen =DefaultGenerator()
)

Parameters:

  • shape Output shape.
  • low Inclusive lower bound.
  • high Exclusive upper bound.
  • gen Generator to draw from.

Return: Sampled array.

Sample uniform values in [low, high).

function Randn

inline Tensor Randn(
    Shape shape,
    Generator & gen =DefaultGenerator()
)

Parameters:

  • shape Output shape.
  • gen Generator to draw from.

Return: Sampled array.

Sample standard normal values.

function Randn

inline Tensor Randn(
    Shape shape,
    float mean,
    float stddev,
    Generator & gen =DefaultGenerator()
)

Parameters:

  • shape Output shape.
  • mean Distribution mean.
  • stddev Distribution standard deviation.
  • gen Generator to draw from.

Return: Sampled array.

Sample normal values.