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

Commit 2c7a190

Browse files
neild0rxwei
authored andcommitted
Rename and refactor convolution and pooling operators. (#168)
- Change method `Tensor.convolved2D(withFilter:strides:padding:)` to a top-level function named **`conv2D(_:filter:strides:padding:)`**, taking the original `self` as a first argument. - Change method `Tensor.convolved3D(withFilter:strides:padding:)` to a top-level function named **`conv3D(_:filter:strides:padding:)`**, taking the original `self` as a first argument. - Change method `Tensor.maxPooled2D(kernelSize:strides:padding:)` to a top-level function named **`maxPool2D(_:filterSize:strides:padding:)`**, taking the original `self` as a first argument. - Change method `Tensor.maxPooled3D(kernelSize:strides:padding:)` to a top-level function named **`maxPool3D(_:filterSize:strides:padding:)`**, taking the original `self` as a first argument. - Change method `Tensor.avgPooled2D(kernelSize:strides:padding:)` to a top-level function named **`avgPool2D(_:filterSize:strides:padding:)`**, taking the original `self` as a first argument. - Change method `Tensor.avgPooled3D(kernelSize:strides:padding:)` to a top-level function named **`avgPool3D(_:filterSize:strides:padding:)`**, taking the original `self` as a first argument.
1 parent 190bf30 commit 2c7a190

File tree

3 files changed

+419
-434
lines changed

3 files changed

+419
-434
lines changed

Sources/TensorFlow/Layers/Convolutional.swift

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public struct Conv1D<Scalar: TensorFlowFloatingPoint>: Layer {
6060
/// - Returns: The output `[batchCount, newWidth, outputChannels]`.
6161
@differentiable
6262
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
63-
let conv2D = input.expandingShape(at: 1).convolved2D(
64-
withFilter: filter.expandingShape(at: 0), strides: (1, 1, stride, 1), padding: padding)
65-
return activation(conv2D.squeezingShape(at: 1) + bias)
63+
let conv = conv2D(input.expandingShape(at: 1), filter: filter.expandingShape(at: 0),
64+
strides: (1, 1, stride, 1), padding: padding)
65+
return activation(conv.squeezingShape(at: 1) + bias)
6666
}
6767
}
6868

@@ -178,9 +178,8 @@ public struct Conv2D<Scalar: TensorFlowFloatingPoint>: Layer {
178178
/// - Returns: The output.
179179
@differentiable
180180
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
181-
return activation(input.convolved2D(withFilter: filter,
182-
strides: (1, strides.0, strides.1, 1),
183-
padding: padding) + bias)
181+
return activation(conv2D(input, filter: filter, strides: (1, strides.0, strides.1, 1),
182+
padding: padding) + bias)
184183
}
185184
}
186185

@@ -294,9 +293,9 @@ public struct Conv3D<Scalar: TensorFlowFloatingPoint>: Layer {
294293
/// - Returns: The output.
295294
@differentiable
296295
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
297-
return activation(input.convolved3D(withFilter: filter,
298-
strides: (1, strides.0, strides.1, strides.2, 1),
299-
padding: padding) + bias)
296+
return activation(conv3D(input, filter: filter,
297+
strides: (1, strides.0, strides.1, strides.2, 1),
298+
padding: padding) + bias)
300299
}
301300
}
302301

@@ -419,9 +418,9 @@ public struct TransposedConv2D: Layer {
419418
strides.1 + (filter.shape[1] * paddingIndex)
420419
let c = filter.shape[2]
421420
let newShape = Tensor<Int32>([Int32(batchSize), Int32(w), Int32(h), Int32(c)])
422-
return activation(input.conv2DBackpropInput(shape: newShape, filter: filter,
423-
strides: (1, strides.0, strides.1, 1),
424-
padding: padding) + bias)
421+
return activation(conv2DBackpropInput(input, shape: newShape, filter: filter,
422+
strides: (1, strides.0, strides.1, 1),
423+
padding: padding) + bias)
425424
}
426425
}
427426

Sources/TensorFlow/Layers/Pooling.swift

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ public struct MaxPool1D<Scalar: TensorFlowFloatingPoint>: Layer {
4444
/// - Returns: The output.
4545
@differentiable
4646
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
47-
return input.expandingShape(at: 1).maxPooled2D(
48-
kernelSize: (1, 1, poolSize, 1), strides: (1, 1, stride, 1), padding: padding
49-
).squeezingShape(at: 1)
47+
return maxPool2D(input.expandingShape(at: 1), filterSize: (1, 1, poolSize, 1),
48+
strides: (1, 1, stride, 1), padding: padding).squeezingShape(at: 1)
5049
}
5150
}
5251

@@ -78,8 +77,7 @@ public struct MaxPool2D<Scalar: TensorFlowFloatingPoint>: Layer {
7877
/// - Returns: The output.
7978
@differentiable
8079
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
81-
return input.maxPooled2D(
82-
kernelSize: poolSize, strides: strides, padding: padding)
80+
return maxPool2D(input, filterSize: poolSize, strides: strides, padding: padding)
8381
}
8482
}
8583

@@ -125,7 +123,7 @@ public struct MaxPool3D<Scalar: TensorFlowFloatingPoint>: Layer {
125123
/// - Returns: The output.
126124
@differentiable
127125
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
128-
return input.maxPooled3D(kernelSize: poolSize, strides: strides, padding: padding)
126+
return maxPool3D(input, filterSize: poolSize, strides: strides, padding: padding)
129127
}
130128
}
131129

@@ -185,9 +183,8 @@ public struct AvgPool1D<Scalar: TensorFlowFloatingPoint>: Layer {
185183
/// - Returns: The output.
186184
@differentiable
187185
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
188-
return input.expandingShape(at: 1).averagePooled2D(
189-
kernelSize: (1, 1, poolSize, 1), strides: (1, 1, stride, 1), padding: padding
190-
).squeezingShape(at: 1)
186+
return avgPool2D(input.expandingShape(at: 1), filterSize: (1, 1, poolSize, 1),
187+
strides: (1, 1, stride, 1), padding: padding).squeezingShape(at: 1)
191188
}
192189
}
193190

@@ -219,7 +216,7 @@ public struct AvgPool2D<Scalar: TensorFlowFloatingPoint>: Layer {
219216
/// - Returns: The output.
220217
@differentiable
221218
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
222-
return input.averagePooled2D(kernelSize: poolSize, strides: strides, padding: padding)
219+
return avgPool2D(input, filterSize: poolSize, strides: strides, padding: padding)
223220
}
224221
}
225222

@@ -265,7 +262,7 @@ public struct AvgPool3D<Scalar: TensorFlowFloatingPoint>: Layer {
265262
/// - Returns: The output.
266263
@differentiable
267264
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
268-
return input.averagePooled3D(kernelSize: poolSize, strides: strides, padding: padding)
265+
return avgPool3D(input, filterSize: poolSize, strides: strides, padding: padding)
269266
}
270267
}
271268

0 commit comments

Comments
 (0)