# groupedConvolution2dLayer

2-D grouped convolutional layer

## Description

A 2-D grouped convolutional layer separates the input channels into groups and applies sliding convolutional filters. Use grouped convolutional layers for channel-wise separable (also known as depth-wise separable) convolution.

For each group, the layer convolves the input by moving the filters along the input vertically and horizontally and computing the dot product of the weights and the input, and then adding a bias term. The layer combines the convolutions for each group independently. If the number of groups is equal to the number of channels, then this layer performs channel-wise convolution.

## Creation

### Syntax

### Description

creates a 2-D grouped convolutional layer and sets the `layer`

= groupedConvolution2dLayer(`filterSize`

,`numFiltersPerGroup`

,`numGroups`

)`FilterSize`

, `NumFiltersPerGroup`

, and `NumGroups`

properties.

creates a layer for channel-wise convolution (also known as depth-wise convolution). In
this case, the software determines the `layer`

= groupedConvolution2dLayer(`filterSize`

,`numFiltersPerGroup`

,'channel-wise')`NumGroups`

property at training time. This syntax is equivalent to setting
`NumGroups`

to the number of input channels.

sets the optional `layer`

= groupedConvolution2dLayer(___,`Name,Value`

)`Stride`

, `DilationFactor`

, Parameters and Initialization,
Learning Rate and Regularization, and `Name`

properties using name-value pairs. To specify input
padding, use the `'Padding'`

name-value pair argument. For example,
`groupedConvolution2dLayer(5,128,2,'Padding','same')`

creates a 2-D
grouped convolutional layer with 2 groups of 128 filters of size `[5 5]`

and pads the input to so that the output has the same size. You can specify multiple
name-value pairs. Enclose each property name in single quotes.

### Input Arguments

**Name-Value Arguments**

Use comma-separated name-value pair arguments to specify the size of the padding to
add along the edges of the layer input or to set the `Stride`

, `DilationFactor`

, Parameters and Initialization,
Learning Rate and Regularization, and `Name`

properties. Enclose names in single quotes.

**Example: **`groupedConvolution2dLayer(5,128,2,'Padding','same')`

creates
a 2-D grouped convolutional layer with 2 groups of 128 filters of size ```
[5
5]
```

and pads the input to so that the output has the same size.

`Padding`

— Input edge padding

`[0 0 0 0]`

(default) | vector of nonnegative integers | `'same'`

Input edge padding, specified as the comma-separated pair consisting of
`'Padding'`

and one of these values:

`'same'`

— Add padding of size calculated by the software at training or prediction time so that the output has the same size as the input when the stride equals 1. If the stride is larger than 1, then the output size is`ceil(inputSize/stride)`

, where`inputSize`

is the height or width of the input and`stride`

is the stride in the corresponding dimension. The software adds the same amount of padding to the top and bottom, and to the left and right, if possible. If the padding that must be added vertically has an odd value, then the software adds extra padding to the bottom. If the padding that must be added horizontally has an odd value, then the software adds extra padding to the right.Nonnegative integer

`p`

— Add padding of size`p`

to all the edges of the input.Vector

`[a b]`

of nonnegative integers — Add padding of size`a`

to the top and bottom of the input and padding of size`b`

to the left and right.Vector

`[t b l r]`

of nonnegative integers — Add padding of size`t`

to the top,`b`

to the bottom,`l`

to the left, and`r`

to the right of the input.

**Example: **
`'Padding',1`

adds one row of padding to the top and bottom, and one
column of padding to the left and right of the input.

**Example: **
`'Padding','same'`

adds padding so that the output has the same size as
the input (if the stride equals 1).

## Properties

### Grouped Convolution

`FilterSize`

— Height and width of filters

vector of two positive integers

Height and width of the filters, specified as a vector `[h w]`

of two positive integers, where `h`

is the height and `w`

is the width. `FilterSize`

defines the size of the local regions to which the neurons connect in the input.

When creating the layer, you can specify `FilterSize`

as a scalar to use the same value for the height and width.

**Example: ** `[5 5]`

specifies filters with a height of 5 and a width of 5.

`NumFiltersPerGroup`

— Number of filters per group

positive integer

Number of filters per group, specified as a positive integer. This property
determines the number of channels in the output of the layer. The number of output
channels is `FiltersPerGroup * NumGroups`

.

**Example: **
`10`

`NumGroups`

— Number of groups

