Have a question? Connect with the community at the TensorFlow Forum

# Conv2D

``````@frozen
public struct Conv2D<Scalar> : Layer where Scalar : TensorFlowFloatingPoint``````

A 2-D convolution layer (e.g. spatial convolution over images).

This layer creates a convolution filter that is convolved with the layer input to produce a tensor of outputs.

• ``` filter ```

The 4-D convolution filter.

#### Declaration

``public var filter: Tensor<Scalar>``
• ``` bias ```

The bias vector.

#### Declaration

``public var bias: Tensor<Scalar>``
• ``` activation ```

The element-wise activation function.

#### Declaration

``````@noDerivative
public let activation: Activation``````
• ``` strides ```

The strides of the sliding window for spatial dimensions.

#### Declaration

``````@noDerivative
public let strides: (Int, Int)``````
• ``` padding ```

The padding algorithm for convolution.

#### Declaration

``````@noDerivative
public let padding: Padding``````
• ``` dilations ```

The dilation factor for spatial dimensions.

#### Declaration

``````@noDerivative
public let dilations: (Int, Int)``````
• ``` Activation ```

The element-wise activation function type.

#### Declaration

``public typealias Activation = @differentiable (Tensor<Scalar>) -> Tensor<Scalar>``
• ``` init(filter:bias:activation:strides:padding:dilations:) ```

Creates a `Conv2D` layer with the specified filter, bias, activation function, strides, dilations and padding.

#### Declaration

``````public init(
filter: Tensor<Scalar>,
bias: Tensor<Scalar>? = nil,
activation: @escaping Activation = identity,
strides: (Int, Int) = (1, 1),
padding: Padding = .valid,
dilations: (Int, Int) = (1, 1)
)``````

#### Parameters

 ``` filter ``` The 4-D convolution filter of shape [filter height, filter width, input channel count, output channel count]. ``` bias ``` The bias vector of shape [output channel count]. ``` activation ``` The element-wise activation function. ``` strides ``` The strides of the sliding window for spatial dimensions, i.e. (stride height, stride width). ``` padding ``` The padding algorithm for convolution. ``` dilations ``` The dilation factors for spatial dimensions, i.e. (dilation height, dilation width).
• ``` forward(_:) ```

Returns the output obtained from applying the layer to the given input.

The output spatial dimensions are computed as:

output height = (input height + 2 * padding height - (dilation height * (filter height - 1) + 1)) / stride height + 1

output width = (input width + 2 * padding width - (dilation width * (filter width - 1) + 1)) / stride width + 1

and padding sizes are determined by the padding scheme.

Note

Padding size equals zero when using `.valid`.

#### Declaration

``````@differentiable
public func forward(_ input: Tensor<Scalar>) -> Tensor<Scalar>``````

#### Parameters

 ``` input ``` The input to the layer of shape [batch size, input height, input width, input channel count].

#### Return Value

The output of shape [batch count, output height, output width, output channel count].

• ``` init(filterShape:strides:padding:dilations:activation:useBias:filterInitializer:biasInitializer:) ```

Creates a `Conv2D` layer with the specified filter shape, strides, padding, dilations and element-wise activation function.

#### Declaration

``````public init(
filterShape: (Int, Int, Int, Int),
strides: (Int, Int) = (1, 1),
padding: Padding = .valid,
dilations: (Int, Int) = (1, 1),
activation: @escaping Activation = identity,
useBias: Bool = true,
filterInitializer: ParameterInitializer<Scalar> = glorotUniform(),
biasInitializer: ParameterInitializer<Scalar> = zeros()
)``````

#### Parameters

 ``` filterShape ``` The shape of the 4-D convolution filter, representing (filter height, filter width, input channel count, output channel count). ``` strides ``` The strides of the sliding window for spatial dimensions, i.e. (stride height, stride width). ``` padding ``` The padding algorithm for convolution. ``` dilations ``` The dilation factors for spatial dimensions, i.e. (dilation height, dilation width). ``` activation ``` The element-wise activation function. ``` filterInitializer ``` Initializer to use for the filter parameters. ``` biasInitializer ``` Initializer to use for the bias parameters.
[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }]
[{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]