Browse Source

Merge pull request #1 from SciSharp/master

Pulling from SciSharp
tags/v0.9
Matthew Moloney GitHub 6 years ago
parent
commit
b5c35783d8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 113 additions and 25 deletions
  1. +27
    -0
      src/TensorFlowNET.Core/APIs/tf.math.cs
  2. +11
    -7
      src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs
  3. +1
    -1
      src/TensorFlowNET.Core/Operations/Operation.Control.cs
  4. +5
    -4
      src/TensorFlowNET.Core/Operations/Operation.cs
  5. +18
    -1
      src/TensorFlowNET.Core/Operations/check_ops.cs
  6. +6
    -4
      src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs
  7. +28
    -0
      src/TensorFlowNET.Core/Operations/gen_math_ops.cs
  8. +2
    -0
      src/TensorFlowNET.Core/Tensors/dtypes.cs
  9. +11
    -4
      src/TensorFlowNET.Core/Tensors/tensor_util.cs
  10. +1
    -1
      src/TensorFlowNET.Core/Variables/RefVariable.cs
  11. +2
    -2
      src/TensorFlowNET.Core/ops.py.cs
  12. +1
    -1
      test/TensorFlowNET.Examples/KMeansClustering.cs

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

@@ -54,6 +54,24 @@ namespace Tensorflow
public static Tensor ceil(Tensor x, string name = null) public static Tensor ceil(Tensor x, string name = null)
=> gen_math_ops.ceil(x, name); => gen_math_ops.ceil(x, name);


/// <summary>
/// Computes sin of x element-wise.
/// </summary>
/// <param name="x"></param>
/// <param name="name"></param>
/// <returns></returns>
public static Tensor sin(Tensor x, string name = null)
=> gen_math_ops.sin(x, name);

/// <summary>
/// Computes hyperbolic sine of x element-wise.
/// </summary>
/// <param name="x"></param>
/// <param name="name"></param>
/// <returns></returns>
public static Tensor sinh(Tensor x, string name = null)
=> gen_math_ops.sinh(x, name);

/// <summary> /// <summary>
/// Computes cos of x element-wise. /// Computes cos of x element-wise.
/// </summary> /// </summary>
@@ -72,6 +90,12 @@ namespace Tensorflow
public static Tensor cosh(Tensor x, string name = null) public static Tensor cosh(Tensor x, string name = null)
=> gen_math_ops.cosh(x, name); => gen_math_ops.cosh(x, name);


public static Tensor tan(Tensor x, string name = null)
=> gen_math_ops.tan(x, name);

public static Tensor tanh(Tensor x, string name = null)
=> gen_math_ops.tanh(x, name);

/// <summary> /// <summary>
/// Returns element-wise largest integer not greater than x. /// Returns element-wise largest integer not greater than x.
/// </summary> /// </summary>
@@ -257,6 +281,9 @@ namespace Tensorflow
return math_ops.reduce_sum(input, axis); return math_ops.reduce_sum(input, axis);
} }


public static Tensor sum(Tensor input, int axis, bool keep_dims = false, string name = null)
=> gen_math_ops._sum(input, axis, keep_dims: keep_dims, name: name);

public static Tensor reduce_mean(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null, int? reduction_indices = null) public static Tensor reduce_mean(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null, int? reduction_indices = null)
=> math_ops.reduce_mean(input_tensor, axis: axis, keepdims: keepdims, name: name, reduction_indices: reduction_indices); => math_ops.reduce_mean(input_tensor, axis: axis, keepdims: keepdims, name: name, reduction_indices: reduction_indices);




+ 11
- 7
src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs View File

@@ -52,13 +52,6 @@ namespace Tensorflow.Clustering
_num_data = math_ops.add_n(_inputs.Select(i => array_ops.shape(i)[0]).ToArray()); _num_data = math_ops.add_n(_inputs.Select(i => array_ops.shape(i)[0]).ToArray());
} }


