TensorTonic
Study Plans
Study Plan
Masked Mean Without Boolean Indexing
JAX
Easy

Compute the mean of the valid entries of x along one axis, where mask marks the valid entries, while keeping every intermediate shape independent of the mask values.

For each slice of x along axis, the result is

result=1∣V∣∑j∈Vxj\text{result} = \frac{1}{|V|} \sum_{j \in V} x_jresult=∣V∣1​j∈V∑​xj​

where j runs over the positions along axis, V is the set of positions where mask is True, and |V| is the number of such positions. A slice with no valid entries gives 0.0. Entries where mask is False are padding, and their values must have no effect on the result.

Your function is called under jax.jit with axis marked as static. That means x and mask arrive as traced arrays whose values are unknown while the function compiles, and axis arrives as an ordinary Python integer.

Return a float32 JAX array whose shape is the shape of x with axis removed.

Examples

Input: x = [[1.0,2.0,3.0,4.0],[10.0,20.0,30.0,40.0]], mask = [[true,true,false,false],[true,false,true,false]], axis = 1

Output: [1.5,20.0]

Explanation: Row 0 keeps 1 and 2, whose mean is 1.5. Row 1 keeps 10 and 30, whose mean is 20.

Input: x = [[1.0,2.0],[3.0,4.0],[5.0,6.0]], mask = [[true,false],[true,true],[false,true]], axis = 0

Output: [2.0,5.0]

Input: x = [[5.0,7.0,9.0],[2.0,4.0,6.0]], mask = [[false,false,false],[true,true,false]], axis = -1

Output: [0.0,3.0]

Hint 1

jnp.where(mask, x, 0.0) keeps the full shape and zeroes the padding.

Hint 2

jnp.sum(mask, axis=axis) counts the valid entries of each slice.

Hint 3

jnp.maximum(count, 1) avoids dividing by zero.

Requirements

  • Select entries with jnp.where instead of boolean indexing such as x[mask], whose output shape depends on the mask values.
  • Handle empty slices with array operations, since a Python if statement cannot test a traced value.

Constraints

  • x is a float32 JAX array with one to three dimensions and at most 30 entries.
  • mask is a bool JAX array with the same shape as x.
  • axis is an integer from -ndim to ndim - 1, where ndim is the number of dimensions of x.
  • Entries of x where mask is True are finite. Entries where mask is False may hold any float32 value, including infinity and NaN.
Try Similar Problems
Functional Array UpdateEasyPRNG Key SplittingEasyPad 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: array

Accepts: array

Accepts: integer

You must run your code first.
Masked Mean Without Boolean Indexing

Masked Mean Without Boolean Indexing

JAX
Easy

Compute the mean of the valid entries of x along one axis, where mask marks the valid entries, while keeping every intermediate shape independent of the mask values.

For each slice of x along axis, the result is

result=1∣V∣∑j∈Vxj\text{result} = \frac{1}{|V|} \sum_{j \in V} x_jresult=∣V∣1​j∈V∑​xj​

where j runs over the positions along axis, V is the set of positions where mask is True, and |V| is the number of such positions. A slice with no valid entries gives 0.0. Entries where mask is False are padding, and their values must have no effect on the result.

Your function is called under jax.jit with axis marked as static. That means x and mask arrive as traced arrays whose values are unknown while the function compiles, and axis arrives as an ordinary Python integer.

Return a float32 JAX array whose shape is the shape of x with axis removed.

Examples

Input: x = [[1.0,2.0,3.0,4.0],[10.0,20.0,30.0,40.0]], mask = [[true,true,false,false],[true,false,true,false]], axis = 1

Output: [1.5,20.0]

Explanation: Row 0 keeps 1 and 2, whose mean is 1.5. Row 1 keeps 10 and 30, whose mean is 20.

Input: x = [[1.0,2.0],[3.0,4.0],[5.0,6.0]], mask = [[true,false],[true,true],[false,true]], axis = 0

Output: [2.0,5.0]

Input: x = [[5.0,7.0,9.0],[2.0,4.0,6.0]], mask = [[false,false,false],[true,true,false]], axis = -1

Output: [0.0,3.0]

Hint 1

jnp.where(mask, x, 0.0) keeps the full shape and zeroes the padding.

Hint 2

jnp.sum(mask, axis=axis) counts the valid entries of each slice.

Hint 3

jnp.maximum(count, 1) avoids dividing by zero.

Requirements

  • Select entries with jnp.where instead of boolean indexing such as x[mask], whose output shape depends on the mask values.
  • Handle empty slices with array operations, since a Python if statement cannot test a traced value.

Constraints

  • x is a float32 JAX array with one to three dimensions and at most 30 entries.
  • mask is a bool JAX array with the same shape as x.
  • axis is an integer from -ndim to ndim - 1, where ndim is the number of dimensions of x.
  • Entries of x where mask is True are finite. Entries where mask is False may hold any float32 value, including infinity and NaN.
Try Similar Problems
Functional Array UpdateEasyPRNG Key SplittingEasyPad 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: array

Accepts: array

Accepts: integer

You must run your code first.