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

Extract conv1D functionality from Conv1D Layer #549

Merged
merged 2 commits into from
Nov 14, 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
11 changes: 5 additions & 6 deletions Sources/TensorFlow/Layers/Convolutional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,12 @@ public struct Conv1D<Scalar: TensorFlowFloatingPoint>: Layer {
/// - Note: Padding size equals zero when using `.valid`.
@differentiable
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
let conv = conv2D(
input.expandingShape(at: 1),
filter: filter.expandingShape(at: 0),
strides: (1, 1, stride, 1),
activation(conv1D(
input,
filter: filter,
stride: stride,
padding: padding,
dilations: (1, 1, dilation, 1))
return activation(conv.squeezingShape(at: 1) + bias)
dilation: dilation) + bias)
}
}

Expand Down
29 changes: 29 additions & 0 deletions Sources/TensorFlow/Operators/NN.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,35 @@ public extension Padding {
}
}

/// Returns a 1-D convolution with the specified input, filter, stride, and padding.
///
/// - Parameters:
/// - input: The input.
/// - filter: The convolution filter.
/// - stride: The stride of the sliding filter.
/// - padding: The padding for the operation.
/// - dilation: The dilation factor.
/// - Precondition: `input` must have rank `3`.
/// - Precondition: `filter` must have rank 3.
@differentiable(wrt: (input, filter))
public func conv1D<Scalar: TensorFlowFloatingPoint>(
_ input: Tensor<Scalar>,
filter: Tensor<Scalar>,
stride: Int = 1,
padding: Padding = .valid,
dilation: Int = 1
) -> Tensor<Scalar> {
precondition(input.shape.rank == 3, "The input must have rank 3.")
precondition(filter.shape.rank == 3, "The filter must have rank 3.")
return conv2D(
input.expandingShape(at: 1),
filter: filter.expandingShape(at: 0),
strides: (1, 1, stride, 1),
padding: padding,
dilations: (1, 1, dilation, 1)
).squeezingShape(at: 1)
}

/// Returns a 2-D convolution with the specified input, filter, strides, and padding.
///
/// - Parameters:
Expand Down