jacinle.nd.shape#

Functions

softmax(x[, axis])

Compute the softmax of the input array at the given axis.

unsqueeze(x, *axes)

Unsqueeze the numpy array at the given axes.

unsqueeze_as(x, y, *x_axes_in_y)

Unsqueeze the numpy array x as the shape of y.

Functions

softmax(x, axis=-1)[source]#

Compute the softmax of the input array at the given axis.

\[\text{softmax}(x)_i = \frac{e^{x_i}}{\sum_j e^{x_j}}\]
Parameters:
  • x (ndarray) – the input array.

  • axis (int) – the axis to apply softmax.

Returns:

the softmax array.

Return type:

ndarray

unsqueeze(x, *axes)[source]#

Unsqueeze the numpy array at the given axes. Similar to the PyTorch’s torch.unsqueeze().

Parameters:
  • x (ndarray) – the input array.

  • axes (int) – the axes to unsqueeze.

Returns:

the unsqueezed array.

Return type:

ndarray

unsqueeze_as(x, y, *x_axes_in_y)[source]#

Unsqueeze the numpy array x as the shape of y. The corresponding axes in x are specified by x_axes_in_y.

Example

>>> x = np.zeros((2, 3, 4))
>>> y = np.zeros((2, 3, 4, 5))
>>> unsqueeze_as(x, y, 0, 1, 2).shape
(2, 3, 4, 1)
Parameters:
  • x (ndarray) – the input array.

  • y (ndarray) – the target array.

  • x_axes_in_y (int) – the axes in x that correspond to the axes in y.

Returns:

the unsqueezed array.

Return type:

ndarray