chainer.functions.linear(x, W, b=None, n_batch_axes=1)[source]

Linear function, or affine transformation.

It accepts two or three arguments: an input minibatch x, a weight matrix W, and optionally a bias vector b. It computes

\[y_i = W x_i + b.\]
  • x (Variable or N-dimensional array) – Input variable, which is a \((s_1, s_2, ..., s_n)\)-shaped float array. Its first n_batch_axes dimensions are handled as minibatch dimensions. The other dimensions are handled as concatenated one dimension whose size must be \((s_{\rm n\_batch\_axes} * ... * s_n = N)\).

  • W (Variable or N-dimensional array) – Weight variable of shape \((M, N)\), where \((N = s_{\rm n\_batch\_axes} * ... * s_n)\).

  • b (Variable or N-dimensional array) – Bias variable (optional) of shape \((M,)\).

  • n_batch_axes (int) – The number of batch axes. The default is 1. The input variable is reshaped into (\({\rm n\_batch\_axes} + 1\))-dimensional tensor. This should be greater than 0.


Output variable. A float array with shape of \((s_1, ..., s_{\rm n\_batch\_axes}, M)\).

Return type


See also

Linear to manage the model parameters W and b.


>>> x = np.random.uniform(0, 1, (3, 4)).astype(np.float32)
>>> W = np.random.uniform(0, 1, (5, 4)).astype(np.float32)
>>> b = np.random.uniform(0, 1, (5,)).astype(np.float32)
>>> y = F.linear(x, W, b)
>>> y.shape
(3, 5)