Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

[Layers] Update DepthwiseConv2D(filterShape:...) bias initialization #441

Merged
merged 5 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions Sources/TensorFlow/Layers/Convolutional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public struct Conv1D<Scalar: TensorFlowFloatingPoint>: Layer {
///
/// and padding size is determined by the padding scheme.
///
/// - Parameter input: The input to the layer [batch count, input width, input channel count].
/// - Returns: The output of shape [batch count, output width, output channel count].
/// - Parameter input: The input to the layer [batch size, input width, input channel count].
/// - Returns: The output of shape [batch size, output width, output channel count].
///
/// - Note: Padding size equals zero when using `.valid`.
@differentiable
Expand Down Expand Up @@ -186,7 +186,7 @@ public struct Conv2D<Scalar: TensorFlowFloatingPoint>: Layer {
/// and padding sizes are determined by the padding scheme.
///
/// - Parameter input: The input to the layer of shape
/// [batch count, input height, input width, input channel count].
/// [batch size, input height, input width, input channel count].
/// - Returns: The output of shape
/// [batch count, output height, output width, output channel count].
///
Expand Down Expand Up @@ -495,8 +495,10 @@ public struct DepthwiseConv2D<Scalar: TensorFlowFloatingPoint>: Layer {

/// Returns the output obtained from applying the layer to the given input.
///
/// - Parameter input: The input to the layer.
/// - Returns: The output.
/// - Parameter input: The input to the layer of shape,
/// [batch count, input height, input width, input channel count]
/// - Returns: The output of shape,
/// [batch count, output height, output width, input channel count * channel multiplier]
@differentiable
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
return activation(depthwiseConv2D(
Expand All @@ -512,7 +514,8 @@ public extension DepthwiseConv2D {
/// element-wise activation function.
///
/// - Parameters:
/// - filterShape: The shape of the 4-D convolution kernel.
/// - filterShape: The shape of the 4-D convolution kernel with form,
/// [filter width, filter height, input channel count, channel multiplier].
/// - strides: The strides of the sliding window for spatial/spatio-temporal dimensions.
/// - padding: The padding algorithm for convolution.
/// - activation: The element-wise activation function.
Expand All @@ -530,7 +533,7 @@ public extension DepthwiseConv2D {
filterShape.0, filterShape.1, filterShape.2, filterShape.3])
self.init(
filter: filterInitializer(filterTensorShape),
bias: biasInitializer([filterShape.3]),
bias: biasInitializer([filterShape.2 * filterShape.3]),
activation: activation,
strides: strides,
padding: padding)
Expand Down
8 changes: 8 additions & 0 deletions Tests/TensorFlowTests/LayerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ final class LayerTests: XCTestCase {
scalars: [9, 12, 23, 28, 25, 36, 55, 68, 41, 60, 87, 108,
57, 84, 119, 148])
XCTAssertEqual(output, expected)

let channelMultiplier = 4
let multiplierLayer = DepthwiseConv2D<Float>(
filterShape: (2, 2, input.shape[3], channelMultiplier),
filterInitializer: glorotUniform(),
biasInitializer: zeros())
let multiplierOutput = multiplierLayer.inferring(from: input)
XCTAssertEqual(multiplierOutput.shape[3], input.shape[3] * channelMultiplier)
}

func testSeparableConv1D() {
Expand Down