Browse Source

Adding new ctor for `Session` (#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.
tags/v0.12
Antonio Haiping 6 years ago
parent
commit
46ec8e561d
5 changed files with 43 additions and 0 deletions
  1. +3
    -0
      src/TensorFlowNET.Core/APIs/tf.math.cs
  2. +19
    -0
      src/TensorFlowNET.Core/Gradients/math_grad.cs
  3. +7
    -0
      src/TensorFlowNET.Core/Operations/gen_math_ops.cs
  4. +9
    -0
      src/TensorFlowNET.Core/Operations/math_ops.cs
  5. +5
    -0
      src/TensorFlowNET.Core/tf.cs

+ 3
- 0
src/TensorFlowNET.Core/APIs/tf.math.cs View File

@@ -203,6 +203,9 @@ namespace Tensorflow
public static Tensor sqrt(Tensor a, string name = null) public static Tensor sqrt(Tensor a, string name = null)
=> gen_math_ops.sqrt(a, name); => gen_math_ops.sqrt(a, name);


public static Tensor sign(Tensor a, string name = null)
=> gen_math_ops.sign(a, name);

public static Tensor subtract<T>(Tensor x, T[] y, string name = null) where T : struct public static Tensor subtract<T>(Tensor x, T[] y, string name = null) where T : struct
=> gen_math_ops.sub(x, ops.convert_to_tensor(y, dtype: x.dtype.as_base_dtype(), name: "y"), name); => gen_math_ops.sub(x, ops.convert_to_tensor(y, dtype: x.dtype.as_base_dtype(), name: "y"), name);




+ 19
- 0
src/TensorFlowNET.Core/Gradients/math_grad.cs View File

@@ -16,6 +16,7 @@


using System; using System;
using System.Linq; using System.Linq;
using Tensorflow.Operations;
using static Tensorflow.Python; using static Tensorflow.Python;


namespace Tensorflow.Gradients namespace Tensorflow.Gradients
@@ -26,6 +27,15 @@ namespace Tensorflow.Gradients
[RegisterGradient("math_grad")] [RegisterGradient("math_grad")]
public class math_grad public class math_grad
{ {
[RegisterGradient("Abs")]
public static Tensor[] _AbsGrad(Operation op, Tensor[] grads)
{
var x = op.inputs[0];
var grad = grads[0];

return new Tensor[] { gen_ops.mul(grad, gen_math_ops.sign(x)) };
}

[RegisterGradient("Add")] [RegisterGradient("Add")]
public static Tensor[] _AddGrad(Operation op, Tensor[] grads) public static Tensor[] _AddGrad(Operation op, Tensor[] grads)
{ {
@@ -428,6 +438,15 @@ namespace Tensorflow.Gradients
}); });
} }


[RegisterGradient("Sign")]
public static Tensor[] _SignGrad(Operation op, Tensor[] grads)
{
var x = op.inputs[0];
var zero = constant_op.constant(0.0f, x.dtype, x.shape);

return new Tensor[] {zero};
}

[RegisterGradient("Square")] [RegisterGradient("Square")]
public static Tensor[] _SquareGrad(Operation op, Tensor[] grads) public static Tensor[] _SquareGrad(Operation op, Tensor[] grads)
{ {


+ 7
- 0
src/TensorFlowNET.Core/Operations/gen_math_ops.cs View File

@@ -210,6 +210,13 @@ namespace Tensorflow
return op.outputs[0]; return op.outputs[0];
} }
public static Tensor sign(Tensor x, string name = "Sign")
{
var op = _op_def_lib._apply_op_helper("Sign", name: name, args: new {x});
return op.outputs[0];
}
public static Tensor sinh(Tensor x, string name = null) public static Tensor sinh(Tensor x, string name = null)
{ {
var _op = _op_def_lib._apply_op_helper("Sinh", name, args: new { x }); var _op = _op_def_lib._apply_op_helper("Sinh", name, args: new { x });


+ 9
- 0
src/TensorFlowNET.Core/Operations/math_ops.cs View File

@@ -216,6 +216,15 @@ namespace Tensorflow
return gen_math_ops.sigmoid(x_tensor, name: name); return gen_math_ops.sigmoid(x_tensor, name: name);
} }


public static Tensor sign(Tensor x, string name = null)
{
return with(ops.name_scope(name, "Sign", new {x}), scope =>
{
x = ops.convert_to_tensor(x, name: "x");
return gen_math_ops.sign(x);
});
}

/// <summary> /// <summary>
/// Returns (x - y)(x - y) element-wise. /// Returns (x - y)(x - y) element-wise.
/// </summary> /// </summary>


+ 5
- 0
src/TensorFlowNET.Core/tf.cs View File

@@ -72,5 +72,10 @@ namespace Tensorflow
{ {
return new Session(graph); return new Session(graph);
} }

public static Session Session(SessionOptions opts)
{
return new Session(null, opts);
}
} }
} }

Loading…
Cancel
Save