This repository was archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
Adding conv transpose 1d & 3d #174
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
45eb51b
adding conv transpose 3d
Shashi456 f1f1da1
updating to callAsFunction
Shashi456 a969648
Updating to refactored conv3dbackpropinput'
Shashi456 135cfaa
adding conv 1d transpose
Shashi456 1485f8b
Updating seed to int32 and some errors
Shashi456 a528f1b
Merge branch 'master' of https://github.com/tensorflow/swift-apis int…
Shashi456 4ebfc07
Adding tests for transposed conv
Shashi456 21042ba
Adding tests to var all tests
Shashi456 6a4d784
Merge branch 'master' of https://github.com/tensorflow/swift-apis int…
Shashi456 369ac71
removing transposed conv2d test
Shashi456 5077cbd
Merge branch 'master' into conv
Shashi456 b544250
Minor test mistake
Shashi456 2bbfe57
Merge branch 'master' into conv
Shashi456 d81e50f
Merge branch 'conv' of https://github.com/Shashi456/swift-apis into conv
Shashi456 ffbc48d
Fixing tests and layers
Shashi456 fe5fec0
Indentation and spacing errors
Shashi456 564ef33
Merge branch 'master' into conv
Shashi456 49179bd
Merge branch 'master' into conv
Shashi456 a20ae42
Update LayerTests.swift
Shashi456 2f9c271
Add property doc
Shashi456 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -350,6 +350,102 @@ public extension Conv3D { | |
} | ||
} | ||
|
||
/// A 1-D transposed convolution layer (e.g. temporal transposed convolution over images). | ||
/// | ||
/// This layer creates a convolution filter that is transpose-convolved with the layer input | ||
/// to produce a tensor of outputs. | ||
@frozen | ||
public struct TransposedConv1D<Scalar: TensorFlowFloatingPoint>: Layer { | ||
/// The 1-D convolution kernel. | ||
public var filter: Tensor<Scalar> | ||
/// The bias vector. | ||
public var bias: Tensor<Scalar> | ||
/// The element-wise activation function. | ||
@noDerivative public let activation: Activation | ||
/// The strides of the sliding window for spatial dimensions. | ||
@noDerivative public let stride: Int | ||
/// The padding algorithm for convolution. | ||
@noDerivative public let padding: Padding | ||
/// The paddingIndex property allows us to handle computation based on padding. | ||
@noDerivative public let paddingIndex: Int | ||
|
||
/// The element-wise activation function type. | ||
public typealias Activation = @differentiable (Tensor<Scalar>) -> Tensor<Scalar> | ||
|
||
/// Creates a `TransposedConv1D` layer with the specified filter, bias, | ||
/// activation function, strides, and padding. | ||
/// | ||
/// - Parameters: | ||
/// - filter: The 3-D convolution kernel. | ||
/// - bias: The bias vector. | ||
/// - activation: The element-wise activation function. | ||
/// - strides: The strides of the sliding window for spatial dimensions. | ||
/// - padding: The padding algorithm for convolution. | ||
public init( | ||
filter: Tensor<Scalar>, | ||
bias: Tensor<Scalar>, | ||
activation: @escaping Activation = identity, | ||
stride: Int = 1, | ||
padding: Padding = .valid | ||
) { | ||
self.filter = filter | ||
self.bias = bias | ||
self.activation = activation | ||
self.stride = stride | ||
self.padding = padding | ||
self.paddingIndex = padding == .same ? 0 : 1 | ||
} | ||
|
||
/// 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 callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> { | ||
let batchSize = input.shape[0] | ||
let w = (input.shape[1] - (1 * paddingIndex)) * | ||
stride + (filter.shape[0] * paddingIndex) | ||
let c = filter.shape[2] | ||
let newShape = Tensor<Int32>([Int32(batchSize), 1, Int32(w), Int32(c)]) | ||
return activation(conv2DBackpropInput( | ||
input.expandingShape(at: 1), | ||
shape: newShape, | ||
filter: filter.expandingShape(at: 0), | ||
strides: (1, 1, stride, 1), | ||
padding: padding) + bias) | ||
} | ||
} | ||
|
||
public extension TransposedConv1D { | ||
/// Creates a `TransposedConv1D` layer with the specified filter shape, strides, padding, and | ||
/// element-wise activation function. The filter tensor is initialized using Glorot uniform | ||
/// initialization with the specified generator. The bias vector is initialized with zeros. | ||
/// | ||
/// - Parameters: | ||
/// - filterShape: The shape of the 3-D convolution kernel. | ||
/// - strides: The strides of the sliding window for spatial dimensions. | ||
/// - padding: The padding algorithm for convolution. | ||
/// - activation: The element-wise activation function. | ||
/// - generator: The random number generator for initialization. | ||
init( | ||
filterShape: (Int, Int, Int), | ||
stride: Int = 1, | ||
padding: Padding = .valid, | ||
activation: @escaping Activation = identity, | ||
filterInitializer: ParameterInitializer<Scalar> = glorotUniform(), | ||
biasInitializer: ParameterInitializer<Scalar> = zeros() | ||
) { | ||
let filterTensorShape = TensorShape([ | ||
filterShape.0, filterShape.1, filterShape.2]) | ||
self.init( | ||
filter: filterInitializer(filterTensorShape), | ||
bias: biasInitializer([filterShape.2]), | ||
activation: activation, | ||
stride: stride, | ||
padding: padding) | ||
} | ||
} | ||
|
||
/// A 2-D transposed convolution layer (e.g. spatial transposed convolution over images). | ||
/// | ||
/// This layer creates a convolution filter that is transpose-convolved with the layer input | ||
|
@@ -449,6 +545,107 @@ public extension TransposedConv2D { | |
} | ||
} | ||
|
||
|
||
/// A 3-D transposed convolution layer (e.g. spatial transposed convolution over images). | ||
/// | ||
/// This layer creates a convolution filter that is transpose-convolved with the layer input | ||
/// to produce a tensor of outputs. | ||
@frozen | ||
public struct TransposedConv3D<Scalar: TensorFlowFloatingPoint>: Layer { | ||
/// The 5-D convolution kernel. | ||
public var filter: Tensor<Scalar> | ||
/// The bias vector. | ||
public var bias: Tensor<Scalar> | ||
/// The element-wise activation function. | ||
@noDerivative public let activation: Activation | ||
/// The strides of the sliding window for spatial dimensions. | ||
@noDerivative public let strides: (Int, Int, Int) | ||
/// The padding algorithm for convolution. | ||
@noDerivative public let padding: Padding | ||
/// The paddingIndex property allows us to handle computation based on padding. | ||
@noDerivative public let paddingIndex: Int | ||
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. Add a doc comment for this property. 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. @rxwei this property isn't really exposed to the users, it's a temporary property which allows us to handle computation based on what kind of padding it is. we currently support |
||
|
||
/// The element-wise activation function type. | ||
public typealias Activation = @differentiable (Tensor<Scalar>) -> Tensor<Scalar> | ||
|
||
/// Creates a `TransposedConv3D` layer with the specified filter, bias, | ||
/// activation function, strides, and padding. | ||
/// | ||
/// - Parameters: | ||
/// - filter: The 5-D convolution kernel. | ||
/// - bias: The bias vector. | ||
/// - activation: The element-wise activation function. | ||
/// - strides: The strides of the sliding window for spatial dimensions. | ||
/// - padding: The padding algorithm for convolution. | ||
public init( | ||
filter: Tensor<Scalar>, | ||
bias: Tensor<Scalar>, | ||
activation: @escaping Activation = identity, | ||
strides: (Int, Int, Int) = (1, 1, 1), | ||
padding: Padding = .valid | ||
) { | ||
self.filter = filter | ||
self.bias = bias | ||
self.activation = activation | ||
self.strides = strides | ||
self.padding = padding | ||
self.paddingIndex = padding == .same ? 0 : 1 | ||
} | ||
|
||
/// 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 callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> { | ||
let batchSize = input.shape[0] | ||
let w = (input.shape[1] - (1 * paddingIndex)) * | ||
strides.0 + (filter.shape[0] * paddingIndex) | ||
let h = (input.shape[2] - (1 * paddingIndex)) * | ||
strides.1 + (filter.shape[1] * paddingIndex) | ||
let d = (input.shape[3] - (1 * paddingIndex)) * | ||
strides.2 + (filter.shape[2] * paddingIndex) | ||
let c = filter.shape[3] | ||
let newShape = Tensor<Int32>([Int32(batchSize), Int32(w), Int32(h), Int32(d), Int32(c)]) | ||
return activation(conv3DBackpropInput( | ||
input, | ||
shape: newShape, | ||
filter: filter, | ||
strides: (1, strides.0, strides.1, strides.2, 1), | ||
padding: padding) + bias) | ||
} | ||
} | ||
|
||
public extension TransposedConv3D { | ||
/// Creates a `TransposedConv3D` layer with the specified filter shape, strides, padding, and | ||
/// element-wise activation function. The filter tensor is initialized using Glorot uniform | ||
/// initialization with the specified generator. The bias vector is initialized with zeros. | ||
/// | ||
/// - Parameters: | ||
/// - filterShape: The shape of the 5-D convolution kernel. | ||
/// - strides: The strides of the sliding window for spatial dimensions. | ||
/// - padding: The padding algorithm for convolution. | ||
/// - activation: The element-wise activation function. | ||
/// - generator: The random number generator for initialization. | ||
init( | ||
filterShape: (Int, Int, Int, Int, Int), | ||
strides: (Int, Int, Int) = (1, 1, 1), | ||
padding: Padding = .valid, | ||
activation: @escaping Activation = identity, | ||
filterInitializer: ParameterInitializer<Scalar> = glorotUniform(), | ||
biasInitializer: ParameterInitializer<Scalar> = zeros() | ||
) { | ||
let filterTensorShape = TensorShape([ | ||
filterShape.0, filterShape.1, filterShape.2, filterShape.3, filterShape.4]) | ||
self.init( | ||
filter: filterInitializer(filterTensorShape), | ||
bias: biasInitializer([filterShape.4]), | ||
activation: activation, | ||
strides: strides, | ||
padding: padding) | ||
} | ||
} | ||
|
||
/// A 2-D depthwise convolution layer. | ||
/// | ||
/// This layer creates seperable convolution filters that are convolved with the layer input to produce a | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Add a doc comment for this property.