# FC¶

class paddle.fluid.dygraph.FC(name_scope, size, num_flatten_dims=1, param_attr=None, bias_attr=None, act=None, is_test=False, dtype='float32')[source]

This interface is used to construct a callable object of the FC class. For more details, refer to code examples. It creates a fully connected layer in the network. It can take one or multiple Tensor as its inputs. It creates a Variable called weights 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 [N, size], where N is batch size. If multiple input tensors are given, the results of multiple output tensors with shape [N, 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 single Tensor :

$Out = Act({XW + b})$

When the input are multiple Tensor :

$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 Tensor .

• $$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 .

See below for an example.

Given:
data_1.data = [[[0.1, 0.2]]]
data_1.shape = (1, 1, 2) # 1 is batch_size

data_2.data = [[[0.1, 0.2, 0.3]]]
data_2.shape = (1, 1, 3) # 1 is batch_size

fc = FC("fc", 2, num_flatten_dims=2)
out = fc(input=[data_1, data_2])

Then:
out.data = [[[0.182996 -0.474117]]]
out.shape = (1, 1, 2)

Parameters
• name_scope (str) – The name of this class.

• size (int) – The number of output units in this layer.

• num_flatten_dims (int, optional) – The fc layer can accept an input tensor with more than two dimensions. If this happens, the multi-dimension tensor will first be flattened into a 2-dimensional 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, suppose 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 or list of ParamAttr, optional) – The parameter attribute for learnable weights(Parameter) of this layer. Default: None.

• bias_attr (ParamAttr or list of ParamAttr, optional) – The attribute for the bias of this layer. If it is set to False, no bias will be added to the output units. If it is set to None, the bias is initialized zero. Default: None.

• act (str, optional) – Activation to be applied to the output of this layer. Default: None.

• is_test (bool, optional) – A flag indicating whether execution is in test phase. Default: False.

• dtype (str, optional) – Dtype used for weight, it can be “float32” or “float64”. Default: “float32”.

Attribute:

weight (list of Parameter): the learnable weights of this layer.

bias (Parameter or None): the learnable bias of this layer.

Returns

None

Examples

from paddle.fluid.dygraph.base import to_variable
import numpy as np

data = np.random.uniform(-1, 1, [30, 10, 32]).astype('float32')
with fluid.dygraph.guard():
fc = FC("fc", 64, num_flatten_dims=2)
data = to_variable(data)
conv = fc(data)

forward(input)

Defines the computation performed at every call. Should be overridden by all subclasses.

Parameters
• *inputs (tuple) – unpacked tuple arguments

• **kwargs (dict) – unpacked dict arguments