From ff8f72a38e4bcfb6df7b5e44eb61ac6a2ad4ce28 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sun, 16 Dec 2018 20:29:55 -0600 Subject: [PATCH] added OpDef for Operation --- src/TensorFlowNET.Core/Graph.cs | 7 +++++-- src/TensorFlowNET.Core/OpDefLibrary.cs | 4 ++-- src/TensorFlowNET.Core/Operation.cs | 22 +++++++++++++++++++++- src/TensorFlowNET.Core/gen_array_ops.cs | 8 +++++++- src/TensorFlowNET.Core/ops.cs | 12 ++++++++++++ 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/TensorFlowNET.Core/Graph.cs b/src/TensorFlowNET.Core/Graph.cs index 013ce8a2..8ed9ceec 100644 --- a/src/TensorFlowNET.Core/Graph.cs +++ b/src/TensorFlowNET.Core/Graph.cs @@ -41,10 +41,13 @@ namespace TensorFlowNET.Core name = op_type; } - name = unique_name(name); + name = name.EndsWith("/") ? ops._name_from_scope_name(name) : unique_name(name); var node_def = ops._NodeDef(op_type, name, device: "", attrs: attrs); - var op = new Operation(node_def, this, inputs, dtypes); + var op = new Operation(node_def, this, + inputs: inputs, + output_types: dtypes, + op_def: op_def); return op; } diff --git a/src/TensorFlowNET.Core/OpDefLibrary.cs b/src/TensorFlowNET.Core/OpDefLibrary.cs index 192d6ef1..bcca3284 100644 --- a/src/TensorFlowNET.Core/OpDefLibrary.cs +++ b/src/TensorFlowNET.Core/OpDefLibrary.cs @@ -86,8 +86,8 @@ namespace TensorFlowNET.Core var op = g.create_op(op_type_name, null, output_types.ToArray(), name: "Placeholder_1/", input_types: new DataType[] { }, - attrs: null, - op_def: null); + attrs: attr_protos, + op_def: op_def); return op; } diff --git a/src/TensorFlowNET.Core/Operation.cs b/src/TensorFlowNET.Core/Operation.cs index 74e5de1a..9b7d9ff2 100644 --- a/src/TensorFlowNET.Core/Operation.cs +++ b/src/TensorFlowNET.Core/Operation.cs @@ -15,8 +15,9 @@ namespace TensorFlowNET.Core public string name; private Tensor[] _outputs; public Tensor[] outputs => _outputs; + public Tensor[] inputs; - public Operation(NodeDef node_def, Graph g, object inputs = null, TF_DataType[] output_types = null, object control_inputs = null, TF_DataType[] input_types = null, string original_op = "", string op_def = "") + public Operation(NodeDef node_def, Graph g, object inputs = null, TF_DataType[] output_types = null, object control_inputs = null, TF_DataType[] input_types = null, string original_op = "", OpDef op_def = null) { _graph = g; @@ -32,5 +33,24 @@ namespace TensorFlowNET.Core _graph._add_op(this); } + + public object get_attr(string name) + { + object ret = null; + + var fields = new string[] { "s", "i", "f", "b", "type", "shape", "tensor", "func" }; + + switch (name) + { + case "dtype": + ret = _outputs[0]; + break; + case "shape": + ret = new TensorShapeProto(); + break; + } + + return ret; + } } } diff --git a/src/TensorFlowNET.Core/gen_array_ops.cs b/src/TensorFlowNET.Core/gen_array_ops.cs index abd34659..bf18f7b1 100644 --- a/src/TensorFlowNET.Core/gen_array_ops.cs +++ b/src/TensorFlowNET.Core/gen_array_ops.cs @@ -12,7 +12,13 @@ namespace TensorFlowNET.Core public static Tensor placeholder(DataType dtype, TensorShape shape = null) { - var op = _op_def_lib._apply_op_helper("Placeholder", dtype: dtype, shape: shape); + var _op = _op_def_lib._apply_op_helper("Placeholder", dtype: dtype, shape: shape); + var _result = _op.outputs; + var _inputs_flat = _op.inputs; + var _attrs = new Dictionary(); + + _attrs["dtype"] = _op.get_attr("dtype"); + _attrs["shape"] = _op.get_attr("shape"); return null; } diff --git a/src/TensorFlowNET.Core/ops.cs b/src/TensorFlowNET.Core/ops.cs index d5e5dccf..bbe51251 100644 --- a/src/TensorFlowNET.Core/ops.cs +++ b/src/TensorFlowNET.Core/ops.cs @@ -49,6 +49,18 @@ namespace TensorFlowNET.Core return node_def; } + public static string _name_from_scope_name(string name) + { + if (name.EndsWith("/")) + { + return name.Substring(0, name.Length - 1); + } + else + { + return name; + } + } + public static int uid() { return 1;