Skip to content

Commit 71dd19d

Browse files
committed
add grad for LeakyRelu.
1 parent a5bb191 commit 71dd19d

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/TensorFlowNET.Core/APIs/tf.queue.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public PaddingFIFOQueue PaddingFIFOQueue(int capacity,
5252
=> new PaddingFIFOQueue(capacity,
5353
new [] { dtype },
5454
new[] { shape },
55-
new[] { name },
5655
shared_name: shared_name,
5756
name: name);
5857

src/TensorFlowNET.Core/Gradients/nn_grad.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ public static Tensor[] _ReluGrad(Operation op, Tensor[] grads)
4747
return new Tensor[] { gen_nn_ops.relu_grad(grads[0], op.outputs[0]) };
4848
}
4949

50+
[RegisterGradient("LeakyRelu")]
51+
public static Tensor[] _LeakyReluGrad(Operation op, Tensor[] grads)
52+
{
53+
var grad = grads[0];
54+
var x = op.inputs[0];
55+
var alpha = (float)op.get_attr("alpha");
56+
return new Tensor[] { gen_nn_ops.leaky_relu_grad(grad, x, alpha: alpha)};
57+
}
58+
5059
/// <summary>
5160
/// The derivative of the softmax nonlinearity.
5261
/// </summary>

src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,18 @@ public static Tensor relu_grad(Tensor gradients, Tensor features, string name =
284284
});
285285

286286
return _op.outputs[0];
287+
}
288+
289+
public static Tensor leaky_relu_grad(Tensor gradients, Tensor features, float alpha = 0.2f, string name = null)
290+
{
291+
var _op = _op_def_lib._apply_op_helper("LeakyReluGrad", name: name, args: new
292+
{
293+
gradients,
294+
features,
295+
alpha
296+
});
297+
298+
return _op.output;
287299
}
288300

289301
public static Tensor softmax(Tensor logits, string name = null)

0 commit comments

Comments
 (0)