positive integer | `'channel-wise'`

Number of groups, specified as a positive integer or
`'channel-wise'`

.

If `NumGroups`

is `'channel-wise'`

, then the
software creates a layer for channel-wise convolution (also known as depth-wise
convolution). In this case, the layer determines the `NumGroups`

property at training time. This value is equivalent to setting
`NumGroups`

to the number of input channels.

The number of groups must evenly divide the number of channels of the layer input.

**Example: **
`2`

`Stride`

— Step size for traversing input

`[1 1]`

(default) | vector of two positive integers

Step size for traversing the input vertically and horizontally, specified as a vector
`[a b]`

of two positive integers, where `a`

is the
vertical step size and `b`

is the horizontal step size. When creating
the layer, you can specify `Stride`

as a scalar to use the same value
for both step sizes.

**Example: **
`[2 3]`

specifies a vertical step size of 2 and a horizontal step size
of 3.

`DilationFactor`

— Factor for dilated convolution

`[1 1]`

(default) | vector of two positive integers

Factor for dilated convolution (also known as atrous convolution), specified as a vector `[h w]`

of two positive integers, where `h`

is the vertical dilation and `w`

is the horizontal dilation. When creating the layer, you can specify `DilationFactor`

as a scalar to use the same value for both horizontal and vertical dilations.

Use dilated convolutions to increase the receptive field (the area of the input which the layer can see) of the layer without increasing the number of parameters or computation.

The layer expands the filters by inserting zeros between each filter element. The dilation
factor determines the step size for sampling the input or equivalently the upsampling factor
of the filter. It corresponds to an effective filter size of (*Filter Size*
– 1) .* *Dilation Factor* + 1. For example, a 3-by-3 filter with the
dilation factor `[2 2]`

is equivalent to a 5-by-5 filter with zeros between
the elements.

**Example: **`[2 3]`

`PaddingSize`

— Size of padding

`[0 0 0 0]`

(default) | vector of four nonnegative integers

Size of padding to apply to input borders, specified as a vector
`[t b l r]`

of four nonnegative
integers, where `t`

is the padding applied to
the top, `b`

is the padding applied to the
bottom, `l`

is the padding applied to the left,
and `r`

is the padding applied to the right.

When you create a layer, use the `'Padding'`

name-value pair argument to specify the padding size.

**Example: **
`[1 1 2 2]`

adds one row of padding to the top
and bottom, and two columns of padding to the left and right of
the input.

`PaddingMode`

— Method to determine padding size

`'manual'`

(default) | `'same'`

Method to determine padding size, specified as `'manual'`

or
`'same'`

.

The software automatically sets the value of `PaddingMode`

based on the `'Padding'`

value you specify
when creating a layer.

If you set the

`'Padding'`

option to a scalar or a vector of nonnegative integers, then the software automatically sets`PaddingMode`

to`'manual'`

.If you set the

`'Padding'`

option to`'same'`

, then the software automatically sets`PaddingMode`

to`'same'`

and calculates the size of the padding at training time so that the output has the same size as the input when the stride equals 1. If the stride is larger than 1, then the output size is`ceil(inputSize/stride)`

, where`inputSize`

is the height or width of the input and`stride`

is the stride in the corresponding dimension. The software adds the same amount of padding to the top and bottom, and to the left and right, if possible. If the padding that must be added vertically has an odd value, then the software adds extra padding to the bottom. If the padding that must be added horizontally has an odd value, then the software adds extra padding to the right.

`PaddingValue`

— Value to pad data

0 (default) | scalar | `'symmetric-include-edge'`

| `'symmetric-exclude-edge'`

| `'replicate'`

Value to pad data, specified as one of the following:

`PaddingValue` | Description | Example |
---|---|---|

Scalar | Pad with the specified scalar value. |
$$\left[\begin{array}{ccc}3& 1& 4\\ 1& 5& 9\\ 2& 6& 5\end{array}\right]\to \left[\begin{array}{ccccccc}0& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 0& 0& 0& 0\\ 0& 0& 3& 1& 4& 0& 0\\ 0& 0& 1& 5& 9& 0& 0\\ 0& 0& 2& 6& 5& 0& 0\\ 0& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 0& 0& 0& 0\end{array}\right]$$ |

