-
Notifications
You must be signed in to change notification settings - Fork 135
Add–1d–layers #57
The head ref may contain hidden characters: "add\u20131d\u2013layers"
Add–1d–layers #57
Changes from 3 commits
82797fd
74fa216
1df460a
c8b7e73
827a185
deb771b
a76891d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -332,6 +332,125 @@ public extension Dense { | |||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| /// A 1-D convolution layer (e.g. temporal convolution over a time-series). | ||||||||
| /// | ||||||||
| /// This layer creates a convolution filter that is convolved with the layer input to produce a | ||||||||
| /// tensor of outputs. | ||||||||
| @_fixed_layout | ||||||||
| public struct Conv1D<Scalar: TensorFlowFloatingPoint>: Layer { | ||||||||
| /// The 3-D convolution kernel. | ||||||||
| public var filter: Tensor<Scalar> | ||||||||
| /// The bias vector. | ||||||||
| public var bias: Tensor<Scalar> | ||||||||
| /// An activation function. | ||||||||
| public typealias Activation = @differentiable (Tensor<Scalar>) -> Tensor<Scalar> | ||||||||
| /// The element-wise activation function. | ||||||||
| @noDerivative public let activation: Activation | ||||||||
| /// The stride of the sliding window for temporal dimension. | ||||||||
| @noDerivative public let stride: Int32 | ||||||||
| /// The padding algorithm for convolution. | ||||||||
| @noDerivative public let padding: Padding | ||||||||
|
|
||||||||
| /// Creates a `Conv1D` layer with the specified filter, bias, activation function, stride, and | ||||||||
| /// padding. | ||||||||
| /// | ||||||||
| /// - Parameters: | ||||||||
| /// - filter: The filter (width, inputChannels, outputChannels). | ||||||||
| /// - bias: The bias (dimensions: output channels). | ||||||||
| /// - activation: The activation activation. | ||||||||
|
||||||||
| /// - activation: The activation activation. | |
| /// - activation: The element-wise activation function. |
Please also make this change elsewhere, including for Conv2D.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are using identity as the default activation in Dense and Conv2D.
| activation: @escaping Activation, | |
| activation: @escaping Activation = identity, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The init method in the struct declaration doesn't have a default. Only the ones in the extensions have default values. So this is consistent, but maybe not what is desired?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, you are right!
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use `[dim0, dim1, ...]` syntax for specifying all shapes in doc comments.
| /// - filterShape: The shape of the filter (width, inputChannels, outputChannels). | |
| /// - filterShape: The 3-D shape of the filter, representing | |
| /// `[width, inputChannels, outputChannels]`. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indent by 4 from self.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| kernelSize: (1, 1, poolSize, 1), strides: (1, 1, stride, 1), padding: padding | |
| kernelSize: (1, 1, poolSize, 1), strides: (1, 1, stride, 1), padding: padding |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| kernelSize: (1, 1, poolSize, 1), strides: (1, 1, stride, 1), padding: padding | |
| kernelSize: (1, 1, poolSize, 1), strides: (1, 1, stride, 1), padding: padding |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Copyright 2019 The TensorFlow Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import XCTest | ||
| @testable import DeepLearning | ||
|
|
||
| final class LayerTests: XCTestCase { | ||
| func testConv1D() { | ||
| let filter = Tensor<Float>(ones: [3, 1, 2]) * Tensor<Float>([[[0.33333333, 1]]]) | ||
| let bias = Tensor<Float>([0, 1]) | ||
| let layer = Conv1D<Float>(filter: filter, bias: bias, activation: identity, stride: 1, padding: .valid) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fit within 100 columns. |
||
| let input = Tensor<Float>([[0, 1, 2, 3, 4], [10, 11, 12, 13, 14]]).expandingShape(at: 2) | ||
| let output = layer.inferring(from: input) | ||
| let expected = Tensor<Float>([[[1, 4], [2, 7], [3, 10]], [[11, 34], [12, 37], [13, 40]]]) | ||
| XCTAssertEqual(round(output), expected) | ||
| } | ||
|
|
||
| func testMaxPool1D() { | ||
| let layer = MaxPool1D<Float>(poolSize: 3, stride: 1, padding: .valid) | ||
| let input = Tensor<Float>([[0, 1, 2, 3, 4], [10, 11, 12, 13, 14]]).expandingShape(at: 2) | ||
| let output = layer.inferring(from: input) | ||
| let expected = Tensor<Float>([[[2], [3], [4]], [[12], [13], [14]]]) | ||
| XCTAssertEqual(round(output), expected) | ||
| } | ||
|
|
||
| func testAvgPool1D() { | ||
| let layer = AvgPool1D<Float>(poolSize: 3, stride: 1, padding: .valid) | ||
| let input = Tensor<Float>([[0, 1, 2, 3, 4], [10, 11, 12, 13, 14]]).expandingShape(at: 2) | ||
| let output = layer.inferring(from: input) | ||
| let expected = Tensor<Float>([[[1], [2], [3]], [[11], [12], [13]]]) | ||
| XCTAssertEqual(round(output), expected) | ||
| } | ||
|
|
||
| static var allTests = [ | ||
| ("testConv1D", testConv1D), ("testMaxPool1D", testMaxPool1D), ("testAvgPool1D", testAvgPool1D) | ||
| ] | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the
- Parameters:doc comment of memberwise initializers, please copy the doc comments from each individual stored property.For example:
Please also make this change elsewhere, including for
Conv2D.