View#

View and shape manipulation operations.

nabla.ops.view.transpose(arg, axis_1=-2, axis_2=-1)[source]#

Transpose array along two axes.

nabla.ops.view.permute(input_array, axes)[source]#

Permute (reorder) the dimensions of a tensor.

Parameters:
  • input_array (Array) – Input tensor

  • axes (tuple[int, ...]) – Tuple specifying the new order of dimensions

Returns:

Tensor with reordered dimensions

Return type:

Array

Example

>>> x = nb.ones((2, 3, 4))  # shape (2, 3, 4)
>>> y = permute(x, (2, 0, 1))  # shape (4, 2, 3)
>>> # Dimension 2 -> position 0, dimension 0 -> position 1, dimension 1 -> position 2
nabla.ops.view.move_axis_to_front(input_array, axis)[source]#

Move specified axis to the front (position 0), shifting others right.

Parameters:
  • input_array (Array) – Input tensor

  • axis (int) – Axis to move to front

Returns:

Tensor with specified axis moved to front

Return type:

Array

Example

>>> x = nb.ones((2, 3, 4))  # shape (2, 3, 4)
>>> y = move_axis_to_front(x, 2)  # shape (4, 2, 3)
>>> # axis 2 moved to front, others shifted: [2, 0, 1]
nabla.ops.view.move_axis_from_front(input_array, target_axis)[source]#

Move front axis (position 0) to specified target position.

Parameters:
  • input_array (Array) – Input tensor (assumes front axis is the one to move)

  • target_axis (int) – Target position for the front axis

Returns:

Tensor with front axis moved to target position

Return type:

Array

Example

>>> x = nb.ones((4, 2, 3))  # front axis has size 4
>>> y = move_axis_from_front(x, 2)  # shape (2, 3, 4)
>>> # front axis moved to position 2: [1, 2, 0]
nabla.ops.view.permute_batch_dims(input_array, axes)[source]#

Permute (reorder) the batch dimensions of an array.

This operation reorders the batch_dims of an Array according to the given axes, similar to how regular permute works on shape dimensions. The shape dimensions remain unchanged.

Parameters:
  • input_array (Array) – Input array with batch dimensions to permute

  • axes (tuple[int, ...]) – Tuple specifying the new order of batch dimensions. All indices should be negative and form a permutation.

Returns:

Array with batch dimensions reordered according to axes

Return type:

Array

Example

>>> import nabla as nb
>>> # Array with batch_dims=(2, 3, 4) and shape=(5, 6)
>>> x = nb.ones((5, 6))
>>> x.batch_dims = (2, 3, 4)  # Simulated for example
>>> y = permute_batch_dims(x, (-1, -3, -2))  # Reorder as (4, 2, 3)
>>> # Result has batch_dims=(4, 2, 3) and shape=(5, 6)
nabla.ops.view.move_axis_to_front_of_batch_dims(input_array, axis)[source]#

Move specified batch dimension to the front (position 0), shifting others right.

Parameters:
  • input_array (Array) – Input tensor with batch dimensions

  • axis (int) – Batch dimension to move to front (negative index)

Returns:

Tensor with specified batch dimension moved to front

Return type:

Array

Example

>>> x = nb.ones((2, 3, 4))  # shape (2, 3, 4)
>>> x.batch_dims = (1, 0)  # Simulated for example
>>> y = move_axis_to_fron_of_batch_dims(x, -1)  # Move last batch dim to front
>>> # Result has batch_dims=(0, 1) and shape=(2, 3, 4)
nabla.ops.view.move_axis_from_front_of_batch_dims(input_array, target_axis)[source]#

Move front batch dimension (position 0) to specified target position.

Parameters:
  • input_array (Array) – Input tensor with batch dimensions (assumes front batch dim is the one to move)

  • target_axis (int) – Target position for the front batch dimension (negative index)

Returns:

Tensor with front batch dimension moved to target position

Return type:

Array

Example

>>> x = nb.ones((4, 2, 3))  # shape (4, 2, 3)
>>> x.batch_dims = (0, 1)  # Simulated for example
>>> y = move_axis_from_front_of_batch_dims(x, -1)  # Move front batch dim to last position
>>> # Result has batch_dims=(1, 0) and shape=(4, 2, 3)
nabla.ops.view.reshape(arg, shape)[source]#

Reshape array to given shape.

nabla.ops.view.broadcast_to(arg, shape)[source]#

Broadcast array to target shape.

nabla.ops.view.broadcast_batch_dims(arg, batch_dims)[source]#

Broadcast array to target batch_dims.

nabla.ops.view.squeeze(arg, axes=None)[source]#

Squeeze array by removing dimensions of size 1.

nabla.ops.view.unsqueeze(arg, axes=None)[source]#

Unsqueeze array by adding dimensions of size 1.

nabla.ops.view.squeeze_batch_dims(arg, axes=None)[source]#

Squeeze array by removing batch dimensions of size 1.

Parameters:
  • arg (Array) – Input array

  • axes (list[int] | None) – List of batch dimension axes to squeeze. If None, returns array unchanged.

Returns:

Array with specified batch dimensions of size 1 removed

Return type:

Array

nabla.ops.view.unsqueeze_batch_dims(arg, axes=None)[source]#

Unsqueeze array by adding batch dimensions of size 1.

Parameters:
  • arg (Array) – Input array

  • axes (list[int] | None) – List of positions where to insert batch dimensions of size 1. If None, returns array unchanged.

Returns:

Array with batch dimensions of size 1 added at specified positions

Return type:

Array

nabla.ops.view.shallow_copy(arg)[source]#

Create a shallow copy of the array.

nabla.ops.view.array_slice(arg, slices, squeeze_axes=None)[source]#

Slice an array along specified dimensions.

Parameters:
  • arg (Array) – Input array to slice

  • slices (list[slice]) – List of slice objects defining the slicing for each dimension

  • squeeze_axes (list[int] | None) – List of axes that should be squeezed (for JAX compatibility)

Returns:

Sliced array

Return type:

Array

nabla.ops.view.pad(arg, slices, target_shape)[source]#

Place a smaller array into a larger zero-filled array at the location specified by slices.

This is the inverse operation of array slicing - given slices, a small array, and target shape, it creates a larger array where the small array is placed at the sliced location and everything else is zero.

Parameters:
  • arg (Array) – Input array (the smaller array to be placed)

  • slices (list[slice]) – List of slice objects defining where to place the array

  • target_shape (tuple[int, ...]) – The shape of the output array

Returns:

Larger array with input placed at sliced location, zeros elsewhere

Return type:

Array

nabla.ops.view.concatenate(args, axis=0)[source]#

Concatenate arrays along an existing axis.

Parameters:
  • args (list[Array]) – List of arrays to concatenate

  • axis (int) – Axis along which to concatenate arrays (default: 0)

Returns:

Concatenated array

Return type:

Array

nabla.ops.view.stack(arrays, axis=0)[source]#

Stack arrays along a new axis.

Parameters:
  • arrays (list[Array]) – List of arrays to stack

  • axis (int) – Axis along which to stack the arrays (default: 0)

Returns:

Stacked array

Return type:

Array