TensorTonic
Problems
PRNG Key Splitting
JAX
Easy

Build one dropout keep mask per layer from a single integer seed, following an exact key-splitting rule.

Dropout randomly zeroes activations during training. A keep mask holds True for every entry that survives, and each entry is kept with probability keep_prob. The masks are fully determined by the seed and by the way keys are split, so follow this rule exactly.

  1. Create the starting key with jax.random.key(seed). It is the running key.
  2. For each layer in order, split the running key into two keys with jax.random.split. The first of the two replaces the running key, and the second belongs to the current layer.
  3. Draw the layer's mask with jax.random.bernoulli, passing that layer key, keep_prob and the mask shape.

Return a bool JAX array of shape (num_layers, followed by the entries of shape), for example (3, 2, 4) for three layers of shape [2, 4], with the mask of layer i at index i of the first axis.

Examples

Input: seed = 0, num_layers = 2, shape = [2,4], keep_prob = 0.5

Output: [[[true,true,false,true],[true,true,true,false]],[[true,true,true,false],[true,false,false,true]]]

Explanation: The seed 0 key is split once for layer 0, and the first key from that split is split again for layer 1. The two layer keys are different, so the two masks differ.

Input: seed = 42, num_layers = 3, shape = [5], keep_prob = 0.8

Output: [[true,true,true,true,true],[true,true,true,true,false],[true,true,true,true,false]]

Input: seed = 2026, num_layers = 1, shape = [3,3], keep_prob = 0.3

Output: [[[true,true,false],[false,false,false],[false,true,true]]]

Hint 1

key, subkey = jax.random.split(key) gives a fresh subkey and a new running key.

Hint 2

jax.random.bernoulli(subkey, p, shape) returns a bool array of that shape.

Hint 3

jnp.stack(list_of_masks) adds a new first axis.

Requirements

  • Leave the shape list unchanged.

Constraints

  • seed is an integer between 0 and 2,147,483,647.
  • num_layers is an integer between 1 and 6.
  • shape is a list of one to three positive integers whose product is at most 8.
  • keep_prob is a number between 0 and 1 inclusive.
Try Similar Problems
Functional Array UpdateEasyMasked Mean Without Boolean IndexingEasyPad and Mask to Bucket LengthMediumValue and Grad of a LossEasyStatic Arguments in jitEasyModel Ensemble with in_axesEasySGD with Momentum over a PytreeEasyRotary Position EmbeddingEasyKV Cache Update at a PositionEasyDevice Mesh and Named ShardingEasyHessian-Vector ProductMediumPer-Example Gradient ClippingMedium

Accepts: integer

Accepts: integer

Accepts: array

Accepts: number

You must run your code first.
PRNG Key Splitting

PRNG Key Splitting

JAX
Easy

Build one dropout keep mask per layer from a single integer seed, following an exact key-splitting rule.

Dropout randomly zeroes activations during training. A keep mask holds True for every entry that survives, and each entry is kept with probability keep_prob. The masks are fully determined by the seed and by the way keys are split, so follow this rule exactly.

  1. Create the starting key with jax.random.key(seed). It is the running key.
  2. For each layer in order, split the running key into two keys with jax.random.split. The first of the two replaces the running key, and the second belongs to the current layer.
  3. Draw the layer's mask with jax.random.bernoulli, passing that layer key, keep_prob and the mask shape.

Return a bool JAX array of shape (num_layers, followed by the entries of shape), for example (3, 2, 4) for three layers of shape [2, 4], with the mask of layer i at index i of the first axis.

Examples

Input: seed = 0, num_layers = 2, shape = [2,4], keep_prob = 0.5

Output: [[[true,true,false,true],[true,true,true,false]],[[true,true,true,false],[true,false,false,true]]]

Explanation: The seed 0 key is split once for layer 0, and the first key from that split is split again for layer 1. The two layer keys are different, so the two masks differ.

Input: seed = 42, num_layers = 3, shape = [5], keep_prob = 0.8

Output: [[true,true,true,true,true],[true,true,true,true,false],[true,true,true,true,false]]

Input: seed = 2026, num_layers = 1, shape = [3,3], keep_prob = 0.3

Output: [[[true,true,false],[false,false,false],[false,true,true]]]

Hint 1

key, subkey = jax.random.split(key) gives a fresh subkey and a new running key.

Hint 2

jax.random.bernoulli(subkey, p, shape) returns a bool array of that shape.

Hint 3

jnp.stack(list_of_masks) adds a new first axis.

Requirements

  • Leave the shape list unchanged.

Constraints

  • seed is an integer between 0 and 2,147,483,647.
  • num_layers is an integer between 1 and 6.
  • shape is a list of one to three positive integers whose product is at most 8.
  • keep_prob is a number between 0 and 1 inclusive.
Try Similar Problems
Functional Array UpdateEasyMasked Mean Without Boolean IndexingEasyPad and Mask to Bucket LengthMediumValue and Grad of a LossEasyStatic Arguments in jitEasyModel Ensemble with in_axesEasySGD with Momentum over a PytreeEasyRotary Position EmbeddingEasyKV Cache Update at a PositionEasyDevice Mesh and Named ShardingEasyHessian-Vector ProductMediumPer-Example Gradient ClippingMedium

Accepts: integer

Accepts: integer

Accepts: array

Accepts: number

You must run your code first.
Problems
Loading...
1 / 1