Skip to content

Commit 5981d2d

Browse files
committed
added tf.nn.dropout
1 parent 112247f commit 5981d2d

5 files changed

Lines changed: 77 additions & 3 deletions

File tree

‎src/TensorFlowNET.Core/APIs/tf.nn.cs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,27 @@ public static partial class tf
1010
{
1111
public static class nn
1212
{
13+
/// <summary>
14+
/// Computes dropout.
15+
/// </summary>
16+
/// <param name="x">A floating point tensor.</param>
17+
/// <param name="keep_prob">(deprecated) A deprecated alias for `(1-rate)`.</param>
18+
/// <param name="noise_shape"></param>
19+
/// <param name="seed">Used to create random seeds.</param>
20+
/// <param name="name"></param>
21+
/// <param name="rate">A scalar `Tensor` with the same type as `x`.</param>
22+
/// <returns>A Tensor of the same shape of `x`.</returns>
23+
public static Tensor dropout(Tensor x, Tensor keep_prob = null, Tensor noise_shape = null, int? seed = null, string name = null,
24+
float? rate = null)
25+
{
26+
Tensor keep = null;
27+
if (keep_prob != null)
28+
keep = 1.0f - keep_prob;
29+
var rate_tensor = keep;
30+
31+
return nn_ops.dropout_v2(x, rate: rate_tensor, noise_shape: noise_shape, seed: seed, name: name);
32+
}
33+
1334
public static (Tensor, Tensor) moments(Tensor x,
1435
int[] axes,
1536
string name = null,

‎src/TensorFlowNET.Core/Operations/nn_ops.cs‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,58 @@ public static Tensor bias_add(Tensor value,
4343
});
4444
}
4545

46+
/// <summary>
47+
/// Computes dropout.
48+
/// </summary>
49+
/// <param name="x"></param>
50+
/// <param name="rate"></param>
51+
/// <param name="noise_shape"></param>
52+
/// <param name="seed"></param>
53+
/// <param name="name"></param>
54+
/// <returns></returns>
55+
public static Tensor dropout_v2(Tensor x, Tensor rate, Tensor noise_shape = null, int? seed = null, string name = null)
56+
{
57+
return with(ops.name_scope(name, "dropout", x), scope =>
58+
{
59+
name = scope;
60+
x = ops.convert_to_tensor(x, name: "x");
61+
if (!x.dtype.is_floating())
62+
throw new NotImplementedException($"x has to be a floating point tensor since it's going to" +
63+
$" be scaled. Got a {x.dtype} tensor instead.");
64+
65+
rate = ops.convert_to_tensor(rate, dtype: x.dtype, name: "rate");
66+
// Do nothing if we know rate == 0
67+
var val = tensor_util.constant_value(rate);
68+
if (!(val is null) && val.Data<float>(0) == 0)
69+
return x;
70+
71+
noise_shape = _get_noise_shape(x, noise_shape);
72+
73+
// Sample a uniform distribution on [0.0, 1.0) and select values larger than
74+
// rate.
75+
//
76+
// NOTE: Random uniform actually can only generate 2^23 floats on [1.0, 2.0)
77+
// and subtract 1.0.
78+
var random_tensor = random_ops.random_uniform(noise_shape, seed: seed, dtype: x.dtype);
79+
var keep_prob = 1.0f - rate;
80+
var scale = 1.0f / keep_prob;
81+
// NOTE: if (1.0 + rate) - 1 is equal to rate, then we want to consider that
82+
// float to be selected, hence we use a >= comparison.
83+
var keep_mask = random_tensor >= rate;
84+
var ret = x * scale * math_ops.cast(keep_mask, x.dtype);
85+
ret.SetShape(x.GetShape());
86+
return ret;
87+
});
88+
}
89+
90+
private static Tensor _get_noise_shape(Tensor x, Tensor noise_shape)
91+
{
92+
if (noise_shape == null)
93+
return array_ops.shape(x);
94+
else
95+
return noise_shape;
96+
}
97+
4698
public static Tensor log_softmax(Tensor logits, int axis = -1, string name = null)
4799
{
48100
return _softmax(logits, gen_nn_ops.log_softmax, axis, name);

‎src/TensorFlowNET.Core/Operations/random_ops.py.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static Tensor random_uniform(Tensor shape,
7676
{
7777
name = scope;
7878
var minTensor = ops.convert_to_tensor(minval, dtype: dtype, name: "min");
79-
var maxTensor = ops.convert_to_tensor(maxval, dtype: dtype, name: "max");
79+
var maxTensor = ops.convert_to_tensor(maxval == null ? 1 : maxval, dtype: dtype, name: "max");
8080
var (seed1, seed2) = random_seed.get_seed(seed);
8181
if (dtype.is_integer())
8282
{

‎src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public partial class Tensor
2424

2525
public static Tensor operator /(Tensor x, Tensor y) => BinaryOpWrapper("truediv", x, y);
2626
public static Tensor operator /(Tensor x, float y) => BinaryOpWrapper("truediv", x, y);
27+
public static Tensor operator /(float x, Tensor y) => BinaryOpWrapper("truediv", x, y);
2728
public static Tensor operator /(Tensor x, double y) => BinaryOpWrapper("truediv", x, y);
2829

2930
public static Tensor operator %(Tensor x, Tensor y) => BinaryOpWrapper("mod", x, y);

‎test/TensorFlowNET.Examples/TextProcess/CnnTextClassification.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public Graph BuildGraph()
163163
var y = tf.placeholder(tf.int32, new TensorShape(-1), name: "y");
164164
var is_training = tf.placeholder(tf.@bool, new TensorShape(), name: "is_training");
165165
var global_step = tf.Variable(0, trainable: false);
166-
var keep_prob = tf.where(is_training, 0.5, 1.0);
166+
var keep_prob = tf.where(is_training, 0.5f, 1.0f);
167167
Tensor x_emb = null;
168168

169169
with(tf.name_scope("embedding"), scope =>
@@ -200,7 +200,7 @@ public Graph BuildGraph()
200200

201201
with(tf.name_scope("dropout"), delegate
202202
{
203-
// var h_drop = tf.nn.dropout(h_pool_flat, self.keep_prob);
203+
var h_drop = tf.nn.dropout(h_pool_flat, keep_prob);
204204
});
205205

206206
return graph;

0 commit comments

Comments
 (0)