From 99f23e4e82b954415406ed261e448dba58933504 Mon Sep 17 00:00:00 2001 From: Tanmay Bakshi Date: Thu, 28 Mar 2019 05:59:14 -0400 Subject: [PATCH] Add 1D version of UpSampling --- Sources/DeepLearning/Layer.swift | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Sources/DeepLearning/Layer.swift b/Sources/DeepLearning/Layer.swift index b104aac99..4a3553654 100644 --- a/Sources/DeepLearning/Layer.swift +++ b/Sources/DeepLearning/Layer.swift @@ -1011,6 +1011,35 @@ public struct Dropout: Layer { } } +/// An upsampling layer for 1-D inputs. +@_fixed_layout +public struct UpSampling1D: Layer { + @noDerivative public let size: Int32 + + /// Creates an upsampling layer. + /// + /// - Parameter size: The upsampling factor for timesteps. + public init(size: Int32) { + self.size = size + } + + /// 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 { + let shape = input.shape + let (batchSize, timesteps, channels) = (shape[0], shape[1], shape[2]) + let scaleOnes = Tensor(ones: [1, 1, size, 1]) + let upSampling = input.reshaped(to: [batchSize, timesteps, 1, channels]) * scaleOnes + return upSampling.reshaped(to: [batchSize, timesteps * size, channels]) + } +} + /// An upsampling layer for 2-D inputs. @_fixed_layout public struct UpSampling2D: Layer {