Skip to content

Commit 7a4f4fe

Browse files
committed
added math_grad._ExpGrad(oper, out_grads) for SciSharp#242
1 parent 72e96b9 commit 7a4f4fe

4 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎src/TensorFlowNET.Core/Gradients/math_grad.cs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ public static Tensor[] _AddGrad(Operation op, Tensor[] grads)
3232
return new Tensor[] { r1, r2 };
3333
}
3434

35+
/// <summary>
36+
/// Returns grad * exp(x).
37+
/// </summary>
38+
/// <param name="op"></param>
39+
/// <param name="grads"></param>
40+
/// <returns></returns>
41+
public static Tensor[] _ExpGrad(Operation op, Tensor[] grads)
42+
{
43+
var grad = grads[0];
44+
var y = op.outputs[0]; // y = e^x
45+
return with(ops.control_dependencies(new Operation[] { grad }), dp => {
46+
y = math_ops.conj(y);
47+
return new Tensor[] { math_ops.mul_no_nan(y, grad) };
48+
});
49+
}
50+
3551
public static Tensor[] _IdGrad(Operation op, Tensor[] grads)
3652
{
3753
return new Tensor[] { grads[0] };

‎src/TensorFlowNET.Core/Gradients/ops.gradient_function_mapping.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public static Func<Operation, Tensor[], Tensor[]> get_gradient_function(Operatio
2222
return math_grad._AddGrad(oper, out_grads);
2323
case "BiasAdd":
2424
return nn_grad._BiasAddGrad(oper, out_grads);
25+
case "Exp":
26+
return math_grad._ExpGrad(oper, out_grads);
2527
case "Identity":
2628
return math_grad._IdGrad(oper, out_grads);
2729
case "Log":

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,13 @@ public static Tensor mul<Tx, Ty>(Tx x, Ty y, string name = null)
352352
return _op.outputs[0];
353353
}
354354

355+
public static Tensor mul_no_nan<Tx, Ty>(Tx x, Ty y, string name = null)
356+
{
357+
var _op = _op_def_lib._apply_op_helper("MulNoNan", name, args: new { x, y });
358+
359+
return _op.outputs[0];
360+
}
361+
355362
public static Tensor real_div(Tensor x, Tensor y, string name = null)
356363
{
357364
var _op = _op_def_lib._apply_op_helper("RealDiv", name, args: new { x, y });

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public static Tensor equal<Tx, Ty>(Tx x, Ty y, string name = null)
7171
public static Tensor multiply(Tensor x, Tensor y, string name = null)
7272
=> gen_math_ops.mul(x, y, name: name);
7373

74+
public static Tensor mul_no_nan(Tensor x, Tensor y, string name = null)
75+
=> gen_math_ops.mul_no_nan(x, y, name: name);
76+
7477
/// <summary>
7578
/// Computes the mean of elements across dimensions of a tensor.
7679
/// Reduces `input_tensor` along the dimensions given in `axis`.

0 commit comments

Comments
 (0)