Skip to content

Commit d8f8a7f

Browse files
committed
Saver is inprogress.
1 parent 459abbc commit d8f8a7f

19 files changed

Lines changed: 199 additions & 55 deletions

‎src/TensorFlowNET.Core/Graphs/Graph.Control.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public Queue<_ControlDependenciesController> _control_dependencies_stack
2828
/// </summary>
2929
/// <param name="input_ops">The data input ops for an op to be created.</param>
3030
/// <returns>A list of control inputs for the op to be created.</returns>
31-
private Operation[] _control_dependencies_for_inputs(Operation[] input_ops)
31+
private ITensorOrOperation[] _control_dependencies_for_inputs(ITensorOrOperation[] input_ops)
3232
{
33-
Operation[] ret = new Operation[0];
33+
var ret = new ITensorOrOperation[0];
3434

3535
foreach(var controller in _control_dependencies_stack)
3636
{
@@ -54,12 +54,12 @@ private Operation[] _control_dependencies_for_inputs(Operation[] input_ops)
5454
return ret;
5555
}
5656

57-
public _ControlDependenciesController control_dependencies(Operation[] control_inputs)
57+
public _ControlDependenciesController control_dependencies(ITensorOrOperation[] control_inputs)
5858
{
5959
if (control_inputs == null)
6060
return new _ControlDependenciesController(this, null);
6161

62-
var control_ops = new List<Operation>();
62+
var control_ops = new List<ITensorOrOperation>();
6363
foreach (var c in control_inputs)
6464
{
6565
control_ops.Add(c);

‎src/TensorFlowNET.Core/Graphs/Graph.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ public Operation[] get_operations()
298298
return _nodes_by_name.Values.Select(x => x).ToArray();
299299
}
300300

301+
public string[] get_all_collection_keys()
302+
{
303+
return _collections.Keys.Where(x => !x.StartsWith("__")).ToArray();
304+
}
305+
301306
public object get_collection(string name, string scope = "")
302307
{
303308
return _collections.ContainsKey(name) ? _collections[name] : null;

‎src/TensorFlowNET.Core/Graphs/_ControlDependenciesController.cs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ namespace Tensorflow
1111
public class _ControlDependenciesController : IPython
1212
{
1313
private Graph _graph;
14-
private List<Operation> _control_inputs_val;
15-
private List<Operation> _seen_nodes;
14+
private List<ITensorOrOperation> _control_inputs_val;
15+
private List<ITensorOrOperation> _seen_nodes;
1616
private Queue<_ControlDependenciesController> _old_stack;
1717
private bool _new_stack;
1818
private Context _old_control_flow_context;
1919

20-
public Operation[] control_inputs => _control_inputs_val.ToArray();
20+
public ITensorOrOperation[] control_inputs => _control_inputs_val.ToArray();
2121

22-
public _ControlDependenciesController(Graph graph, List<Operation> control_inputs)
22+
public _ControlDependenciesController(Graph graph, List<ITensorOrOperation> control_inputs)
2323
{
2424
_graph = graph;
2525
if (control_inputs == null)
2626
{
27-
_control_inputs_val = new List<Operation>();
27+
_control_inputs_val = new List<ITensorOrOperation>();
2828
_new_stack = true;
2929
}
3030
else
@@ -33,15 +33,15 @@ public _ControlDependenciesController(Graph graph, List<Operation> control_input
3333
_new_stack = false;
3434
}
3535

36-
_seen_nodes = new List<Operation>();
36+
_seen_nodes = new List<ITensorOrOperation>();
3737
}
3838

39-
public void add_op(Operation op)
39+
public void add_op(ITensorOrOperation op)
4040
{
4141
_seen_nodes.Add(op);
4242
}
4343

44-
public bool op_in_group(Operation op)
44+
public bool op_in_group(ITensorOrOperation op)
4545
{
4646
return _seen_nodes.Contains(op);
4747
}

‎src/TensorFlowNET.Core/ITensorOrOperation.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ namespace Tensorflow
1111
public interface ITensorOrOperation
1212
{
1313
string Device { get; }
14+
Operation op { get; }
1415
}
1516
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ public Operation _apply_op_helper(string op_type_name, string name = "", dynamic
107107

108108
values = ops.internal_convert_to_tensor(values,
109109
name: input_name,
110-
as_ref: input_arg.IsRef);
110+
dtype: dtype,
111+
as_ref: input_arg.IsRef,
112+
preferred_dtype: default_dtype);
111113

112114
//if (!String.IsNullOrEmpty(input_arg.TypeAttr))
113115
//attrs[input_arg.TypeAttr] = values.dtype;
@@ -163,14 +165,20 @@ public Operation _apply_op_helper(string op_type_name, string name = "", dynamic
163165

164166
foreach (var arg in op_def.OutputArg)
165167
{
168+
types = new List<TF_DataType>();
166169
if (!string.IsNullOrEmpty(arg.NumberAttr))
167170
{
168171

169172
}
170173
else if (!string.IsNullOrEmpty(arg.TypeAttr))
171174
{
172-
output_types.Add((TF_DataType)attr_protos[arg.TypeAttr].Type);
175+
types = new List<TF_DataType>() { (TF_DataType)attr_protos[arg.TypeAttr].Type };
173176
}
177+
178+
if (arg.IsRef)
179+
types = types.Select(x => x.as_ref()).ToList();
180+
181+
output_types.AddRange(types);
174182
}
175183

176184
// Add Op to graph

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public partial class Operation : ITensorOrOperation
1616
private int _id_value;
1717

1818
public string type => OpType;
19+
public Operation op => this;
1920

2021
private Status status = new Status();
2122

@@ -75,7 +76,7 @@ public Operation(Graph g, string opType, string oper_name)
7576
/// </param>
7677
/// <param name="original_op"></param>
7778
/// <param name="op_def"></param>
78-
public Operation(NodeDef node_def, Graph g, Tensor[] inputs = null, TF_DataType[] output_types = null, Operation[] control_inputs = null, TF_DataType[] input_types = null, string original_op = "", OpDef op_def = null)
79+
public Operation(NodeDef node_def, Graph g, Tensor[] inputs = null, TF_DataType[] output_types = null, ITensorOrOperation[] control_inputs = null, TF_DataType[] input_types = null, string original_op = "", OpDef op_def = null)
7980
{
8081
Graph = g;
8182

@@ -120,6 +121,11 @@ public Operation(NodeDef node_def, Graph g, Tensor[] inputs = null, TF_DataType[
120121
_control_flow_post_processing();
121122
}
122123

124+
public void run(FeedItem[] feed_dict = null, Session session = null)
125+
{
126+
ops._run_using_default_session(this, feed_dict, Graph, session);
127+
}
128+
123129
private object[] _reconstruct_sequence_inputs(OpDef op_def, Tensor[] inputs, MapField<string, AttrValue> attrs)
124130
{
125131
var grouped_inputs = new List<object>();
@@ -204,7 +210,7 @@ private NodeDef GetNodeDef()
204210

205211
public override string ToString()
206212
{
207-
return _handle == IntPtr.Zero ? "Undefined" : $"'{Name}' type={OpType}";
213+
return _handle == IntPtr.Zero ? "tf.Operation Undefined" : $"tf.Operation '{Name}' type={OpType}";
208214
}
209215

210216
public static implicit operator Operation(IntPtr handle) => new Operation(handle);

‎src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ public static Operation group<T>(T[] inputs, string name = "") where T : ITensor
2828
{
2929
var dev = ops_on_device.Keys.First();
3030
var deps = ops_on_device.Values.First();
31-
if (typeof(T).Name == "Operation")
32-
return _GroupControlDeps(dev, deps.Select(x => x as Operation).ToArray(), name);
33-
else
34-
throw new NotImplementedException("control_flow_ops.group");
31+
return _GroupControlDeps(dev, deps.Select(x => x.op).ToArray(), name);
3532
}
3633

3734
// 2-level tree. The root node is the returned NoOp node.

‎src/TensorFlowNET.Core/Sessions/BaseSession.cs‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,8 @@ public BaseSession(string target = "", Graph graph = null)
3535
c_api.TF_DeleteSessionOptions(opts);
3636
}
3737

38-
public virtual NDArray run(RefVariable fetches, FeedItem[] feed_dict = null)
39-
{
40-
return _run(fetches, feed_dict);
41-
}
42-
43-
public virtual NDArray run(Tensor fetches, FeedItem[] feed_dict = null)
44-
{
45-
return _run(fetches, feed_dict);
46-
}
4738

48-
public virtual NDArray run(Operation fetches, FeedItem[] feed_dict = null)
39+
public virtual NDArray run<T>(T fetches, FeedItem[] feed_dict = null)
4940
{
5041
return _run(fetches, feed_dict);
5142
}

‎src/TensorFlowNET.Core/Tensors/Tensor.Implicit.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public static implicit operator IntPtr(Tensor tensor)
3030
return tensor._handle;
3131
}
3232

33+
public static implicit operator Operation(Tensor tensor)
34+
{
35+
return tensor.op;
36+
}
37+
3338
public static implicit operator Tensor(IntPtr handle)
3439
{
3540
return new Tensor(handle);

‎src/TensorFlowNET.Core/Tensors/Tensor.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public override string ToString()
261261
}
262262
}
263263

264-
return $"tf.Tensor {name} shape=({string.Join(",", shape)}) dtype={dtype.ToString()}";
264+
return $"tf.Tensor '{name}' shape=({string.Join(",", shape)}) dtype={dtype.ToString()}";
265265
}
266266

267267
public void Dispose()

0 commit comments

Comments
 (0)