FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TensorFlow.NET/src/TensorFlowNET.Core/ops.py.cs at v0.1.0-Tensor · attackgithub/TensorFlow.NET · GitHub
attackgithub
TensorFlow.NET
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TensorFlow.NET
/
src
/
TensorFlowNET.Core
/
ops.py.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
227 lines (198 loc) · 7.72 KB
Breadcrumbs
TensorFlow.NET
/
src
/
TensorFlowNET.Core
/
ops.py.cs
Copy path
File metadata and controls
227 lines (198 loc) · 7.72 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Runtime
.
InteropServices
;
using
System
.
Text
;
using
System
.
Threading
;
using
Tensorflow
;
using
node_def_pb2
=
Tensorflow
;
using
Google
.
Protobuf
;
using
System
.
Linq
;
namespace
Tensorflow
{
public
partial
class
ops
{
public
static
void
add_to_collection
<
T
>
(
string
name
,
T
value
)
{
var
graph
=
tf
.
get_default_graph
(
)
;
graph
.
add_to_collection
(
name
,
value
)
;
}
public
static
void
add_to_collections
<
T
>
(
List
<
string
>
names
,
T
value
)
{
var
graph
=
tf
.
get_default_graph
(
)
;
graph
.
add_to_collections
(
names
,
value
)
;
}
/// <summary>
/// Wrapper for `Graph.get_collection()` using the default graph.
/// contains many standard names for collections.
/// </summary>
/// <param name="key">
/// The key for the collection. For example, the `GraphKeys` class
/// </param>
/// <param name="scope"></param>
/// <returns>
/// The list of values in the collection with the given `name`, or
/// an empty list if no value has been added to that collection. The
/// list contains the values in the order under which they were
/// collected.
/// </returns>
public
static
object
get_collection
(
string
key
,
string
scope
=
""
)
{
return
get_default_graph
(
)
.
get_collection
(
key
,
scope
)
;
}
public
static
Graph
get_default_graph
(
)
{
return
tf
.
Graph
(
)
;
}
public
static
Graph
_get_graph_from_inputs
(
List
<
Tensor
>
op_input_list
,
Graph
graph
=
null
)
{
foreach
(
var
op_input
in
op_input_list
)
{
// Determine if this is a valid graph_element.
var
graph_element
=
op_input
;
}
return
get_default_graph
(
)
;
}
/// <summary>
/// Converts the given `value` to a `Tensor`.
/// </summary>
/// <param name="value"></param>
/// <param name="dtype"></param>
/// <param name="name"></param>
/// <returns></returns>
public
static
Tensor
convert_to_tensor
(
object
value
,
TF_DataType
dtype
=
TF_DataType
.
DtInvalid
,
string
name
=
""
)
{
switch
(
value
)
{
case
Tensor
val
:
return
val
;
default
:
var
nd
=
tensor_util
.
convert_to_numpy_ndarray
(
value
)
;
return
constant_op
.
Constant
(
nd
,
name
)
;
}
}
/// <summary>
/// Wrapper for `Graph.control_dependencies()` using the default graph.
/// </summary>
/// <param name="control_inputs"></param>
public
static
_ControlDependenciesController
control_dependencies
(
Operation
[
]
control_inputs
)
{
return
get_default_graph
(
)
.
control_dependencies
(
control_inputs
)
;
}
/// <summary>
/// Creates a TF_Operation.
/// </summary>
/// <param name="graph">a `Graph`.</param>
/// <param name="node_def">`node_def_pb2.NodeDef` for the operation to create.</param>
/// <param name="inputs">
/// A list of `Tensor`s (corresponding to scalar inputs) and lists of
/// `Tensor`s (corresponding to sequence inputs, e.g. "int64 * N",
/// "list(int64)"). The length of the list should be equal to the number of
/// inputs specified by this operation's op def.
/// </param>
/// <param name="control_inputs">A list of `Operation`s to set as control dependencies.</param>
/// <returns>A wrapped TF_Operation*.</returns>
public
static
IntPtr
_create_c_op
(
Graph
graph
,
NodeDef
node_def
,
List
<
Tensor
>
inputs
,
Operation
[
]
control_inputs
)
{
var
op_desc
=
graph
.
NewOperation
(
node_def
.
Op
,
node_def
.
Name
)
;
// Add inputs
if
(
inputs
!=
null
)
{
foreach
(
var
op_input
in
inputs
)
{
bool
isList
=
false
;
if
(
!
isList
)
{
c_api
.
TF_AddInput
(
op_desc
,
op_input
.
_as_tf_output
(
)
)
;
}
else
{
c_api
.
TF_AddInputList
(
op_desc
,
inputs
.
Select
(
x
=>
x
.
_as_tf_output
(
)
)
.
ToArray
(
)
,
inputs
.
Count
)
;
}
}
}
var
status
=
new
Status
(
)
;
// Add control inputs
foreach
(
var
control_input
in
control_inputs
)
c_api
.
TF_AddControlInput
(
op_desc
,
control_input
)
;
// Add attrs
foreach
(
var
attr
in
node_def
.
Attr
)
{
var
bytes
=
attr
.
Value
.
ToByteArray
(
)
;
var
proto
=
Marshal
.
AllocHGlobal
(
bytes
.
Length
)
;
Marshal
.
Copy
(
bytes
,
0
,
proto
,
bytes
.
Length
)
;
c_api
.
TF_SetAttrValueProto
(
op_desc
,
attr
.
Key
,
proto
,
proto_len
:
(
uint
)
bytes
.
Length
,
status
:
status
)
;
status
.
Check
(
true
)
;
}
var
c_op
=
c_api
.
TF_FinishOperation
(
op_desc
,
status
)
;
status
.
Check
(
true
)
;
return
c_op
;
}
public
static
OpDef
_get_op_def
(
Graph
graph
,
string
type
)
{
return
graph
.
GetOpDef
(
type
)
;
}
public
static
NodeDef
_NodeDef
(
string
op_type
,
string
name
,
string
device
=
""
,
Dictionary
<
string
,
AttrValue
>
attrs
=
null
)
{
var
node_def
=
new
node_def_pb2
.
NodeDef
(
)
;
node_def
.
Op
=
op_type
;
node_def
.
Name
=
name
;
foreach
(
var
attr
in
attrs
)
{
node_def
.
Attr
.
Add
(
attr
.
Key
,
attr
.
Value
)
;
}
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
;
}
}
/// <summary>
/// A context manager that lifts ops out of control-flow scopes and function-building graphs.
/// </summary>
/// <returns></returns>
public
static
void
init_scope
(
)
{
// Retrieve the active name scope: entering an `init_scope` preserves
// the name scope of the current context.
var
default_graph
=
get_default_graph
(
)
;
var
scope
=
default_graph
.
get_name_scope
(
)
;
if
(
!
String
.
IsNullOrEmpty
(
scope
)
&&
!
scope
.
EndsWith
(
"/"
)
)
// Names that end with trailing slashes are treated by `name_scope` as
// absolute.
scope
+=
"/"
;
// inner_device_stack = default_graph._device_function_stack
// var outer_context = default_graph.as_default;
Python
.
with
(
ops
.
control_dependencies
(
null
)
,
delegate
{
var
outer_graph
=
get_default_graph
(
)
;
// outer_device_stack = None
}
)
;
}
private
static
int
uid_number
=
0
;
/// <summary>
/// A unique (within this program execution) integer.
/// Not thread safe
/// </summary>
/// <returns></returns>
public
static
int
uid
(
)
{
return
uid_number
++
;
}
public
static
void
colocate_with
(
Operation
op
,
bool
ignore_existing
=
false
)
{
_colocate_with_for_gradient
(
op
,
null
,
ignore_existing
)
;
}
private
static
void
_colocate_with_for_gradient
(
Operation
op
,
int
?
gradient_uid
,
bool
ignore_existing
=
false
)
{
var
default_graph
=
get_default_graph
(
)
;
default_graph
.
_colocate_with_for_gradient
(
op
,
gradient_uid
,
ignore_existing
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL