Skip to content

Commit 46ec8e5

Browse files
acifonelliOceania2018
authored andcommitted
Adding new ctor for Session (SciSharp#309)
Meant to be a shortcut to directly set some basic configuration. E.g. ``` var sessionOptions = new SessionOptions(); sessionOptions.SetConfig(new ConfigProto { LogDevicePlacement = true, InterOpParallelismThreads = 4, GpuOptions = new GPUOptions { AllowGrowth = true, PerProcessGpuMemoryFraction = 0.12 } }); var session = tf.Session(sessionOptions); ``` Adding `AbsGrad` To be aligned with Tensorflow, introducing also `Sign` operation and its gradient.
1 parent 930f601 commit 46ec8e5

5 files changed

Lines changed: 43 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ public static Tensor divide(Tensor a, Tensor b)
203203
public static Tensor sqrt(Tensor a, string name = null)
204204
=> gen_math_ops.sqrt(a, name);
205205

206+
public static Tensor sign(Tensor a, string name = null)
207+
=> gen_math_ops.sign(a, name);
208+
206209
public static Tensor subtract<T>(Tensor x, T[] y, string name = null) where T : struct
207210
=> gen_math_ops.sub(x, ops.convert_to_tensor(y, dtype: x.dtype.as_base_dtype(), name: "y"), name);
208211

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
using System;
1818
using System.Linq;
19+
using Tensorflow.Operations;
1920
using static Tensorflow.Python;
2021

2122
namespace Tensorflow.Gradients
@@ -26,6 +27,15 @@ namespace Tensorflow.Gradients
2627
[RegisterGradient("math_grad")]
2728
public class math_grad
2829
{
30+
[RegisterGradient("Abs")]
31+
public static Tensor[] _AbsGrad(Operation op, Tensor[] grads)
32+
{
33+
var x = op.inputs[0];
34+
var grad = grads[0];
35+
36+
return new Tensor[] { gen_ops.mul(grad, gen_math_ops.sign(x)) };
37+
}
38+
2939
[RegisterGradient("Add")]
3040
public static Tensor[] _AddGrad(Operation op, Tensor[] grads)
3141
{
@@ -428,6 +438,15 @@ public static Tensor[] _SigmoidGrad(Operation op, Tensor[] grads)
428438
});
429439
}
430440

441+
[RegisterGradient("Sign")]
442+
public static Tensor[] _SignGrad(Operation op, Tensor[] grads)
443+
{
444+
var x = op.inputs[0];
445+
var zero = constant_op.constant(0.0f, x.dtype, x.shape);
446+
447+
return new Tensor[] {zero};
448+
}
449+
431450
[RegisterGradient("Square")]
432451
public static Tensor[] _SquareGrad(Operation op, Tensor[] grads)
433452
{

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ public static Tensor sigmoid_grad(Tensor y, Tensor dy, string name = "SigmoidGra
210210
return op.outputs[0];
211211
}
212212

213+
public static Tensor sign(Tensor x, string name = "Sign")
214+
{
215+
var op = _op_def_lib._apply_op_helper("Sign", name: name, args: new {x});
216+
217+
return op.outputs[0];
218+
}
219+
213220
public static Tensor sinh(Tensor x, string name = null)
214221
{
215222
var _op = _op_def_lib._apply_op_helper("Sinh", name, args: new { x });

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ public static Tensor sigmoid<T>(T x, string name = null)
216216
return gen_math_ops.sigmoid(x_tensor, name: name);
217217
}
218218

219+
public static Tensor sign(Tensor x, string name = null)
220+
{
221+
return with(ops.name_scope(name, "Sign", new {x}), scope =>
222+
{
223+
x = ops.convert_to_tensor(x, name: "x");
224+
return gen_math_ops.sign(x);
225+
});
226+
}
227+
219228
/// <summary>
220229
/// Returns (x - y)(x - y) element-wise.
221230
/// </summary>

‎src/TensorFlowNET.Core/tf.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,10 @@ public static Session Session(Graph graph)
7272
{
7373
return new Session(graph);
7474
}
75+
76+
public static Session Session(SessionOptions opts)
77+
{
78+
return new Session(null, opts);
79+
}
7580
}
7681
}

0 commit comments

Comments
 (0)