`'symmetric-include-edge'` | Pad using mirrored values of the input, including the edge values. |
$$\left[\begin{array}{ccc}3& 1& 4\\ 1& 5& 9\\ 2& 6& 5\end{array}\right]\to \left[\begin{array}{ccccccc}5& 1& 1& 5& 9& 9& 5\\ 1& 3& 3& 1& 4& 4& 1\\ 1& 3& 3& 1& 4& 4& 1\\ 5& 1& 1& 5& 9& 9& 5\\ 6& 2& 2& 6& 5& 5& 6\\ 6& 2& 2& 6& 5& 5& 6\\ 5& 1& 1& 5& 9& 9& 5\end{array}\right]$$ |

`'symmetric-exclude-edge'` | Pad using mirrored values of the input, excluding the edge values. |
$$\left[\begin{array}{ccc}3& 1& 4\\ 1& 5& 9\\ 2& 6& 5\end{array}\right]\to \left[\begin{array}{ccccccc}5& 6& 2& 6& 5& 6& 2\\ 9& 5& 1& 5& 9& 5& 1\\ 4& 1& 3& 1& 4& 1& 3\\ 9& 5& 1& 5& 9& 5& 1\\ 5& 6& 2& 6& 5& 6& 2\\ 9& 5& 1& 5& 9& 5& 1\\ 4& 1& 3& 1& 4& 1& 3\end{array}\right]$$ |

`'replicate'` | Pad using repeated border elements of the input |
$$\left[\begin{array}{ccc}3& 1& 4\\ 1& 5& 9\\ 2& 6& 5\end{array}\right]\to \left[\begin{array}{ccccccc}3& 3& 3& 1& 4& 4& 4\\ 3& 3& 3& 1& 4& 4& 4\\ 3& 3& 3& 1& 4& 4& 4\\ 1& 1& 1& 5& 9& 9& 9\\ 2& 2& 2& 6& 5& 5& 5\\ 2& 2& 2& 6& 5& 5& 5\\ 2& 2& 2& 6& 5& 5& 5\end{array}\right]$$ |

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

| `char`

| `string`

`NumChannelsPerGroup`

— Number of channels per group

`'auto'`

(default) | positive integer

Number of channels per group, specified as `'auto'`

or a positive
integer. The number of channels per group is equal to the number of input channels
divided by the number of groups.

The software automatically sets this property at training time.

**Example: **
`256`

### Parameters and Initialization

`WeightsInitializer`

— Function to initialize weights

`'glorot'`

(default) | `'he'`

| `'narrow-normal'`

| `'zeros'`

| `'ones'`

| function handle

Function to initialize the weights, specified as one of the following:

`'glorot'`

– Initialize the weights with the Glorot initializer [1] (also known as Xavier initializer). The Glorot initializer independently samples from a uniform distribution with zero mean and variance`2/(numIn + numOut)`

, where`numIn = FilterSize(1)*FilterSize(2)*NumChannelsPerGroup`

and`numOut = FilterSize(1)*FilterSize(2)*NumFiltersPerGroup`

.`'he'`

– Initialize the weights with the He initializer [2]. The He initializer samples from a normal distribution with zero mean and variance`2/numIn`

, where`numIn = FilterSize(1)*FilterSize(2)*NumChannelsPerGroup`

.`'narrow-normal'`

– Initialize the weights by independently sampling from a normal distribution with zero mean and standard deviation 0.01.`'zeros'`

– Initialize the weights with zeros.`'ones'`

– Initialize the weights with ones.Function handle – Initialize the weights with a custom function. If you specify a function handle, then the function must be of the form

`weights = func(sz)`

, where`sz`

is the size of the weights. For an example, see Specify Custom Weight Initialization Function.

The layer only initializes the weights when the `Weights`

property is empty.

**Data Types: **`char`

| `string`

| `function_handle`

`BiasInitializer`

— Function to initialize bias

`'zeros'`

(default) | `'narrow-normal'`

| `'ones'`

| function handle

Function to initialize the bias, specified as one of the following:

`'zeros'`

— Initialize the bias with zeros.`'ones'`

— Initialize the bias with ones.`'narrow-normal'`

— Initialize the bias by independently sampling from a normal distribution with a mean of zero and a standard deviation of 0.01.Function handle — Initialize the bias with a custom function. If you specify a function handle, then the function must be of the form

`bias = func(sz)`

, where`sz`

is the size of the bias.

The layer only initializes the bias when the `Bias`

property is
empty.

**Data Types: **`char`

