forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cs
More file actions
92 lines (80 loc) · 2.89 KB
/
Graph.cs
File metadata and controls
92 lines (80 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using TF_DataType = Tensorflow.DataType;
namespace Tensorflow
{
/// <summary>
/// TensorFlow uses a dataflow graph to represent your computation in terms of the dependencies between individual operations.
/// This leads to a low-level programming model in which you first define the dataflow graph,
/// then create a TensorFlow session to run parts of the graph across a set of local and remote devices.
/// https://www.tensorflow.org/guide/graphs
/// </summary>
public class Graph
{
private IntPtr _c_graph;
public IntPtr Handle => _c_graph;
private Dictionary<int, Operation> _nodes_by_id;
private Dictionary<string, Operation> _nodes_by_name;
private Dictionary<string, int> _names_in_use;
public int _version;
private int _next_id_counter;
public Graph(IntPtr graph)
{
this._c_graph = graph;
_nodes_by_id = new Dictionary<int, Operation>();
_nodes_by_name = new Dictionary<string, Operation>();
_names_in_use = new Dictionary<string, int>();
}
public unsafe Operation create_op(string op_type, List<Tensor> inputs, TF_DataType[] dtypes,
TF_DataType[] input_types = null, string name = "",
Dictionary<string, AttrValue> attrs = null, OpDef op_def = null)
{
if (String.IsNullOrEmpty(name))
{
name = op_type;
}
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: inputs,
output_types: dtypes,
control_inputs: new object[] { },
input_types: input_types,
original_op: null,
op_def: op_def);
return op;
}
public void _add_op(Operation op)
{
_nodes_by_id[op._id] = op;
//_nodes_by_name[op.name] = op;
_version = Math.Max(_version, op._id);
}
public int _next_id()
{
return ++_next_id_counter;
}
public string unique_name(string name)
{
var name_key = name.ToLower();
if (_names_in_use.ContainsKey(name_key))
{
_names_in_use[name_key]++;
}
else
{
_names_in_use[name_key] = 1;
return name;
}
return $"{name}_{_names_in_use[name_key]}";
}
public Operation[] get_operations()
{
return _nodes_by_name.Values.Select(x => x).ToArray();
}
}
}