public Tensor[] op()
{
return control_flow_ops.cond(gen_math_ops.equal(_num_remaining, 0),
() => new Operation[] { check_ops.assert_equal(_cluster_centers_initialized, true) },
_initialize);
}

private Operation[] _initialize() private Operation[] _initialize()
{ {
with(ops.control_dependencies(new Operation[] with(ops.control_dependencies(new Operation[]
@@ -72,6 +65,17 @@ namespace Tensorflow.Clustering
throw new NotImplementedException("_InitializeClustersOpFactory _initialize"); throw new NotImplementedException("_InitializeClustersOpFactory _initialize");
} }


public Tensor[] op()
{
return control_flow_ops.cond(gen_math_ops.equal(_num_remaining, 0),
() =>
{
var op = check_ops.assert_equal(_cluster_centers_initialized, true);
return new Operation[] { op };
},
_initialize);
}

/*private int _add_new_centers() /*private int _add_new_centers()
{ {
var new_centers = _choose_initial_centers(); var new_centers = _choose_initial_centers();


+ 1
- 1
src/TensorFlowNET.Core/Operations/Operation.Control.cs View File

@@ -25,7 +25,7 @@ namespace Tensorflow


public void _add_control_input(Operation op) public void _add_control_input(Operation op)
{ {
c_api.TF_AddControlInput(_handle, op);
c_api.TF_AddControlInput(_operDesc, op);
} }


public void _add_control_inputs(Operation[] ops) public void _add_control_inputs(Operation[] ops)


+ 5
- 4
src/TensorFlowNET.Core/Operations/Operation.cs View File

@@ -11,6 +11,7 @@ namespace Tensorflow
public partial class Operation : ITensorOrOperation public partial class Operation : ITensorOrOperation
{ {
private readonly IntPtr _handle; // _c_op in python private readonly IntPtr _handle; // _c_op in python
private readonly IntPtr _operDesc;


private Graph _graph; private Graph _graph;
//[JsonIgnore] //[JsonIgnore]
@@ -58,9 +59,9 @@ namespace Tensorflow
{ {
_graph = g; _graph = g;


var desc = c_api.TF_NewOperation(g, opType, oper_name);
c_api.TF_SetAttrType(desc, "dtype", TF_DataType.TF_INT32);
c_api.TF_FinishOperation(desc, status);
_operDesc = c_api.TF_NewOperation(g, opType, oper_name);
c_api.TF_SetAttrType(_operDesc, "dtype", TF_DataType.TF_INT32);
_handle = c_api.TF_FinishOperation(_operDesc, status);
} }


/// <summary> /// <summary>
@@ -112,7 +113,7 @@ namespace Tensorflow
op_def = g.GetOpDef(node_def.Op); op_def = g.GetOpDef(node_def.Op);


var grouped_inputs = _reconstruct_sequence_inputs(op_def, inputs, node_def.Attr); var grouped_inputs = _reconstruct_sequence_inputs(op_def, inputs, node_def.Attr);
_handle = ops._create_c_op(g, node_def, grouped_inputs, control_input_ops.ToArray());
(_handle, _operDesc) = ops._create_c_op(g, node_def, grouped_inputs, control_input_ops.ToArray());


// Initialize self._outputs. // Initialize self._outputs.
output_types = new TF_DataType[NumOutputs]; output_types = new TF_DataType[NumOutputs];


+ 18
- 1
src/TensorFlowNET.Core/Operations/check_ops.cs View File

@@ -12,12 +12,29 @@ namespace Tensorflow
/// <param name="t1"></param> /// <param name="t1"></param>
/// <param name="t2"></param> /// <param name="t2"></param>
/// <param name="name"></param> /// <param name="name"></param>
public static Operation assert_equal(object t1, object t2, object[] data = null, string name = null)
public static Operation assert_equal(object t1, object t2, object[] data = null, string message = null, string name = null)
{ {
if (message == null)
message = "";

return with(ops.name_scope(name, "assert_equal", new { t1, t2, data }), delegate return with(ops.name_scope(name, "assert_equal", new { t1, t2, data }), delegate
{ {
var x = ops.convert_to_tensor(t1, name: "x"); var x = ops.convert_to_tensor(t1, name: "x");
var y = ops.convert_to_tensor(t2, name: "y"); var y = ops.convert_to_tensor(t2, name: "y");

if (data == null)
{
data = new object[]
{
message,
"Condition x == y did not hold element-wise:",
$"x (%s) = {x.name}",
x,
$"y (%s) = {y.name}",
y
};
}

var condition = math_ops.reduce_all(gen_math_ops.equal(x, y)); var condition = math_ops.reduce_all(gen_math_ops.equal(x, y));
var x_static = tensor_util.constant_value(x); var x_static = tensor_util.constant_value(x);
var y_static = tensor_util.constant_value(y); var y_static = tensor_util.constant_value(y);


+ 6
- 4
src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs View File

@@ -16,14 +16,16 @@ namespace Tensorflow
name = scope; name = scope;
var xs = ops.convert_n_to_tensor(data); var xs = ops.convert_n_to_tensor(data);
condition = ops.convert_to_tensor(condition, name: "Condition"); condition = ops.convert_to_tensor(condition, name: "Condition");
Func<Operation[]> true_assert = () => new Operation[]
Func<Operation[]> true_assert = () =>
{ {
gen_logging_ops._assert(condition, data, summarize, name: "Assert")
var assert = gen_logging_ops._assert(condition, data, summarize, name: "Assert");
return new Operation[] { assert };
}; };


Func<Operation[]> false_assert = () => new Operation[]
Func<Operation[]> false_assert = () =>
{ {
gen_control_flow_ops.no_op()
var op = gen_control_flow_ops.no_op();
return new Operation[] { op };
}; };


var guarded_assert = cond(condition, false_assert, true_assert, name: "AssertGuard"); var guarded_assert = cond(condition, false_assert, true_assert, name: "AssertGuard");


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

@@ -101,6 +101,20 @@ namespace Tensorflow
return _op.outputs[0]; return _op.outputs[0];
} }
public static Tensor sin(Tensor x, string name = null)
{
var _op = _op_def_lib._apply_op_helper("Sin", 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 });
return _op.outputs[0];
}
public static Tensor cos(Tensor x, string name = null) public static Tensor cos(Tensor x, string name = null)
{ {
var _op = _op_def_lib._apply_op_helper("Cos", name, args: new { x }); var _op = _op_def_lib._apply_op_helper("Cos", name, args: new { x });
@@ -115,6 +129,20 @@ namespace Tensorflow
return _op.outputs[0]; return _op.outputs[0];
} }
public static Tensor tan(Tensor x, string name = null)
{
var _op = _op_def_lib._apply_op_helper("Tan", name, args: new { x });
return _op.outputs[0];
}
public static Tensor tanh(Tensor x, string name = null)
{
var _op = _op_def_lib._apply_op_helper("Tanh", name, args: new { x });
return _op.outputs[0];
}
public static Tensor floor(Tensor x, string name = null) public static Tensor floor(Tensor x, string name = null)
{ {
var _op = _op_def_lib._apply_op_helper("Floor", name, args: new { x }); var _op = _op_def_lib._apply_op_helper("Floor", name, args: new { x });


+ 2
- 0
src/TensorFlowNET.Core/Tensors/dtypes.cs View File

@@ -10,6 +10,8 @@ namespace Tensorflow
{ {
switch (type) switch (type)
{ {
case TF_DataType.TF_BOOL:
return typeof(bool);
case TF_DataType.TF_INT32: case TF_DataType.TF_INT32:
return typeof(int); return typeof(int);
case TF_DataType.TF_INT16: case TF_DataType.TF_INT16:


+ 11
- 4
src/TensorFlowNET.Core/Tensors/tensor_util.cs View File

@@ -47,16 +47,23 @@ namespace Tensorflow
var tensor_dtype = tensor.Dtype.as_numpy_dtype(); var tensor_dtype = tensor.Dtype.as_numpy_dtype();


if (tensor.TensorContent.Length > 0) if (tensor.TensorContent.Length > 0)
return np.frombuffer(tensor.TensorContent.ToByteArray(), tensor_dtype)
.reshape(shape);
{
return np.frombuffer(tensor.TensorContent.ToByteArray(), tensor_dtype).reshape(shape);
}
else if (tensor.Dtype == DataType.DtHalf || tensor.Dtype == DataType.DtBfloat16) else if (tensor.Dtype == DataType.DtHalf || tensor.Dtype == DataType.DtBfloat16)
; ;
else if (tensor.Dtype == DataType.DtFloat) else if (tensor.Dtype == DataType.DtFloat)
; ;
else if (new DataType[] { DataType.DtInt32, DataType.DtUint8 }.Contains(tensor.Dtype)) else if (new DataType[] { DataType.DtInt32, DataType.DtUint8 }.Contains(tensor.Dtype))
{
if (tensor.IntVal.Count == 1) if (tensor.IntVal.Count == 1)
return np.repeat(np.array(tensor.IntVal[0]), Convert.ToInt32(num_elements))
.reshape(shape);
return np.repeat(np.array(tensor.IntVal[0]), num_elements).reshape(shape);
}
else if (tensor.Dtype == DataType.DtBool)
{
if (tensor.BoolVal.Count == 1)
return np.repeat(np.array(tensor.BoolVal[0]), num_elements).reshape(shape);
}


throw new NotImplementedException("MakeNdarray"); throw new NotImplementedException("MakeNdarray");
} }


+ 1
- 1
src/TensorFlowNET.Core/Variables/RefVariable.cs View File

@@ -265,7 +265,7 @@ namespace Tensorflow


public override string ToString() public override string ToString()
{ {
return $"tf.Variable '{name}' shape={shape} dtype={dtype}";
return $"tf.RefVariable '{name}' shape={shape} dtype={dtype}";
} }


public VariableDef to_proto(string export_scope) public VariableDef to_proto(string export_scope)


+ 2
- 2
src/TensorFlowNET.Core/ops.py.cs View File

@@ -122,7 +122,7 @@ namespace Tensorflow
/// </param> /// </param>
/// <param name="control_inputs">A list of `Operation`s to set as control dependencies.</param> /// <param name="control_inputs">A list of `Operation`s to set as control dependencies.</param>
/// <returns>A wrapped TF_Operation*.</returns> /// <returns>A wrapped TF_Operation*.</returns>
public static IntPtr _create_c_op<T>(Graph graph, NodeDef node_def, T[] inputs, Operation[] control_inputs)
public static (IntPtr, IntPtr) _create_c_op<T>(Graph graph, NodeDef node_def, T[] inputs, Operation[] control_inputs)
{ {
var op_desc = graph.NewOperation(node_def.Op, node_def.Name); var op_desc = graph.NewOperation(node_def.Op, node_def.Name);


@@ -164,7 +164,7 @@ namespace Tensorflow


status.Check(true); status.Check(true);


return c_op;
return (c_op, op_desc);
} }


public static OpDef _get_op_def(Graph graph, string type) public static OpDef _get_op_def(Graph graph, string type)


+ 1
- 1
test/TensorFlowNET.Examples/KMeansClustering.cs View File

@@ -16,7 +16,7 @@ namespace TensorFlowNET.Examples
public class KMeansClustering : Python, IExample public class KMeansClustering : Python, IExample
{ {
public int Priority => 8; public int Priority => 8;
public bool Enabled => false;
public bool Enabled => true;
public string Name => "K-means Clustering"; public string Name => "K-means Clustering";


Datasets mnist; Datasets mnist;


Loading…
Cancel
Save