@@ -187,7 +187,7 @@ TFOutput ShapeTensorOutput (TFShape shape)
187187 /// <param name="mean">The mean of the standard distribution.</param>
188188 /// <param name="stddev">The standard deviation of the normal distribution.</param>
189189 /// <param name="seed">Integer seed used for the random distribution, using the TensorFlow SetRandomSeed .</param>
190- /// <param name="operName">> Operation name, optional.</param>
190+ /// <param name="operName">Operation name, optional.</param>
191191 public TFOutput RandomNormal ( TFShape shape , double mean = 0 , double stddev = 1 , int ? seed = null , string operName = null )
192192 {
193193 var scopeName = MakeName ( "RandomNormal" , operName ) ;
@@ -257,5 +257,67 @@ public void GetRandomSeeds (int? operationSeed, out int graphSeed, out int local
257257 }
258258 }
259259 }
260- }
260+
261+ /// <summary>
262+ /// Computes dropout.
263+ /// </summary>
264+ /// <param name="x">A tensor.</param>
265+ /// <param name="keep_prob">A scalar Tensor with the same type as x. The probability that each element is kept.</param>
266+ /// <param name="noise_shape">A 1-D Tensor of type int32, representing the shape for randomly generated keep/drop flags.</param>
267+ /// <param name="seed">Integer seed used for the random distribution, using the TensorFlow SetRandomSeed .</param>
268+ /// <param name="operName">Operation name, optional.</param>
269+ /// <remarks>
270+ /// With probability keep_prob, outputs the input element scaled up by 1 / keep_prob,
271+ /// otherwise outputs 0. The scaling is so that the expected sum is unchanged.
272+ /// </remarks>
273+ public TFOutput Dropout ( TFOutput x , TFOutput keep_prob , TFShape noise_shape = null , int ? seed = null , string operName = null )
274+ {
275+ var scopeName = MakeName ( "dropout" , operName ) ;
276+
277+ using ( var newScope = WithScope ( scopeName ) ) {
278+ if ( noise_shape == null )
279+ noise_shape = new TFShape ( GetShape ( x ) ) ;
280+
281+ TFOutput shapeTensor = ShapeTensorOutput ( noise_shape ) ;
282+
283+ // uniform [keep_prob, 1.0 + keep_prob)
284+ TFOutput random_tensor = keep_prob ;
285+ random_tensor = Add ( random_tensor , RandomUniform ( shapeTensor , seed : seed , dtype : x . OutputType ) ) ;
286+
287+ // 0. if [keep_prob, 1.0) and 1. if [1.0, 1.0 + keep_prob)
288+ TFOutput binary_tensor = Floor ( random_tensor ) ;
289+ TFOutput ret = Mul ( Div ( x , keep_prob ) , binary_tensor ) ;
290+ SetTensorShape ( ret , GetShape ( x ) ) ;
291+ return ret ;
292+ }
293+ }
294+
295+ /// <summary>
296+ /// Computes dropout.
297+ /// </summary>
298+ /// <param name="x">A tensor.</param>
299+ /// <param name="keep_prob">A scalar Tensor with the same type as x. The probability that each element is kept.</param>
300+ /// <param name="noise_shape">A 1-D Tensor of type int32, representing the shape for randomly generated keep/drop flags.</param>
301+ /// <param name="seed">Integer seed used for the random distribution, using the TensorFlow SetRandomSeed .</param>
302+ /// <param name="operName">Operation name, optional.</param>
303+ /// <remarks>
304+ /// With probability keep_prob, outputs the input element scaled up by 1 / keep_prob,
305+ /// otherwise outputs 0. The scaling is so that the expected sum is unchanged.
306+ /// </remarks>
307+ public TFOutput Dropout ( TFOutput x , double keep_prob , TFShape noise_shape = null , int ? seed = null , string operName = null )
308+ {
309+ if ( keep_prob < 0 || keep_prob >= 1 )
310+ throw new ArgumentOutOfRangeException ( "keep_prob must be a scalar tensor or a float in the range (0, 1], got " + keep_prob ) ;
311+
312+ if ( keep_prob == 1 )
313+ return x ;
314+
315+ var scopeName = MakeName ( "dropout" , operName ) ;
316+ using ( var newScope = WithScope ( scopeName ) ) {
317+ var tkeep_prob = Const ( keep_prob ) ;
318+ return Dropout ( x , tkeep_prob , noise_shape , seed , operName ) ;
319+ }
320+ }
321+ }
322+
261323}
0 commit comments