fc

paddle.fluid.layers.fc(input, size, num_flatten_dims=1, param_attr=None, bias_attr=None, act=None, name=None)[source]

Fully Connected Layer

This operator creates a fully connected layer in the network. It can take a Tensor(or LoDTensor) or a list of Tensor(or LoDTensor) as its inputs(see Args in detail). It creates a variable called weight for each input Tensor, which represents a fully connected weight matrix from each input unit to each output unit. The fully connected layer multiplies each input Tensor with its corresponding weight to produce an output Tensor with shape \([M, size]\) , where M is batch size. If a list of Tensor is given, the results of multiple output Tensors with shape \([M, size]\) will be summed up. If bias_attr is not None, a bias variable will be created and added to the output. Finally, if act is not None, it will be applied to the output as well.

When the input is a single Tensor(or LoDTensor):

\[Out = Act({XW + b})\]

When the input is a list of Tensor(or LoDTensor):

\[Out = Act({\sum_{i=0}^{N-1}X_iW_i + b})\]

In the above equation:

  • \(N\): Number of the input. N equals to len(input) if input is list of Variable.

  • \(X_i\): The i-th input tensor.

  • \(W_i\): The i-th weights matrix corresponding i-th input tensor.

  • \(b\): The bias parameter created by this layer (if needed).

  • \(Act\): The activation function.

  • \(Out\): The output Tensor.

Case 1:
Given a single Tensor data_1, and num_flatten_dims = 2:
    data_1.data = [[[0.1, 0.2],
                    [0.3, 0.4]]]
    data_1.shape = (1, 2, 2) # 1 is batch_size

    out = fluid.layers.fc(input=data_1, size=1, num_flatten_dims=2)

Then output is:
    out.data = [[0.83234344], [0.34936576]]
    out.shape = (1, 2, 1)

Case 2:
Given a list of Tensor:
    data_1.data = [[[0.1, 0.2],
                   [0.3, 0.4]]]
    data_1.shape = (1, 2, 2) # 1 is batch_size

    data_2 = [[[0.1, 0.2, 0.3]]]
    data_2.shape = (1, 1, 3)

    out = fluid.layers.fc(input=[data_1, data_2], size=2)

Then:
    out.data = [[0.18669507, 0.1893476]]
    out.shape = (1, 2)
Parameters
  • input (Variable|list of Variable) – A Tensor(or LoDTensor) with shape \([N_1, N_2,..., N_k]\) or a list of Tensor(or LoDTensor). The dimensions of the input Tensor is at least 2 and the data type should be float32 or float64.

  • size (int) – The number of output units in this layer, which also means the feature size of ouput Tensor(or LoDTensor).

  • num_flatten_dims (int) – The fc layer can accept an input Tensor with more than two dimensions. If this happens, the multidimensional tensor will first be flattened into a 2-D matrix. The parameter num_flatten_dims determines how the input Tensor is flattened: the first num_flatten_dims (inclusive, index starts from 1) dimensions will be flatten to form the first dimension of the final matrix (height of the matrix), and the rest \(rank(X) - num\_flatten\_dims\) dimensions are flattened to form the second dimension of the final matrix (width of the matrix). For example, assuming that X is a 5-dimensional Tensor with a shape [2, 3, 4, 5, 6], and num_flatten_dims = 3. Then, the flattened matrix will have a shape [2 x 3 x 4, 5 x 6] = [24, 30]. Default: 1.

  • param_attr (ParamAttr) – To specify the weight parameter property. Default: None, which means the default weight parameter property is used. See usage for details in ParamAttr .

  • bias_attr (ParamAttr) – To specify the bias parameter property. Default: None, which means the default bias parameter property is used. See usage for details in ParamAttr .

  • act (str) – Activation to be applied to the output of this layer, such as tanh, softmax, sigmoid, relu. For more information, please refer to Activation Function . Default: None.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name .

Returns

Tensor or LoDTensor calculated by fc layer. The data type is same with input.

Return type

Variable

Raises

ValueError – If dimensions of the input Tensor is less than 2.

Examples

import paddle.fluid as fluid
# when input is single tensor
data = fluid.data(name="data", shape=[-1, 32], dtype="float32")
fc = fluid.layers.fc(input=data, size=1000, act="tanh")

# when input are multiple tensors
data_1 = fluid.data(name="data_1", shape=[-1, 32], dtype="float32")
data_2 = fluid.data(name="data_2", shape=[-1, 36], dtype="float32")
fc = fluid.layers.fc(input=[data_1, data_2], size=1000, act="tanh")