Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/TensorFlowNET.Core/APIs/tf.math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ public static Tensor divide(Tensor a, Tensor b)
public static Tensor sqrt(Tensor a, string name = null)
=> 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
=> gen_math_ops.sub(x, ops.convert_to_tensor(y, dtype: x.dtype.as_base_dtype(), name: "y"), name);

Expand Down
19 changes: 19 additions & 0 deletions src/TensorFlowNET.Core/Gradients/math_grad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

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

namespace Tensorflow.Gradients
Expand All @@ -26,6 +27,15 @@ namespace Tensorflow.Gradients
[RegisterGradient("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")]
public static Tensor[] _AddGrad(Operation op, Tensor[] grads)
{
Expand Down Expand Up @@ -428,6 +438,15 @@ public static Tensor[] _SigmoidGrad(Operation op, Tensor[] grads)
});
}

[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")]
public static Tensor[] _SquareGrad(Operation op, Tensor[] grads)
{
Expand Down
7 changes: 7 additions & 0 deletions src/TensorFlowNET.Core/Operations/gen_math_ops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ public static Tensor sigmoid_grad(Tensor y, Tensor dy, string name = "SigmoidGra
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)
{
var _op = _op_def_lib._apply_op_helper("Sinh", name, args: new { x });
Expand Down
9 changes: 9 additions & 0 deletions src/TensorFlowNET.Core/Operations/math_ops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ public static Tensor sigmoid<T>(T x, string name = null)
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>
/// Returns (x - y)(x - y) element-wise.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/TensorFlowNET.Core/tf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,10 @@ public static Session Session(Graph graph)
{
return new Session(graph);
}

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