| `string`

| `function_handle`

`Weights`

— Layer weights

`[]`

(default) | numeric array

Layer weights for the layer, specified as a numeric array.

The layer weights are learnable parameters. You can specify the
initial value for the weights directly using the `Weights`

property of the layer. When you train a network, if the `Weights`

property of the layer is nonempty, then `trainNetwork`

uses the `Weights`

property as the
initial value. If the `Weights`

property is empty, then
`trainNetwork`

uses the initializer specified by the `WeightsInitializer`

property of the layer.

At training time, `Weights`

is a
`FilterSize(1)`

-by-`FilterSize(2)`

-by-`NumChannelsPerGroup`

-by-`NumFiltersPerGroup`

-by-`NumGroups`

array, where `NumInputChannels`

is the number of channels of the
layer input.

**Data Types: **`single`

| `double`

`Bias`

— Layer biases

`[]`

(default) | numeric array

Layer biases for the layer, specified as a numeric array.

The layer biases are learnable parameters. When you train a
network, if `Bias`

is nonempty, then `trainNetwork`

uses the `Bias`

property as the
initial value. If `Bias`

is empty, then
`trainNetwork`

uses the initializer specified by `BiasInitializer`

.

At training time, `Bias`

is a
1-by-1-by-`NumFiltersPerGroup`

-by-`NumGroups`

array.

**Data Types: **`single`

| `double`

### Learning Rate and Regularization

`WeightLearnRateFactor`

— Learning rate factor for weights

`1`

(default) | nonnegative scalar

Learning rate factor for the weights, specified as a nonnegative scalar.

The software multiplies this factor by the global learning rate to determine the
learning rate for the weights in this layer. For example, if
`WeightLearnRateFactor`

is `2`

, then the
learning rate for the weights in this layer is twice the current global learning rate.
The software determines the global learning rate based on the settings you specify using
the `trainingOptions`

function.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

`BiasLearnRateFactor`

— Learning rate factor for biases

`1`

(default) | nonnegative scalar

Learning rate factor for the biases, specified as a nonnegative scalar.

The software multiplies this factor by the global learning rate
to determine the learning rate for the biases in this layer. For example, if
`BiasLearnRateFactor`

is `2`

, then the learning rate for
the biases in the layer is twice the current global learning rate. The software determines the
global learning rate based on the settings you specify using the `trainingOptions`

function.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

`WeightL2Factor`

— *L*_{2} regularization factor for weights

1 (default) | nonnegative scalar

_{2}

*L _{2}* regularization factor for the weights,
specified as a nonnegative scalar.

The software multiplies this factor by the global
*L _{2}* regularization factor to determine the

*L*regularization for the weights in this layer. For example, if

_{2}`WeightL2Factor`

is `2`

,
then the *L*regularization for the weights in this layer is twice the global

_{2}*L*regularization factor. You can specify the global

_{2}*L*regularization factor using the

_{2}`trainingOptions`

function.**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

`BiasL2Factor`

— *L*_{2} regularization factor for biases

`0`

(default) | nonnegative scalar

_{2}

*L _{2}* regularization factor for the biases,
specified as a nonnegative scalar.

The software multiplies this factor by the global
*L _{2}* regularization factor to
determine the

*L*regularization for the biases in this layer. For example, if

_{2}`BiasL2Factor`

is `2`

, then
the *L*regularization for the biases in this layer is twice the global

_{2}*L*regularization factor. The software determines the global

_{2}*L*regularization factor based on the settings you specify using the

_{2}`trainingOptions`

function.**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

### Layer

`Name`

— Layer name

`''`

(default) | character vector | string scalar

Layer name, specified as a character vector or a string scalar.
For `Layer`

array input, the `trainNetwork`

, `assembleNetwork`

, `layerGraph`

, and
`dlnetwork`

functions automatically assign
names to layers with the name `''`

.

**Data Types: **`char`

| `string`

`NumInputs`

— Number of inputs

`1`

(default)

This property is read-only.

Number of inputs of the layer. This layer accepts a single input only.

**Data Types: **`double`

`InputNames`

— Input names

`{'in'}`

(default)

This property is read-only.

Input names of the layer. This layer accepts a single input only.

**Data Types: **`cell`

`NumOutputs`

— Number of outputs

`1`

(default)

This property is read-only.

Number of outputs of the layer. This layer has a single output only.

**Data Types: **`double`

