diff --git a/README.md b/README.md index d301e51cd..df3ad1ade 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ struct Model: Layer { var layer3 = Dense(inputSize: hiddenSize, outputSize: 3, activation: identity) @differentiable - func call(_ input: Tensor) -> Tensor { + func callAsFunction(_ input: Tensor) -> Tensor { return input.sequenced(through: layer1, layer2, layer3) } } diff --git a/Sources/TensorFlow/Layer.swift b/Sources/TensorFlow/Layer.swift index 28f995f3e..9ed9d3e65 100644 --- a/Sources/TensorFlow/Layer.swift +++ b/Sources/TensorFlow/Layer.swift @@ -31,7 +31,7 @@ public protocol Layer: Differentiable & KeyPathIterable /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - func call(_ input: Input) -> Output + func callAsFunction(_ input: Input) -> Output } public extension Layer { diff --git a/Sources/TensorFlow/Layers/Convolutional.swift b/Sources/TensorFlow/Layers/Convolutional.swift index c96f5b3e4..63e0a3b86 100644 --- a/Sources/TensorFlow/Layers/Convolutional.swift +++ b/Sources/TensorFlow/Layers/Convolutional.swift @@ -59,7 +59,7 @@ public struct Conv1D: Layer { /// - Parameter input: The input to the layer `[batchCount, width, inputChannels]`. /// - Returns: The output `[batchCount, newWidth, outputChannels]`. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { let conv2D = input.expandingShape(at: 1).convolved2D( withFilter: filter.expandingShape(at: 0), strides: (1, 1, stride, 1), padding: padding) return activation(conv2D.squeezingShape(at: 1) + bias) @@ -177,7 +177,7 @@ public struct Conv2D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { return activation(input.convolved2D(withFilter: filter, strides: (1, strides.0, strides.1, 1), padding: padding) + bias) @@ -293,7 +293,7 @@ public struct Conv3D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { return activation(input.convolved3D(withFilter: filter, strides: (1, strides.0, strides.1, strides.2, 1), padding: padding) + bias) @@ -411,7 +411,7 @@ public struct TransposedConv2D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { let batchSize = input.shape[0] let w = (input.shape[1] - (1 * paddingIndex)) * strides.0 + (filter.shape[0] * paddingIndex) diff --git a/Sources/TensorFlow/Layers/Core.swift b/Sources/TensorFlow/Layers/Core.swift index c3b021f8a..20711e9d0 100644 --- a/Sources/TensorFlow/Layers/Core.swift +++ b/Sources/TensorFlow/Layers/Core.swift @@ -53,7 +53,7 @@ public struct Dropout: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable(vjp: _vjpApplied(to:)) - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { switch Context.local.learningPhase { case .training: return applyingTraining(to: input) @@ -92,7 +92,7 @@ public struct Flatten: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { let batchSize = input.shape[0] let remaining = input.shape[1..: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { return input.reshaped(toShape: shape) } } @@ -163,7 +163,7 @@ public struct Dense: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { return activation(matmul(input, weight) + bias) } } diff --git a/Sources/TensorFlow/Layers/Normalization.swift b/Sources/TensorFlow/Layers/Normalization.swift index 5eb4e7314..483d11969 100644 --- a/Sources/TensorFlow/Layers/Normalization.swift +++ b/Sources/TensorFlow/Layers/Normalization.swift @@ -90,7 +90,7 @@ public struct BatchNorm: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable(vjp: _vjpApplied(to:)) - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { switch Context.local.learningPhase { case .training: return applyingTraining(to: input) @@ -185,7 +185,7 @@ public struct LayerNorm: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { let mean = input.mean(alongAxes: axis) let variance = input.variance(alongAxes: axis) let inv = rsqrt(variance + epsilon) * scale diff --git a/Sources/TensorFlow/Layers/Pooling.swift b/Sources/TensorFlow/Layers/Pooling.swift index 7a9ca9e0e..0879987a3 100644 --- a/Sources/TensorFlow/Layers/Pooling.swift +++ b/Sources/TensorFlow/Layers/Pooling.swift @@ -43,7 +43,7 @@ public struct MaxPool1D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { return input.expandingShape(at: 1).maxPooled2D( kernelSize: (1, 1, poolSize, 1), strides: (1, 1, stride, 1), padding: padding ).squeezingShape(at: 1) @@ -77,7 +77,7 @@ public struct MaxPool2D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { return input.maxPooled2D( kernelSize: poolSize, strides: strides, padding: padding) } @@ -124,7 +124,7 @@ public struct MaxPool3D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { return input.maxPooled3D(kernelSize: poolSize, strides: strides, padding: padding) } } @@ -184,7 +184,7 @@ public struct AvgPool1D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { return input.expandingShape(at: 1).averagePooled2D( kernelSize: (1, 1, poolSize, 1), strides: (1, 1, stride, 1), padding: padding ).squeezingShape(at: 1) @@ -218,7 +218,7 @@ public struct AvgPool2D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { return input.averagePooled2D(kernelSize: poolSize, strides: strides, padding: padding) } } @@ -264,7 +264,7 @@ public struct AvgPool3D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { return input.averagePooled3D(kernelSize: poolSize, strides: strides, padding: padding) } } @@ -304,7 +304,7 @@ public struct GlobalAvgPool1D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { return input.mean(squeezingAxes: 1) } } @@ -320,7 +320,7 @@ public struct GlobalAvgPool2D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { return input.mean(squeezingAxes: [1, 2]) } } @@ -336,7 +336,7 @@ public struct GlobalAvgPool3D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { return input.mean(squeezingAxes: [1, 2, 3]) } } @@ -355,7 +355,7 @@ public struct GlobalMaxPool1D: Layer { /// phase. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { return input.max(squeezingAxes: 1) } } @@ -371,7 +371,7 @@ public struct GlobalMaxPool2D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { return input.max(squeezingAxes: [1, 2]) } } @@ -387,7 +387,7 @@ public struct GlobalMaxPool3D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { return input.max(squeezingAxes: [1, 2, 3]) } } diff --git a/Sources/TensorFlow/Layers/Recurrent.swift b/Sources/TensorFlow/Layers/Recurrent.swift index a60610dde..305ec9422 100644 --- a/Sources/TensorFlow/Layers/Recurrent.swift +++ b/Sources/TensorFlow/Layers/Recurrent.swift @@ -62,7 +62,7 @@ public extension RNNCell { /// - previousState: The previous state of the RNN cell. /// - Returns: The output. @differentiable - func call(input: TimeStepInput, state: State) -> RNNCellOutput { + func callAsFunction(input: TimeStepInput, state: State) -> RNNCellOutput { return self(RNNCellInput(input: input, state: state)) } } @@ -113,7 +113,7 @@ public struct SimpleRNNCell: RNNCell, VectorNum /// - Parameter input: The input to the layer. /// - Returns: The hidden state. @differentiable - public func call(_ input: Input) -> Output { + public func callAsFunction(_ input: Input) -> Output { let concatenatedInput = input.input.concatenated(with: input.state.value, alongAxis: 1) let newState = State(tanh(matmul(concatenatedInput, weight) + bias)) return Output(output: newState, state: newState) @@ -175,7 +175,7 @@ public struct LSTMCell: RNNCell, VectorNumeric /// - Parameter input: The input to the layer. /// - Returns: The hidden state. @differentiable - public func call(_ input: Input) -> Output { + public func callAsFunction(_ input: Input) -> Output { let gateInput = input.input.concatenated(with: input.state.hidden, alongAxis: 1) let inputGate = sigmoid(matmul(gateInput, inputWeight) + inputBias) @@ -203,7 +203,7 @@ public struct RNN: Layer { } @differentiable(wrt: (self, input), vjp: _vjpCall(_:initialState:)) - public func call(_ input: [Cell.TimeStepInput], + public func callAsFunction(_ input: [Cell.TimeStepInput], initialState: Cell.State) -> [Cell.TimeStepOutput] { var currentHiddenState = initialState var timeStepOutputs: [Cell.TimeStepOutput] = [] @@ -253,7 +253,7 @@ public struct RNN: Layer { } @differentiable(wrt: (self, inputs)) - public func call(_ inputs: [Cell.TimeStepInput]) -> [Cell.TimeStepOutput] { + public func callAsFunction(_ inputs: [Cell.TimeStepInput]) -> [Cell.TimeStepOutput] { return self(inputs, initialState: cell.zeroState.withoutDerivative()) } diff --git a/Sources/TensorFlow/Layers/Upsampling.swift b/Sources/TensorFlow/Layers/Upsampling.swift index 4fbbacdf3..6ebccd332 100644 --- a/Sources/TensorFlow/Layers/Upsampling.swift +++ b/Sources/TensorFlow/Layers/Upsampling.swift @@ -29,7 +29,7 @@ public struct UpSampling1D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { let shape = input.shape let (batchSize, timesteps, channels) = (shape[0], shape[1], shape[2]) let scaleOnes = Tensor(ones: [1, 1, size, 1]) @@ -55,7 +55,7 @@ public struct UpSampling2D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { let shape = input.shape let (batchSize, height, width, channels) = (shape[0], shape[1], shape[2], shape[3]) let scaleOnes = Tensor(ones: [1, 1, size, 1, size, 1]) @@ -107,7 +107,7 @@ public struct UpSampling3D: Layer { /// - Parameter input: The input to the layer. /// - Returns: The output. @differentiable - public func call(_ input: Tensor) -> Tensor { + public func callAsFunction(_ input: Tensor) -> Tensor { var result = repeatingElements(input, alongAxis: 1, count: size) result = repeatingElements(result, alongAxis: 2, count: size) result = repeatingElements(result, alongAxis: 3, count: size) diff --git a/Tests/TensorFlowTests/SequentialTests.swift b/Tests/TensorFlowTests/SequentialTests.swift index c7706fba3..8d305e9a1 100644 --- a/Tests/TensorFlowTests/SequentialTests.swift +++ b/Tests/TensorFlowTests/SequentialTests.swift @@ -24,7 +24,7 @@ final class SequentialTests: XCTestCase { seed: (0xeffeffe, 0xfffe)) @differentiable - func call(_ input: Tensor) -> Tensor { + func callAsFunction(_ input: Tensor) -> Tensor { return input.sequenced(through: dense1, dense2) } } diff --git a/Tests/TensorFlowTests/TrivialModelTests.swift b/Tests/TensorFlowTests/TrivialModelTests.swift index b2b20f179..1a198ac30 100644 --- a/Tests/TensorFlowTests/TrivialModelTests.swift +++ b/Tests/TensorFlowTests/TrivialModelTests.swift @@ -34,7 +34,7 @@ final class TrivialModelTests: XCTestCase { ) } @differentiable - func call(_ input: Tensor) -> Tensor { + func callAsFunction(_ input: Tensor) -> Tensor { let h1 = l1(input) return l2(h1) }