@@ -42,46 +42,64 @@ public extension Tensor where Scalar == Int32 {
4242 /// - shape: The dimensions of the tensor.
4343 ///
4444 init ( randomStandardUniform shape: TensorShape ) {
45- let dist = UniformIntegerDistribution < Scalar > ( )
46- var scalars : [ Scalar ] = [ ]
47- for _ in 0 ..< shape. contiguousSize {
48- scalars. append ( dist. next ( using: & PhiloxRandomNumberGenerator. global) )
49- }
50- self . init ( shape: shape, scalars: scalars)
45+ self . init ( randomStandardUniform: shape, generator: & PhiloxRandomNumberGenerator. global)
5146 }
5247}
5348
54- public extension Tensor where Scalar: BinaryFloatingPoint ,
55- Scalar. RawSignificand: FixedWidthInteger {
49+ public extension Tensor where Scalar: BinaryFloatingPoint {
5650 /// Creates a tensor with the specified shape, randomly sampling scalar values
57- /// from a uniform distribution between 0 and 1.
51+ /// from a uniform distribution between 0 and 1, using the default random
52+ /// number generator.
5853 ///
5954 /// - Parameters:
6055 /// - shape: The dimensions of the tensor.
61- /// - generator: Random number generator to use.
56+ /// - seed: The seed value.
57+ ///
58+ init (
59+ randomUniform shape: TensorShape ,
60+ seed: ( Int64 , Int64 ) = ( Int64 . random ( in: Int64 . min..< Int64 . max) ,
61+ Int64 . random ( in: Int64 . min..< Int64 . max) )
62+ ) {
63+ self = Raw . statelessRandomUniform (
64+ shape: Tensor < Int32 > ( ( 0 ..< shape. rank) . map { shape [ $0] } ) ,
65+ seed: Tensor < Int64 > ( [ seed. 0 , seed. 1 ] )
66+ )
67+ }
68+
69+ /// Creates a tensor with the specified shape, randomly sampling scalar values
70+ /// from a normal distribution, using the default random number generator.
6271 ///
63- init < G: RandomNumberGenerator > ( randomUniform shape: TensorShape ,
64- generator: inout G ) {
65- let dist = UniformFloatingPointDistribution < Scalar > ( )
66- var scalars : [ Scalar ] = [ ]
67- for _ in 0 ..< shape. contiguousSize {
68- scalars. append ( dist. next ( using: & generator) )
69- }
70- self . init ( shape: shape, scalars: scalars)
72+ /// - Parameters:
73+ /// - shape: The dimensions of the tensor.
74+ /// - seed: The seed value.
75+ ///
76+ init (
77+ randomNormal shape: TensorShape ,
78+ seed: ( Int64 , Int64 ) = ( Int64 . random ( in: Int64 . min..< Int64 . max) ,
79+ Int64 . random ( in: Int64 . min..< Int64 . max) )
80+ ) {
81+ self = Raw . statelessRandomNormal (
82+ shape: Tensor < Int32 > ( ( 0 ..< shape. rank) . map { shape [ $0] } ) ,
83+ seed: Tensor < Int64 > ( [ seed. 0 , seed. 1 ] )
84+ )
7185 }
86+ }
7287
88+ public extension Tensor where Scalar: BinaryFloatingPoint ,
89+ Scalar. RawSignificand: FixedWidthInteger {
7390 /// Creates a tensor with the specified shape, randomly sampling scalar values
74- /// from a uniform distribution between 0 and 1, using the default random
75- /// number generator.
91+ /// from a uniform distribution between 0 and 1.
7692 ///
7793 /// - Parameters:
7894 /// - shape: The dimensions of the tensor.
95+ /// - generator: Random number generator to use.
7996 ///
80- init ( randomUniform shape: TensorShape ) {
97+ init < G: RandomNumberGenerator > ( randomUniform shape: TensorShape ,
98+ generator: inout G ) {
8199 let dist = UniformFloatingPointDistribution < Scalar > ( )
82100 var scalars : [ Scalar ] = [ ]
83101 for _ in 0 ..< shape. contiguousSize {
84- scalars. append ( dist. next ( using: & PhiloxRandomNumberGenerator . global ) )
102+ scalars. append ( dist. next ( using: & generator ) )
85103 }
86104 self . init ( shape: shape, scalars: scalars)
87105 }
@@ -106,22 +124,35 @@ public extension Tensor where Scalar: BinaryFloatingPoint,
106124 }
107125 self . init ( shape: shape, scalars: scalars)
108126 }
127+ }
109128
110- /// Creates a tensor with the specified shape, randomly sampling scalar values
111- /// from a normal distribution, using the default random number generator.
129+ public extension Tensor where Scalar: TensorFlowFloatingPoint {
130+ private static func glorot(
131+ fromStandardUniform randomUniform: __shared Tensor< Scalar > ,
132+ shape: __shared TensorShape
133+ ) -> Tensor < Scalar > {
134+ let spatialDimCount = shape. count - 2
135+ let receptiveField = shape [ 0 ..< spatialDimCount] . contiguousSize
136+ let fanIn = shape [ shape. count - 2 ] * receptiveField
137+ let fanOut = shape [ shape. count - 1 ] * receptiveField
138+ let minusOneToOne = 2 * randomUniform - 1
139+ return sqrt ( Scalar ( 6 ) / Scalar( fanIn + fanOut) ) * minusOneToOne
140+ }
141+
142+ /// Creates a tensor by performing Glorot uniform initialization for the specified shape,
143+ /// randomly sampling scalar values from a uniform distribution between `-limit` and `limit`,
144+ /// generated by the default random number generator, where limit is
145+ /// `sqrt(6 / (fanIn + fanOut))` and `fanIn`/`fanOut` represent the number of input and output
146+ /// features multiplied by the receptive field if present.
112147 ///
113148 /// - Parameters:
114149 /// - shape: The dimensions of the tensor.
115- /// - mean: The mean of the distribution.
116- /// - stddev: The standard deviation of the distribution.
117150 ///
118- init ( randomNormal shape: TensorShape , mean: Scalar = 0 , stddev: Scalar = 1 ) {
119- let dist = NormalDistribution < Scalar > ( mean: mean, standardDeviation: stddev)
120- var scalars : [ Scalar ] = [ ]
121- for _ in 0 ..< shape. contiguousSize {
122- scalars. append ( dist. next ( using: & PhiloxRandomNumberGenerator. global) )
123- }
124- self . init ( shape: shape, scalars: scalars)
151+ init ( glorotUniform shape: TensorShape ,
152+ seed: ( Int64 , Int64 ) = ( Int64 . random ( in: Int64 . min..< Int64 . max) ,
153+ Int64 . random ( in: Int64 . min..< Int64 . max) ) ) {
154+ let uniform = Tensor ( randomUniform: shape, seed: seed)
155+ self = Tensor . glorot ( fromStandardUniform: uniform, shape: shape)
125156 }
126157}
127158
@@ -137,24 +168,7 @@ public extension Tensor where Scalar: TensorFlowFloatingPoint,
137168 /// - generator: Random number generator to use.
138169 ///
139170 init < G: RandomNumberGenerator > ( glorotUniform shape: TensorShape , generator: inout G ) {
140- let spatialDimCount = shape. count - 2
141- let receptiveField = shape [ 0 ..< spatialDimCount] . contiguousSize
142- let fanIn = shape [ shape. count - 2 ] * receptiveField
143- let fanOut = shape [ shape. count - 1 ] * receptiveField
144- let minusOneToOne = 2 * Tensor( randomUniform: shape, generator: & generator) - 1
145- self = sqrt ( Scalar ( 6 ) / Scalar( fanIn + fanOut) ) * minusOneToOne
146- }
147-
148- /// Creates a tensor by performing Glorot uniform initialization for the specified shape,
149- /// randomly sampling scalar values from a uniform distribution between `-limit` and `limit`,
150- /// generated by the default random number generator, where limit is
151- /// `sqrt(6 / (fanIn + fanOut))` and `fanIn`/`fanOut` represent the number of input and output
152- /// features multiplied by the receptive field if present.
153- ///
154- /// - Parameters:
155- /// - shape: The dimensions of the tensor.
156- ///
157- init ( glorotUniform shape: TensorShape ) {
158- self . init ( glorotUniform: shape, generator: & PhiloxRandomNumberGenerator. global)
171+ let uniform = Tensor ( randomUniform: shape, generator: & generator)
172+ self = Tensor . glorot ( fromStandardUniform: uniform, shape: shape)
159173 }
160174}
0 commit comments