`OutputNames`

— Output names

`{'out'}`

(default)

This property is read-only.

Output names of the layer. This layer has a single output only.

**Data Types: **`cell`

## Examples

### Create Grouped Convolution Layer

Create a grouped convolutional layer with 3 groups of 10 filters, each with a height and width of 11, and the name `'gconv1'`

.

layer = groupedConvolution2dLayer(11,10,3,'Name','gconv1')

layer = GroupedConvolution2DLayer with properties: Name: 'gconv1' Hyperparameters FilterSize: [11 11] NumGroups: 3 NumChannelsPerGroup: 'auto' NumFiltersPerGroup: 10 Stride: [1 1] DilationFactor: [1 1] PaddingMode: 'manual' PaddingSize: [0 0 0 0] PaddingValue: 0 Learnable Parameters Weights: [] Bias: [] Show all properties

### Create Channel-Wise Convolution Layer

Create a channel-wise convolutional (also known as depth-wise convolutional) layer with groups of 10 filters, each with a height and width of 11, and the name `'cwconv1'`

.

layer = groupedConvolution2dLayer(11,10,'channel-wise','Name','cwconv1')

layer = GroupedConvolution2DLayer with properties: Name: 'cwconv1' Hyperparameters FilterSize: [11 11] NumGroups: 'channel-wise' NumChannelsPerGroup: 'auto' NumFiltersPerGroup: 10 Stride: [1 1] DilationFactor: [1 1] PaddingMode: 'manual' PaddingSize: [0 0 0 0] PaddingValue: 0 Learnable Parameters Weights: [] Bias: [] Show all properties

### Create Layers for Channel-Wise Separable Convolution

A typical convolutional neural network contains blocks of convolution, batch normalization, and ReLU layers. For example,

filterSize = 3; numFilters = 16; convLayers = [ convolution2dLayer(filterSize,numFilters,'Stride',2,'Padding','same') batchNormalizationLayer reluLayer];

For channel-wise separable convolution (also known as depth-wise separable convolution), replace the convolution block with channel-wise convolution and point-wise convolution blocks.

Specify the filter size and the stride in the channel-wise convolution and the number of filters in the point-wise convolution. For the channel-wise convolution, specify one filter per group. For point-wise convolution, specify filters of size 1 in `convolution2dLayer`

.

cwsConvLayers = [ groupedConvolution2dLayer(filterSize,1,'channel-wise','Stride',2,'Padding','same') batchNormalizationLayer reluLayer convolution2dLayer(1,numFilters,'Padding','same') batchNormalizationLayer reluLayer];

Create a network containing layers for channel-wise separable convolution.

layers = [ imageInputLayer([227 227 3]) convolution2dLayer(3,32,'Padding','same') batchNormalizationLayer reluLayer groupedConvolution2dLayer(3,1,'channel-wise','Stride',2,'Padding','same') batchNormalizationLayer reluLayer convolution2dLayer(1,16,'Padding','same') batchNormalizationLayer reluLayer maxPooling2dLayer(2,'Stride',2) fullyConnectedLayer(5) softmaxLayer classificationLayer];

## References

[1] Glorot,
Xavier, and Yoshua Bengio. "Understanding the Difficulty of Training Deep Feedforward Neural
Networks." In *Proceedings of the Thirteenth International Conference on Artificial
Intelligence and Statistics*, 249–356. Sardinia, Italy: AISTATS,
2010.

[2] He, Kaiming,
Xiangyu Zhang, Shaoqing Ren, and Jian Sun. "Delving Deep into Rectifiers: Surpassing Human-Level
Performance on ImageNet Classification." In *Proceedings of the 2015 IEEE
International Conference on Computer Vision*, 1026–1034. Washington, DC: IEEE
Computer Vision Society, 2015.

## Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Code generation for the ARM Compute Library is not supported for a 2-D grouped convolution layer that has the

`NumGroups`

property set to an integer value greater than two.For code generation, the

`PaddingValue`

parameter must be equal to`0`

, which is the default value.

### GPU Code Generation

Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Usage notes and limitations:

Code generation for the ARM Mali GPU is not supported for a 2-D grouped convolution layer that has the

`NumGroups`

property set as`'channel-wise'`

or a value greater than two.For code generation, the

`PaddingValue`

parameter must be equal to`0`

, which is the default value.

## Version History

**Introduced in R2019a**

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

# Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)