chainer.functions.expand_dims

chainer.functions.expand_dims(x, axis)[source]

Expands dimensions of an input variable without copy.

Parameters
  • x (Variable or N-dimensional array) – Input variable.

  • axis (int) – Position where new axis is to be inserted. The axis parameter is acceptable when \(-ndim - 1 \leq axis \leq ndim\). (ndim is the dimension of input variables). When \(axis < 0\), the result is the same with \(ndim + 1 - |axis|\).

Returns

Variable that holds an expanded input. The ndim of output is one greater than that of x.

Return type

Variable

Example

>>> x = np.array([1, 2, 3])
>>> x.shape
(3,)
>>> y = F.expand_dims(x, axis=0)
>>> y.shape
(1, 3)
>>> y.array
array([[1, 2, 3]])
>>> y = F.expand_dims(x, axis=1)
>>> y.shape
(3, 1)
>>> y.array
array([[1],
       [2],
       [3]])
>>> y = F.expand_dims(x, axis=-2)
>>> y.shape
(1, 3)
>>> y.array
array([[1, 2, 3]])