We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
Gather Elements
Select columns independently from each row of a matrix. Each index tells you which column to read from that same row; it does not select a row.
Your task
Implement gather(x, indices).
xis a floating-point tensor of shape(m, n), with n > 0.indiceshas shape(m, k)on the same device. Its values are valid column IDs in[0, n), delivered as integer-valued floats. Cast them to integers.- Return shape
(m, k)with x's dtype and device, leaving both inputs unchanged.
output[i, j] = x[i, indices[i, j]]
For x=[[10, 20, 30], [40, 50, 60]] and indices=[[2, 0], [1, 2]],
return [[30, 10], [50, 60]]. Each row uses its own indices.
Keep the index order and repetitions. Repeating a column ID repeats its value
in the output. The output width k can exceed n when indices repeat. With k=0,
return shape (m, 0) rather than dropping the row dimension.
Use torch.gather(x, dim=1, index=indices.long()) in PyTorch or
jnp.take_along_axis(x, indices.astype(jnp.int32), axis=1) in JAX.
Do not flatten x or use indices from one row for every row.
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Select values using coordinates
A slice selects a regularly spaced region of an array. A gather selects entries using supplied indices. Check whether those indices select whole rows or columns, or a different entry within each row.
For [[10, 11], [20, 21]], choosing column 1 from the first row and column 0 from
the second gives [11, 20]. Choosing columns [1, 0] from every row instead
gives [[11, 10], [21, 20]]. These operations use the same index values but have
different meanings and output shapes.
Scatter writes values to selected destinations
Scatter takes values and destination indices. The operation may replace existing values or combine them using a reduction such as addition.
Starting with [0, 0, 0], scatter-add of values [2, 3] at indices [1, 1]
gives [0, 5, 0]. Both values contribute to the repeated destination. Repeated
assignment is not equivalent to this sum; follow the operation's rule for
repeated indices.
Selecting with a mask differs from replacing with a mask
For values [10, 20, 30] and mask [True, False, True], selecting masked entries
gives [10, 30]. Replacing masked entries with zero gives [0, 20, 0].
Selection can change the shape; replacement preserves it. A selection whose size
depends on runtime values needs particular care in compiled code with fixed
output shapes.
Check whether an update changes the original array
JAX indexed updates return a new array value. In PyTorch, basic slicing can return a view that shares storage, while advanced indexing returns a copy. Modifying a selected copy does not modify the source. Direct indexed assignment is a separate operation, so distinguish changing a selection from assigning into the original.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
JAX reference solution
Sign in to track your attempts toward unlocking the reference solution.