@@ -361,6 +361,130 @@ public extension Conv3D {
361
361
}
362
362
}
363
363
364
+ /// A 1-D transposed convolution layer (e.g. temporal transposed convolution over images).
365
+ ///
366
+ /// This layer creates a convolution filter that is transpose-convolved with the layer input
367
+ /// to produce a tensor of outputs.
368
+ @_fixed_layout
369
+ public struct TransposedConv1D : Layer {
370
+ /// The 1-D convolution kernel.
371
+ public var filter : Tensor < Float >
372
+ /// The bias vector.
373
+ public var bias : Tensor < Float >
374
+ /// An activation function.
375
+ public typealias Activation = @differentiable ( Tensor < Float > ) -> Tensor < Float >
376
+ /// The element-wise activation function.
377
+ @noDerivative public let activation : Activation
378
+ /// The strides of the sliding window for spatial dimensions.
379
+ @noDerivative public let strides : Int
380
+ /// The padding algorithm for convolution.
381
+ @noDerivative public let padding : Padding
382
+ @noDerivative public let paddingIndex : Int
383
+
384
+ /// Creates a `TransposedConv1D` layer with the specified filter, bias,
385
+ /// activation function, strides, and padding.
386
+ ///
387
+ /// - Parameters:
388
+ /// - filter: The 3-D convolution kernel.
389
+ /// - bias: The bias vector.
390
+ /// - activation: The element-wise activation function.
391
+ /// - strides: The strides of the sliding window for spatial dimensions.
392
+ /// - padding: The padding algorithm for convolution.
393
+ public init (
394
+ filter: Tensor < Float > ,
395
+ bias: Tensor < Float > ,
396
+ activation: @escaping Activation ,
397
+ strides: Int ,
398
+ padding: Padding
399
+ ) {
400
+ self . filter = filter
401
+ self . bias = bias
402
+ self . activation = activation
403
+ self . strides = strides
404
+ self . padding = padding
405
+ self . paddingIndex = padding == . same ? 0 : 1
406
+ }
407
+
408
+ /// Returns the output obtained from applying the layer to the given input.
409
+ ///
410
+ /// - Parameter input: The input to the layer.
411
+ /// - Returns: The output.
412
+ @differentiable
413
+ public func callAsFunction( _ input: Tensor < Float > ) -> Tensor < Float > {
414
+ let batchSize = input. shape [ 0 ]
415
+ let w = ( input. shape [ 1 ] - ( 1 * paddingIndex) ) *
416
+ strides. 0 + ( filter. shape [ 0 ] * paddingIndex)
417
+ let c = filter. shape [ 2 ]
418
+ let newShape = Tensor < Int32 > ( [ Int32 ( batchSize) , Int32 ( w) , Int32 ( c) , 1 ] )
419
+ return activation ( conv2DBackpropInput ( input. expandingShape ( at: 1 ) , shape: newShape,
420
+ filter: filter. expandingShape ( at: 0 ) ,
421
+ strides: ( 1 , 1 , strides, 1 ) ,
422
+ padding: padding) + bias)
423
+ }
424
+ }
425
+
426
+ public extension TransposedConv1D {
427
+ /// Creates a `TransposedConv1D` layer with the specified filter shape, strides, padding, and
428
+ /// element-wise activation function. The filter tensor is initialized using Glorot uniform
429
+ /// initialization with the specified generator. The bias vector is initialized with zeros.
430
+ ///
431
+ /// - Parameters:
432
+ /// - filterShape: The shape of the 3-D convolution kernel.
433
+ /// - strides: The strides of the sliding window for spatial dimensions.
434
+ /// - padding: The padding algorithm for convolution.
435
+ /// - activation: The element-wise activation function.
436
+ /// - generator: The random number generator for initialization.
437
+ ///
438
+ /// - Note: Use `init(filterShape:strides:padding:activation:seed:)` for faster random
439
+ /// initialization.
440
+ init < G: RandomNumberGenerator > (
441
+ filterShape: ( Int , Int , Int ) ,
442
+ strides: Int = 1 ,
443
+ padding: Padding = . valid,
444
+ activation: @escaping Activation = identity,
445
+ generator: inout G
446
+ ) {
447
+ let filterTensorShape = TensorShape ( [
448
+ filterShape. 0 , filterShape. 1 , filterShape. 2 ] )
449
+ self . init (
450
+ filter: Tensor ( glorotUniform: filterTensorShape, generator: & generator) ,
451
+ bias: Tensor ( zeros: TensorShape ( [ filterShape. 2 ] ) ) ,
452
+ activation: activation,
453
+ strides: strides,
454
+ padding: padding)
455
+ }
456
+ }
457
+
458
+ public extension TransposedConv1D {
459
+ /// Creates a `TransposedConv1D` layer with the specified filter shape, strides, padding, and
460
+ /// element-wise activation function. The filter tensor is initialized using Glorot uniform
461
+ /// initialization with the specified seed. The bias vector is initialized with zeros.
462
+ ///
463
+ /// - Parameters:
464
+ /// - filterShape: The shape of the 3-D convolution kernel.
465
+ /// - strides: The strides of the sliding window for spatial dimensions.
466
+ /// - padding: The padding algorithm for convolution.
467
+ /// - activation: The element-wise activation function.
468
+ /// - seed: The random seed for initialization. The default value is random.
469
+ init (
470
+ filterShape: ( Int , Int , Int ) ,
471
+ strides: Int = 1 ,
472
+ padding: Padding = . valid,
473
+ activation: @escaping Activation = identity,
474
+ seed: ( Int64 , Int64 ) = ( Int64 . random ( in: Int64 . min..< Int64 . max) ,
475
+ Int64 . random ( in: Int64 . min..< Int64 . max) )
476
+ ) {
477
+ let filterTensorShape = TensorShape ( [
478
+ filterShape. 0 , filterShape. 1 , filterShape. 2 ] )
479
+ self . init (
480
+ filter: Tensor ( glorotUniform: filterTensorShape, seed: seed) ,
481
+ bias: Tensor ( zeros: TensorShape ( [ filterShape. 2 ] ) ) ,
482
+ activation: activation,
483
+ strides: strides,
484
+ padding: padding)
485
+ }
486
+ }
487
+
364
488
/// A 2-D transposed convolution layer (e.g. spatial transposed convolution over images).
365
489
///
366
490
/// This layer creates a convolution filter that is transpose-convolved with the layer input
0 commit comments