From fabf42408b40e6e4efdf3122932f120ce17c37d3 Mon Sep 17 00:00:00 2001 From: PAWAN SASANKA AMMANAMANCHI Date: Thu, 4 Apr 2019 13:07:23 +0530 Subject: [PATCH 01/10] Adding GlobalMaxPooling 1D, 2D, 3D Adding GlobalMaxPooling 1D, 2D, 3D --- Sources/DeepLearning/Operators.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/DeepLearning/Operators.swift b/Sources/DeepLearning/Operators.swift index 26e3187be..f1222ed53 100644 --- a/Sources/DeepLearning/Operators.swift +++ b/Sources/DeepLearning/Operators.swift @@ -44,6 +44,7 @@ public extension Tensor where Scalar: TensorFlowFloatingPoint { epsilon: epsilon) return (value, { v in let mean = self.mean(alongAxes: axis) + let max = self.max(alongAxes: axis) let squaredDiff: Tensor = Raw.squaredDifference(self, mean) let variance = squaredDiff.mean(alongAxes: axis) From 5f214b5bbb2d0b8c3c6249d26efa725e55895dca Mon Sep 17 00:00:00 2001 From: PAWAN SASANKA AMMANAMANCHI Date: Wed, 10 Apr 2019 10:54:57 +0530 Subject: [PATCH 02/10] Review changes --- Sources/DeepLearning/Operators.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/DeepLearning/Operators.swift b/Sources/DeepLearning/Operators.swift index f1222ed53..26e3187be 100644 --- a/Sources/DeepLearning/Operators.swift +++ b/Sources/DeepLearning/Operators.swift @@ -44,7 +44,6 @@ public extension Tensor where Scalar: TensorFlowFloatingPoint { epsilon: epsilon) return (value, { v in let mean = self.mean(alongAxes: axis) - let max = self.max(alongAxes: axis) let squaredDiff: Tensor = Raw.squaredDifference(self, mean) let variance = squaredDiff.mean(alongAxes: axis) From 95691208222a538b75fc9b345ae013980cea5888 Mon Sep 17 00:00:00 2001 From: PAWAN SASANKA AMMANAMANCHI Date: Wed, 17 Apr 2019 10:01:13 +0530 Subject: [PATCH 03/10] Changing along axes to squeezing axes --- Sources/DeepLearning/Layer.swift | 70 ++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/Sources/DeepLearning/Layer.swift b/Sources/DeepLearning/Layer.swift index 2eeb63f01..b521ff242 100644 --- a/Sources/DeepLearning/Layer.swift +++ b/Sources/DeepLearning/Layer.swift @@ -973,6 +973,7 @@ public struct AvgPool2D: Layer { } } +<<<<<<< HEAD public extension AvgPool2D { /// Creates an average pooling layer. /// @@ -985,6 +986,43 @@ public extension AvgPool2D { strides: (1, strides.0, strides.1, 1), padding: padding) } +||||||| merged common ancestors +/// A global max pooling layer for temporal data. +@_fixed_layout +public struct GlobalMaxPooling1D: Layer { + /// Creates a global max pooling layer. + public init() {} + + /// Returns the output obtained from applying the layer to the given input. + /// + /// - Parameters: + /// - input: The input to the layer. + /// - context: The contextual information for the layer application, e.g. the current learning + /// phase. + /// - Returns: The output. + @differentiable + public func applied(to input: Tensor, in _: Context) -> Tensor { + return input.max(alongAxes: 1).reshaped(to: [input.shape[0], input.shape[2]]) + } +======= +/// A global max pooling layer for temporal data. +@_fixed_layout +public struct GlobalMaxPooling1D: Layer { + /// Creates a global max pooling layer. + public init() {} + + /// Returns the output obtained from applying the layer to the given input. + /// + /// - Parameters: + /// - input: The input to the layer. + /// - context: The contextual information for the layer application, e.g. the current learning + /// phase. + /// - Returns: The output. + @differentiable + public func applied(to input: Tensor, in _: Context) -> Tensor { + return input.max(squeezingAxes: 1) + } +>>>>>>> Changing along axes to squeezing axes } /// An average pooling layer for spatial or spatio-temporal data. @@ -1014,8 +1052,16 @@ public struct AvgPool3D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable +<<<<<<< HEAD public func call(_ input: Tensor) -> Tensor { return input.averagePooled3D(kernelSize: poolSize, strides: strides, padding: padding) +||||||| merged common ancestors + public func applied(to input: Tensor, in _: Context) -> Tensor { + return input.max(alongAxes: [1, 2]).reshaped(to: [input.shape[0], input.shape[3]]) +======= + public func applied(to input: Tensor, in _: Context) -> Tensor { + return input.max(squeezingAxes: [1, 2]) +>>>>>>> Changing along axes to squeezing axes } } @@ -1033,6 +1079,7 @@ public extension AvgPool3D { } } +<<<<<<< HEAD public extension AvgPool3D { /// Creates an average pooling layer with the specified pooling window size and stride. All /// pooling sizes and strides are the same. @@ -1040,6 +1087,29 @@ public extension AvgPool3D { self.init(poolSize: (poolSize, poolSize, poolSize), strides: (strides, strides, strides), padding: padding) +||||||| merged common ancestors + /// Returns the output obtained from applying the layer to the given input. + /// + /// - Parameters: + /// - input: The input to the layer. + /// - context: The contextual information for the layer application, e.g. the current learning + /// phase. + /// - Returns: The output. + @differentiable + public func applied(to input: Tensor, in _: Context) -> Tensor { + return input.max(alongAxes: [1, 2, 3]).reshaped(to: [input.shape[0], input.shape[4]]) +======= + /// Returns the output obtained from applying the layer to the given input. + /// + /// - Parameters: + /// - input: The input to the layer. + /// - context: The contextual information for the layer application, e.g. the current learning + /// phase. + /// - Returns: The output. + @differentiable + public func applied(to input: Tensor, in _: Context) -> Tensor { + return input.max(squeezingAxes: [1, 2, 3]) +>>>>>>> Changing along axes to squeezing axes } } From 3b8a86513c809783eafb1b65485d8fa897c0fa1e Mon Sep 17 00:00:00 2001 From: PAWAN SASANKA AMMANAMANCHI Date: Fri, 26 Apr 2019 21:20:05 +0530 Subject: [PATCH 04/10] Add tests --- Tests/DeepLearningTests/LayerTests.swift | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Tests/DeepLearningTests/LayerTests.swift b/Tests/DeepLearningTests/LayerTests.swift index 4647f0366..8104cbec3 100644 --- a/Tests/DeepLearningTests/LayerTests.swift +++ b/Tests/DeepLearningTests/LayerTests.swift @@ -98,6 +98,30 @@ final class LayerTests: XCTestCase { XCTAssertEqual(output, expected) } + func testGlobalMaxPool1D() { + let layer = GlobalMaxPool1D() + let input = Tensor(shape: [1, 10, 1], scalars: (0..<10).map(Float.init)) + let output = layer.inferring(from: input) + let expected = Tensor([9]) + XCTAssertEqual(output, expected) + } + + func testGlobalMaxPool2D() { + let layer = GlobalMaxPool2D() + let input = Tensor(shape: [1, 2, 10, 1], scalars: (0..<20).map(Float.init)) + let output = layer.inferring(from: input) + let expected = Tensor([19]) + XCTAssertEqual(output, expected) + } + + func testGlobalMaxPool3D() { + let layer = GlobalMaxPool3D() + let input = Tensor(shape: [1, 2, 3, 5, 1], scalars: (0..<30).map(Float.init)) + let output = layer.inferring(from: input) + let expected = Tensor([29]) + XCTAssertEqual(output, expected) + } + func testUpSampling1D() { let size = 6 let layer = UpSampling1D(size: size) @@ -196,6 +220,9 @@ final class LayerTests: XCTestCase { ("testGlobalAvgPool1D", testGlobalAvgPool1D), ("testGlobalAvgPool2D", testGlobalAvgPool2D), ("testGlobalAvgPool3D", testGlobalAvgPool3D), + ("testGlobalMaxPool1D", testGlobalMaxPool1D), + ("testGlobalMaxPool2D", testGlobalMaxPool2D), + ("testGlobalMaxPool3D", testGlobalMaxPool3D), ("testUpSampling1D", testUpSampling1D), ("testUpSampling2D", testUpSampling2D), ("testUpSampling3D", testUpSampling3D), From dd2098101c8e5f8a85bbc27c900855d26ae08002 Mon Sep 17 00:00:00 2001 From: PAWAN SASANKA AMMANAMANCHI Date: Sun, 19 May 2019 15:14:24 +0530 Subject: [PATCH 05/10] merge errors --- Sources/DeepLearning/Layer.swift | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/Sources/DeepLearning/Layer.swift b/Sources/DeepLearning/Layer.swift index b521ff242..52ea5f083 100644 --- a/Sources/DeepLearning/Layer.swift +++ b/Sources/DeepLearning/Layer.swift @@ -973,7 +973,6 @@ public struct AvgPool2D: Layer { } } -<<<<<<< HEAD public extension AvgPool2D { /// Creates an average pooling layer. /// @@ -986,7 +985,7 @@ public extension AvgPool2D { strides: (1, strides.0, strides.1, 1), padding: padding) } -||||||| merged common ancestors + /// A global max pooling layer for temporal data. @_fixed_layout public struct GlobalMaxPooling1D: Layer { @@ -1004,7 +1003,7 @@ public struct GlobalMaxPooling1D: Layer { public func applied(to input: Tensor, in _: Context) -> Tensor { return input.max(alongAxes: 1).reshaped(to: [input.shape[0], input.shape[2]]) } -======= + /// A global max pooling layer for temporal data. @_fixed_layout public struct GlobalMaxPooling1D: Layer { @@ -1022,7 +1021,6 @@ public struct GlobalMaxPooling1D: Layer { public func applied(to input: Tensor, in _: Context) -> Tensor { return input.max(squeezingAxes: 1) } ->>>>>>> Changing along axes to squeezing axes } /// An average pooling layer for spatial or spatio-temporal data. @@ -1052,16 +1050,14 @@ public struct AvgPool3D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable -<<<<<<< HEAD public func call(_ input: Tensor) -> Tensor { return input.averagePooled3D(kernelSize: poolSize, strides: strides, padding: padding) -||||||| merged common ancestors + public func applied(to input: Tensor, in _: Context) -> Tensor { return input.max(alongAxes: [1, 2]).reshaped(to: [input.shape[0], input.shape[3]]) -======= + public func applied(to input: Tensor, in _: Context) -> Tensor { return input.max(squeezingAxes: [1, 2]) ->>>>>>> Changing along axes to squeezing axes } } @@ -1079,7 +1075,6 @@ public extension AvgPool3D { } } -<<<<<<< HEAD public extension AvgPool3D { /// Creates an average pooling layer with the specified pooling window size and stride. All /// pooling sizes and strides are the same. @@ -1087,7 +1082,7 @@ public extension AvgPool3D { self.init(poolSize: (poolSize, poolSize, poolSize), strides: (strides, strides, strides), padding: padding) -||||||| merged common ancestors + /// Returns the output obtained from applying the layer to the given input. /// /// - Parameters: @@ -1098,7 +1093,7 @@ public extension AvgPool3D { @differentiable public func applied(to input: Tensor, in _: Context) -> Tensor { return input.max(alongAxes: [1, 2, 3]).reshaped(to: [input.shape[0], input.shape[4]]) -======= + /// Returns the output obtained from applying the layer to the given input. /// /// - Parameters: @@ -1109,7 +1104,6 @@ public extension AvgPool3D { @differentiable public func applied(to input: Tensor, in _: Context) -> Tensor { return input.max(squeezingAxes: [1, 2, 3]) ->>>>>>> Changing along axes to squeezing axes } } From 0ada979a2bf4f039c7798170c2610099f629d7d5 Mon Sep 17 00:00:00 2001 From: PAWAN SASANKA AMMANAMANCHI Date: Sun, 19 May 2019 15:24:19 +0530 Subject: [PATCH 06/10] rebase errors --- Sources/DeepLearning/Layer.swift | 83 ++++++++++++++------------------ 1 file changed, 35 insertions(+), 48 deletions(-) diff --git a/Sources/DeepLearning/Layer.swift b/Sources/DeepLearning/Layer.swift index 52ea5f083..21240d78f 100644 --- a/Sources/DeepLearning/Layer.swift +++ b/Sources/DeepLearning/Layer.swift @@ -985,42 +985,6 @@ public extension AvgPool2D { strides: (1, strides.0, strides.1, 1), padding: padding) } - -/// A global max pooling layer for temporal data. -@_fixed_layout -public struct GlobalMaxPooling1D: Layer { - /// Creates a global max pooling layer. - public init() {} - - /// Returns the output obtained from applying the layer to the given input. - /// - /// - Parameters: - /// - input: The input to the layer. - /// - context: The contextual information for the layer application, e.g. the current learning - /// phase. - /// - Returns: The output. - @differentiable - public func applied(to input: Tensor, in _: Context) -> Tensor { - return input.max(alongAxes: 1).reshaped(to: [input.shape[0], input.shape[2]]) - } - -/// A global max pooling layer for temporal data. -@_fixed_layout -public struct GlobalMaxPooling1D: Layer { - /// Creates a global max pooling layer. - public init() {} - - /// Returns the output obtained from applying the layer to the given input. - /// - /// - Parameters: - /// - input: The input to the layer. - /// - context: The contextual information for the layer application, e.g. the current learning - /// phase. - /// - Returns: The output. - @differentiable - public func applied(to input: Tensor, in _: Context) -> Tensor { - return input.max(squeezingAxes: 1) - } } /// An average pooling layer for spatial or spatio-temporal data. @@ -1052,12 +1016,6 @@ public struct AvgPool3D: Layer { @differentiable public func call(_ input: Tensor) -> Tensor { return input.averagePooled3D(kernelSize: poolSize, strides: strides, padding: padding) - - public func applied(to input: Tensor, in _: Context) -> Tensor { - return input.max(alongAxes: [1, 2]).reshaped(to: [input.shape[0], input.shape[3]]) - - public func applied(to input: Tensor, in _: Context) -> Tensor { - return input.max(squeezingAxes: [1, 2]) } } @@ -1082,6 +1040,14 @@ public extension AvgPool3D { self.init(poolSize: (poolSize, poolSize, poolSize), strides: (strides, strides, strides), padding: padding) + } +} + +/// A global max pooling layer for temporal data. +@_fixed_layout +public struct GlobalMaxPooling1D: Layer { + /// Creates a global max pooling layer. + public init() {} /// Returns the output obtained from applying the layer to the given input. /// @@ -1092,17 +1058,38 @@ public extension AvgPool3D { /// - Returns: The output. @differentiable public func applied(to input: Tensor, in _: Context) -> Tensor { - return input.max(alongAxes: [1, 2, 3]).reshaped(to: [input.shape[0], input.shape[4]]) + return input.max(squeezingAxes: 1) + } +} + +/// A global max pooling layer for spatial data. +@_fixed_layout +public struct GlobalMaxPool2D: Layer { + /// Creates a global max pooling layer. + public init() {} /// Returns the output obtained from applying the layer to the given input. /// - /// - Parameters: - /// - input: The input to the layer. - /// - context: The contextual information for the layer application, e.g. the current learning - /// phase. + /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func applied(to input: Tensor, in _: Context) -> Tensor { + public func call(_ input: Tensor) -> Tensor { + return input.max(squeezingAxes: [1, 2]) + } +} + +/// A global max pooling layer for spatial and spatio-temporal data. +@_fixed_layout +public struct GlobalMaxPool3D: Layer { + /// Creates a global max pooling layer. + public init() {} + + /// Returns the output obtained from applying the layer to the given input. + /// + /// - Parameter input: The input to the layer. + /// - Returns: The output. + @differentiable + public func call(_ input: Tensor) -> Tensor { return input.max(squeezingAxes: [1, 2, 3]) } } From a1079f3d2015dd3d12cdc61fc8acf412c437d2df Mon Sep 17 00:00:00 2001 From: PAWAN SASANKA AMMANAMANCHI Date: Sun, 19 May 2019 15:34:29 +0530 Subject: [PATCH 07/10] call error --- Sources/DeepLearning/Layer.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/DeepLearning/Layer.swift b/Sources/DeepLearning/Layer.swift index 21240d78f..c21972c7c 100644 --- a/Sources/DeepLearning/Layer.swift +++ b/Sources/DeepLearning/Layer.swift @@ -1057,7 +1057,7 @@ public struct GlobalMaxPooling1D: Layer { /// phase. /// - Returns: The output. @differentiable - public func applied(to input: Tensor, in _: Context) -> Tensor { + public func call(_ input: Tensor) -> Tensor { return input.max(squeezingAxes: 1) } } From 07d15fdd815c4278d924f59dc984ce447a810ad3 Mon Sep 17 00:00:00 2001 From: PAWAN SASANKA AMMANAMANCHI Date: Tue, 28 May 2019 13:15:38 +0530 Subject: [PATCH 08/10] updating branch --- Sources/DeepLearning/Layers/Pooling.Swift | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/Sources/DeepLearning/Layers/Pooling.Swift b/Sources/DeepLearning/Layers/Pooling.Swift index 58dc46364..a88bbca23 100644 --- a/Sources/DeepLearning/Layers/Pooling.Swift +++ b/Sources/DeepLearning/Layers/Pooling.Swift @@ -345,3 +345,54 @@ public struct GlobalAvgPool3D: Layer { return input.mean(squeezingAxes: [1, 2, 3]) } } + +/// A global max pooling layer for temporal data. +@_fixed_layout +public struct GlobalMaxPooling1D: Layer { + /// Creates a global max pooling layer. + public init() {} + + /// Returns the output obtained from applying the layer to the given input. + /// + /// - Parameters: + /// - input: The input to the layer. + /// - context: The contextual information for the layer application, e.g. the current learning + /// phase. + /// - Returns: The output. + @differentiable + public func applied(to input: Tensor, in _: Context) -> Tensor { + return input.max(squeezingAxes: 1) + } +} + +/// A global max pooling layer for spatial data. +@_fixed_layout +public struct GlobalMaxPool2D: Layer { + /// Creates a global max pooling layer. + public init() {} + + /// Returns the output obtained from applying the layer to the given input. + /// + /// - Parameter input: The input to the layer. + /// - Returns: The output. + @differentiable + public func call(_ input: Tensor) -> Tensor { + return input.max(squeezingAxes: [1, 2]) + } +} + +/// A global max pooling layer for spatial and spatio-temporal data. +@_fixed_layout +public struct GlobalMaxPool3D: Layer { + /// Creates a global max pooling layer. + public init() {} + + /// Returns the output obtained from applying the layer to the given input. + /// + /// - Parameter input: The input to the layer. + /// - Returns: The output. + @differentiable + public func call(_ input: Tensor) -> Tensor { + return input.max(squeezingAxes: [1, 2, 3]) + } +} From cda3a82e56c5daa92848d95765dab11f49bc7cf4 Mon Sep 17 00:00:00 2001 From: PAWAN SASANKA AMMANAMANCHI Date: Fri, 31 May 2019 11:01:53 +0530 Subject: [PATCH 09/10] fix build errors --- Sources/TensorFlow/Layers/Pooling.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/TensorFlow/Layers/Pooling.swift b/Sources/TensorFlow/Layers/Pooling.swift index 87b4f3fd7..2bd8597f7 100644 --- a/Sources/TensorFlow/Layers/Pooling.swift +++ b/Sources/TensorFlow/Layers/Pooling.swift @@ -355,7 +355,7 @@ public struct GlobalMaxPooling1D: Layer { /// phase. /// - Returns: The output. @differentiable - public func applied(to input: Tensor, in _: Context) -> Tensor { + public func call(_ input: Tensor) -> Tensor { return input.max(squeezingAxes: 1) } } From bd7b3cab4eb1ddc1273858c0be347e2de8f9f5c0 Mon Sep 17 00:00:00 2001 From: Pawan Sasanka Ammanamanchi Date: Fri, 31 May 2019 13:30:32 +0530 Subject: [PATCH 10/10] Build errors --- Sources/TensorFlow/Layers/Pooling.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/TensorFlow/Layers/Pooling.swift b/Sources/TensorFlow/Layers/Pooling.swift index 2bd8597f7..7a9ca9e0e 100644 --- a/Sources/TensorFlow/Layers/Pooling.swift +++ b/Sources/TensorFlow/Layers/Pooling.swift @@ -343,7 +343,7 @@ public struct GlobalAvgPool3D: Layer { /// A global max pooling layer for temporal data. @_fixed_layout -public struct GlobalMaxPooling1D: Layer { +public struct GlobalMaxPool1D: Layer { /// Creates a global max pooling layer. public init() {}