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

Add Function/Lambda Layer #298

Merged
merged 7 commits into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Sources/TensorFlow/Layers/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,19 @@ public extension Dense {
activation: activation)
}
}

/// A layer that encloses a custom differentiable function.
public struct Function<Input: Differentiable, Output: Differentiable>: Layer {
public typealias Body = @differentiable (Input) -> Output

@noDerivative public let body: Body

public init(_ body: @escaping Body) {
self.body = body
}

@differentiable
public func callAsFunction(_ input: Input) -> Output {
return body(input)
}
}
19 changes: 14 additions & 5 deletions Tests/TensorFlowTests/LayerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class LayerTests: XCTestCase {
// Input shapes.
let inputHeight = 2
let inputWidth = 5

let filter = Tensor<Float>(shape: [width, inputChannels, outputChannels],
scalars: [2, 3, 4, 1, 2, 3])
let bias = Tensor<Float>([0])
Expand Down Expand Up @@ -256,14 +256,14 @@ final class LayerTests: XCTestCase {
XCTAssertEqual(output.shape, expected)
}

func testEmbedding() {
var layer = Embedding<Float>(vocabularySize: 3, embeddingSize: 5)
func testEmbedding() {
var layer = Embedding<Float>(vocabularySize: 3, embeddingSize: 5)
var data = Tensor<Int32>(shape: [2, 3], scalars: [0, 1, 2, 1, 2, 2])
var input = EmbeddingInput(indices: data)
var output = layer.inferring(from: input)
let expectedShape = TensorShape([2, 3, 5])
XCTAssertEqual(output.shape, expectedShape)

let pretrained = Tensor<Float>(shape:[2, 2], scalars: [0.4, 0.3, 0.2, 0.1])
layer = Embedding<Float>(embeddings: pretrained)
data = Tensor<Int32>(shape: [2, 2], scalars: [0, 1, 1, 1])
Expand Down Expand Up @@ -318,6 +318,14 @@ final class LayerTests: XCTestCase {
// XCTAssertEqual(𝛁rnn.cell.bias, [ 0.2496884, 0.66947335, 0.7978788, -0.22378457])
}

func testFunction() {
let tanhLayer = Function<Tensor<Float>, Tensor<Float>>(tanh)
let input = Tensor(shape: [5, 1], scalars: (0..<5).map(Float.init))
let output = tanhLayer.inferring(from: input)
let expected = Tensor<Float>([[0.0], [0.7615942], [0.9640276], [0.9950547], [0.9993292]])
XCTAssertEqual(output, expected)
}

static var allTests = [
("testConv1D", testConv1D),
("testConv1DDilation", testConv1DDilation),
Expand All @@ -344,6 +352,7 @@ final class LayerTests: XCTestCase {
("testFlatten", testFlatten),
("testEmbedding", testEmbedding),
("testSimpleRNNCell", testSimpleRNNCell),
("testRNN", testRNN)
("testRNN", testRNN),
("testFunction", testFunction)
]
}