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

Commit 76f5ee6

Browse files
Shashi456dan-zheng
authored andcommitted
Solving errors in AvgPool3d and Maxpool3d Implementations (#119)
* Add Avgpool3d and Maxpool3d * Adding tests and init extensions * Removing self.init from top level functions * Adding vjps for Avgpool3d and maxpool3d
1 parent 5883af6 commit 76f5ee6

File tree

3 files changed

+288
-35
lines changed

3 files changed

+288
-35
lines changed

Sources/DeepLearning/Layer.swift

Lines changed: 137 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ public struct MaxPool1D<Scalar: TensorFlowFloatingPoint>: Layer {
794794
/// - Returns: The output.
795795
@differentiable
796796
public func call(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
797-
return input.expandingShape(at: 1).maxPooled(
797+
return input.expandingShape(at: 1).maxPooled2D(
798798
kernelSize: (1, 1, poolSize, 1), strides: (1, 1, stride, 1), padding: padding
799799
).squeezingShape(at: 1)
800800
}
@@ -822,15 +822,50 @@ public struct MaxPool2D<Scalar: TensorFlowFloatingPoint>: Layer {
822822
self.padding = padding
823823
}
824824

825-
/// Creates a max pooling layer.
825+
/// Returns the output obtained from applying the layer to the given input.
826826
///
827-
/// - Parameters:
828-
/// - poolSize: Vertical and horizontal factors by which to downscale.
829-
/// - strides: The strides.
830-
/// - padding: The padding.
831-
public init(poolSize: (Int, Int), strides: (Int, Int), padding: Padding = .valid) {
832-
self.poolSize = (1, poolSize.0, poolSize.1, 1)
833-
self.strides = (1, strides.0, strides.1, 1)
827+
/// - Parameter input: The input to the layer.
828+
/// - Returns: The output.
829+
@differentiable
830+
public func call(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
831+
return input.maxPooled2D(
832+
kernelSize: poolSize, strides: strides, padding: padding)
833+
}
834+
}
835+
836+
public extension MaxPool2D {
837+
/// Creates a max pooling layer.
838+
///
839+
/// - Parameters:
840+
/// - poolSize: Vertical and horizontal factors by which to downscale.
841+
/// - strides: The strides.
842+
/// - padding: The padding.
843+
init(poolSize: (Int, Int), strides: (Int, Int), padding: Padding = .valid) {
844+
self.init(poolSize: (1, poolSize.0, poolSize.1, 1),
845+
strides: (1, strides.0, strides.1, 1),
846+
padding: padding)
847+
}
848+
}
849+
850+
/// A max pooling layer for spatial or spatio-temporal data.
851+
@_fixed_layout
852+
public struct MaxPool3D<Scalar: TensorFlowFloatingPoint>: Layer {
853+
/// The size of the sliding reduction window for pooling.
854+
@noDerivative let poolSize: (Int, Int, Int, Int, Int)
855+
/// The strides of the sliding window for each dimension of a 5-D input.
856+
/// Strides in non-spatial dimensions must be `1`.
857+
@noDerivative let strides: (Int, Int, Int, Int, Int)
858+
/// The padding algorithm for pooling.
859+
@noDerivative let padding: Padding
860+
861+
/// Creates a max pooling layer.
862+
public init(
863+
poolSize: (Int, Int, Int, Int, Int),
864+
strides: (Int, Int, Int, Int, Int),
865+
padding: Padding
866+
) {
867+
self.poolSize = poolSize
868+
self.strides = strides
834869
self.padding = padding
835870
}
836871

@@ -840,11 +875,34 @@ public struct MaxPool2D<Scalar: TensorFlowFloatingPoint>: Layer {
840875
/// - Returns: The output.
841876
@differentiable
842877
public func call(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
843-
return input.maxPooled(
844-
kernelSize: poolSize, strides: strides, padding: padding)
878+
return input.maxPooled3D(kernelSize: poolSize, strides: strides, padding: padding)
845879
}
846880
}
847881

882+
public extension MaxPool3D {
883+
/// Creates a max pooling layer.
884+
///
885+
/// - Parameters:
886+
/// - poolSize: Vertical and horizontal factors by which to downscale.
887+
/// - strides: The strides.
888+
/// - padding: The padding.
889+
init(poolSize: (Int, Int, Int), strides: (Int, Int, Int), padding: Padding = .valid) {
890+
self.init(poolSize: (1, poolSize.0, poolSize.1, poolSize.2, 1),
891+
strides: (1, strides.0, strides.1, strides.2, 1),
892+
padding: padding)
893+
}
894+
}
895+
896+
public extension MaxPool3D {
897+
/// Creates a max pooling layer with the specified pooling window size and stride. All
898+
/// pooling sizes and strides are the same.
899+
init(poolSize: Int, stride: Int, padding: Padding = .valid) {
900+
self.init(poolSize: (poolSize, poolSize, poolSize),
901+
strides: (stride, stride, stride),
902+
padding: padding)
903+
}
904+
}
905+
848906
/// An average pooling layer for temporal data.
849907
@_fixed_layout
850908
public struct AvgPool1D<Scalar: TensorFlowFloatingPoint>: Layer {
@@ -877,7 +935,7 @@ public struct AvgPool1D<Scalar: TensorFlowFloatingPoint>: Layer {
877935
/// - Returns: The output.
878936
@differentiable
879937
public func call(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
880-
return input.expandingShape(at: 1).averagePooled(
938+
return input.expandingShape(at: 1).averagePooled2D(
881939
kernelSize: (1, 1, poolSize, 1), strides: (1, 1, stride, 1), padding: padding
882940
).squeezingShape(at: 1)
883941
}
@@ -894,7 +952,7 @@ public struct AvgPool2D<Scalar: TensorFlowFloatingPoint>: Layer {
894952
/// The padding algorithm for pooling.
895953
@noDerivative let padding: Padding
896954

897-
/// Creates a average pooling layer.
955+
/// Creates an average pooling layer.
898956
public init(
899957
poolSize: (Int, Int, Int, Int),
900958
strides: (Int, Int, Int, Int),
@@ -905,15 +963,49 @@ public struct AvgPool2D<Scalar: TensorFlowFloatingPoint>: Layer {
905963
self.padding = padding
906964
}
907965

908-
/// Creates a average pooling layer.
966+
/// Returns the output obtained from applying the layer to the given input.
909967
///
910-
/// - Parameters:
911-
/// - poolSize: Vertical and horizontal factors by which to downscale.
912-
/// - strides: The strides.
913-
/// - padding: The padding.
914-
public init(poolSize: (Int, Int), strides: (Int, Int), padding: Padding = .valid) {
915-
self.poolSize = (1, poolSize.0, poolSize.1, 1)
916-
self.strides = (1, strides.0, strides.1, 1)
968+
/// - Parameter input: The input to the layer.
969+
/// - Returns: The output.
970+
@differentiable
971+
public func call(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
972+
return input.averagePooled2D(kernelSize: poolSize, strides: strides, padding: padding)
973+
}
974+
}
975+
976+
public extension AvgPool2D {
977+
/// Creates an average pooling layer.
978+
///
979+
/// - Parameters:
980+
/// - poolSize: Vertical and horizontal factors by which to downscale.
981+
/// - strides: The strides.
982+
/// - padding: The padding.
983+
init(poolSize: (Int, Int), strides: (Int, Int), padding: Padding = .valid) {
984+
self.init(poolSize: (1, poolSize.0, poolSize.1, 1),
985+
strides: (1, strides.0, strides.1, 1),
986+
padding: padding)
987+
}
988+
}
989+
990+
/// An average pooling layer for spatial or spatio-temporal data.
991+
@_fixed_layout
992+
public struct AvgPool3D<Scalar: TensorFlowFloatingPoint>: Layer {
993+
/// The size of the sliding reduction window for pooling.
994+
@noDerivative let poolSize: (Int, Int, Int, Int, Int)
995+
/// The strides of the sliding window for each dimension of a 5-D input.
996+
/// Strides in non-spatial dimensions must be `1`.
997+
@noDerivative let strides: (Int, Int, Int, Int, Int)
998+
/// The padding algorithm for pooling.
999+
@noDerivative let padding: Padding
1000+
1001+
/// Creates an average pooling layer.
1002+
public init(
1003+
poolSize: (Int, Int, Int, Int, Int),
1004+
strides: (Int, Int, Int, Int, Int),
1005+
padding: Padding
1006+
) {
1007+
self.poolSize = poolSize
1008+
self.strides = strides
9171009
self.padding = padding
9181010
}
9191011

@@ -923,10 +1015,33 @@ public struct AvgPool2D<Scalar: TensorFlowFloatingPoint>: Layer {
9231015
/// - Returns: The output.
9241016
@differentiable
9251017
public func call(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
926-
return input.averagePooled(kernelSize: poolSize, strides: strides, padding: padding)
1018+
return input.averagePooled3D(kernelSize: poolSize, strides: strides, padding: padding)
9271019
}
9281020
}
9291021

1022+
public extension AvgPool3D {
1023+
/// Creates an average pooling layer.
1024+
///
1025+
/// - Parameters:
1026+
/// - poolSize: Vertical and horizontal factors by which to downscale.
1027+
/// - strides: The strides.
1028+
/// - padding: The padding.
1029+
init(poolSize: (Int, Int, Int), strides: (Int, Int, Int), padding: Padding = .valid) {
1030+
self.init(poolSize: (1, poolSize.0, poolSize.1, poolSize.2, 1),
1031+
strides: (1, strides.0, strides.1, strides.2, 1),
1032+
padding: padding)
1033+
}
1034+
}
1035+
1036+
public extension AvgPool3D {
1037+
/// Creates an average pooling layer with the specified pooling window size and stride. All
1038+
/// pooling sizes and strides are the same.
1039+
init(poolSize: Int, strides: Int, padding: Padding = .valid) {
1040+
self.init(poolSize: (poolSize, poolSize, poolSize),
1041+
strides: (strides, strides, strides),
1042+
padding: padding)
1043+
}
1044+
}
9301045

9311046
/// A global average pooling layer for temporal data.
9321047
@_fixed_layout

0 commit comments

Comments
 (0)