FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TensorFlow.NET/src/TensorFlowNET.Core/Graph.cs at master · awesomedotnetcore/TensorFlow.NET · GitHub
awesomedotnetcore
/
TensorFlow.NET
Public
forked from
SciSharp/TensorFlow.NET
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TensorFlow.NET
/
src
/
TensorFlowNET.Core
/
Graph.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
151 lines (130 loc) · 4.63 KB
Breadcrumbs
TensorFlow.NET
/
src
/
TensorFlowNET.Core
/
Graph.cs
Copy path
File metadata and controls
151 lines (130 loc) · 4.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
;
private
List
<
String
>
_unfetchable_ops
=
new
List
<
string
>
(
)
;
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
T
as_graph_element
<
T
>
(
T
obj
,
bool
allow_tensor
=
true
,
bool
allow_operation
=
true
)
{
return
_as_graph_element_locked
(
obj
,
allow_tensor
,
allow_operation
)
;
}
private
Func
<
object
>
_as_graph_element
(
object
obj
)
{
return
null
;
}
private
T
_as_graph_element_locked
<
T
>
(
T
obj
,
bool
allow_tensor
=
true
,
bool
allow_operation
=
true
)
{
string
types_str
=
""
;
if
(
allow_tensor
&&
allow_operation
)
{
types_str
=
"Tensor or Operation"
;
}
else
if
(
allow_tensor
)
{
types_str
=
"Tensor"
;
}
else
if
(
allow_operation
)
{
types_str
=
"Operation"
;
}
var
temp_obj
=
_as_graph_element
(
obj
)
;
if
(
obj
is
Tensor
&&
allow_tensor
)
{
if
(
(
obj
as
Tensor
)
.
graph
.
Equals
(
this
)
)
{
return
obj
;
}
else
{
throw
new
Exception
(
$
"Tensor
{
obj
}
is not an element of this graph."
)
;
}
}
throw
new
Exception
(
$
"Can not convert a
{
typeof
(
T
)
.
Name
}
into a
{
types_str
}
."
)
;
}
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
bool
is_fetchable
<
T
>
(
T
tensor_or_op
)
{
if
(
tensor_or_op
is
Tensor
)
{
return
!
_unfetchable_ops
.
Contains
(
(
tensor_or_op
as
Tensor
)
.
name
)
;
;
}
else
if
(
tensor_or_op
is
Operation
)
{
return
!
_unfetchable_ops
.
Contains
(
(
tensor_or_op
as
Operation
)
.
name
)
;
}
return
false
;
}
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
(
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL