FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TensorFlow.NET/src/TensorFlowNET.Core/Binding.Util.cs at master · feelsyt/TensorFlow.NET · GitHub
feelsyt
/
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
/
Binding.Util.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
537 lines (466 loc) · 16.6 KB
Breadcrumbs
TensorFlow.NET
/
src
/
TensorFlowNET.Core
/
Binding.Util.cs
Copy path
File metadata and controls
537 lines (466 loc) · 16.6 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*****************************************************************************
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
using
Tensorflow
.
NumPy
;
using
System
;
using
System
.
Collections
;
using
System
.
Collections
.
Generic
;
using
System
.
ComponentModel
;
using
System
.
Diagnostics
;
using
System
.
IO
;
using
System
.
Linq
;
using
Tensorflow
.
Operations
;
namespace
Tensorflow
{
/// <summary>
/// Binding utilities to mimic python functions.
/// </summary>
public
static
partial
class
Binding
{
public
static
T2
get
<
T1
,
T2
>
(
this
Dictionary
<
T1
,
T2
>
dict
,
T1
key
)
=>
key
==
null
?
default
:
(
dict
.
ContainsKey
(
key
)
?
dict
[
key
]
:
default
)
;
public
static
void
Update
<
T
>
(
this
IList
<
T
>
list
,
T
element
)
{
var
index
=
list
.
IndexOf
(
element
)
;
if
(
index
<
0
)
list
.
Add
(
element
)
;
else
{
list
[
index
]
=
element
;
}
}
public
static
void
difference_update
<
T
>
(
this
IList
<
T
>
list
,
IList
<
T
>
list2
)
{
foreach
(
var
el
in
list2
)
{
if
(
list
.
Contains
(
el
)
)
list
.
Remove
(
el
)
;
}
}
public
static
void
add
<
T
>
(
this
IList
<
T
>
list
,
T
element
)
=>
list
.
Add
(
element
)
;
public
static
void
add
<
T
>
(
this
IList
<
T
>
list
,
IEnumerable
<
T
>
elements
)
{
foreach
(
var
ele
in
elements
)
list
.
Add
(
ele
)
;
}
public
static
void
append
<
T
>
(
this
IList
<
T
>
list
,
T
element
)
=>
list
.
Insert
(
list
.
Count
,
element
)
;
public
static
void
append
<
T
>
(
this
IList
<
T
>
list
,
IList
<
T
>
elements
)
{
for
(
int
i
=
0
;
i
<
elements
.
Count
(
)
;
i
++
)
list
.
Insert
(
list
.
Count
,
elements
[
i
]
)
;
}
public
static
T
[
]
concat
<
T
>
(
this
IList
<
T
>
list1
,
IList
<
T
>
list2
)
{
var
list
=
new
List
<
T
>
(
)
;
list
.
AddRange
(
list1
)
;
list
.
AddRange
(
list2
)
;
return
list
.
ToArray
(
)
;
}
public
static
void
extend
<
T
>
(
this
List
<
T
>
list
,
IEnumerable
<
T
>
elements
)
=>
list
.
AddRange
(
elements
)
;
private
static
string
_tostring
(
object
obj
)
{
switch
(
obj
)
{
case
NDArray
nd
:
return
nd
.
ToString
(
)
;
/*case Array arr:
if (arr.Rank != 1 || arr.GetType().GetElementType()?.IsArray == true)
arr = Arrays.Flatten(arr);
var objs = toObjectArray(arr);
return $"[{string.Join(", ", objs.Select(_tostring))}]";*/
default
:
return
obj
?
.
ToString
(
)
??
"null"
;
}
}
private
static
TextWriter
_writer
=
Console
.
Out
;
public
static
TextWriter
tf_output_redirect
{
set
{
if
(
_writer
!=
null
)
{
_writer
.
Flush
(
)
;
if
(
_writer
is
StringWriter
sw
)
sw
.
GetStringBuilder
(
)
.
Clear
(
)
;
}
_writer
=
value
;
}
get
=>
_writer
??
Console
.
Out
;
}
public
static
void
print
(
object
obj
)
{
tf_output_redirect
.
WriteLine
(
_tostring
(
obj
)
)
;
}
public
static
void
print
(
string
format
,
params
object
[
]
objects
)
{
if
(
!
format
.
Contains
(
"{}"
)
)
{
tf_output_redirect
.
WriteLine
(
format
+
" "
+
string
.
Join
(
" "
,
objects
.
Select
(
x
=>
x
.
ToString
(
)
)
)
)
;
return
;
}
foreach
(
var
obj
in
objects
)
{
}
tf_output_redirect
.
WriteLine
(
format
)
;
}
public
static
int
len
(
object
a
)
{
switch
(
a
)
{
case
Tensor
tensor
:
return
(
int
)
tensor
.
shape
[
0
]
;
case
Tensors
arr
:
return
arr
.
Length
;
case
Array
arr
:
return
arr
.
Length
;
case
IList
arr
:
return
arr
.
Count
;
case
ICollection
arr
:
return
arr
.
Count
;
case
IEnumerable
enumerable
:
return
enumerable
.
OfType
<
object
>
(
)
.
Count
(
)
;
case
Axis
axis
:
return
axis
.
size
;
case
Shape
arr
:
return
arr
.
ndim
;
}
throw
new
NotImplementedException
(
"len() not implemented for type: "
+
a
.
GetType
(
)
)
;
}
public
static
int
min
(
int
a
,
int
b
)
=>
Math
.
Min
(
a
,
b
)
;
public
static
float
min
(
float
a
,
float
b
)
=>
Math
.
Min
(
a
,
b
)
;
public
static
int
max
(
int
a
,
int
b
)
=>
Math
.
Max
(
a
,
b
)
;
public
static
T
[
]
list
<
T
>
(
IEnumerable
<
T
>
list
)
=>
list
.
ToArray
(
)
;
public
static
IEnumerable
<
int
>
range
(
int
end
)
{
return
Enumerable
.
Range
(
0
,
end
)
;
}
public
static
IEnumerable
<
int
>
range
(
int
start
,
int
end
)
{
return
Enumerable
.
Range
(
start
,
end
-
start
)
;
}
public
static
IEnumerable
<
T
>
reversed
<
T
>
(
IList
<
T
>
values
)
{
var
len
=
values
.
Count
;
for
(
int
i
=
len
-
1
;
i
>=
0
;
i
--
)
yield
return
values
[
i
]
;
}
[
DebuggerStepThrough
]
public
static
void
tf_with
<
T
>
(
T
py
,
Action
<
T
>
action
)
where
T
:
ITensorFlowObject
{
py
.
__enter__
(
)
;
action
(
py
)
;
py
.
__exit__
(
)
;
}
[
DebuggerStepThrough
]
public
static
TOut
tf_with
<
TIn
,
TOut
>
(
TIn
py
,
Func
<
TIn
,
TOut
>
action
)
where
TIn
:
ITensorFlowObject
{
py
.
__enter__
(
)
;
var
result
=
action
(
py
)
;
py
.
__exit__
(
)
;
return
result
;
}
public
static
float
time
(
)
{
return
(
float
)
(
DateTime
.
UtcNow
-
new
DateTime
(
1970
,
1
,
1
)
)
.
TotalSeconds
;
}
public
static
IEnumerable
<
(
T1
,
T2
)
>
zip
<
T1
,
T2
>
(
(
T1
,
T1
)
t1
,
(
T2
,
T2
)
t2
)
{
for
(
int
i
=
0
;
i
<
2
;
i
++
)
{
if
(
i
==
0
)
yield
return
(
t1
.
Item1
,
t2
.
Item1
)
;
else
yield
return
(
t1
.
Item2
,
t2
.
Item2
)
;
}
}
public
static
IEnumerable
<
(
T
,
T
)
>
zip
<
T
>
(
NDArray
t1
,
NDArray
t2
,
Axis
axis
=
null
)
where
T
:
unmanaged
{
if
(
axis
==
null
)
{
var
a
=
t1
.
ToArray
<
T
>
(
)
;
var
b
=
t2
.
ToArray
<
T
>
(
)
;
for
(
int
i
=
0
;
i
<
a
.
Length
;
i
++
)
yield
return
(
a
[
i
]
,
b
[
i
]
)
;
}
else
throw
new
NotImplementedException
(
""
)
;
}
public
static
IEnumerable
<
(
T1
,
T2
)
>
zip
<
T1
,
T2
>
(
IList
<
T1
>
t1
,
IList
<
T2
>
t2
)
{
for
(
int
i
=
0
;
i
<
t1
.
Count
;
i
++
)
yield
return
(
t1
[
i
]
,
t2
[
i
]
)
;
}
public
static
IEnumerable
<
(
T1
,
T2
,
T3
)
>
zip
<
T1
,
T2
,
T3
>
(
IList
<
T1
>
t1
,
IList
<
T2
>
t2
,
IList
<
T3
>
t3
)
{
for
(
int
i
=
0
;
i
<
t1
.
Count
;
i
++
)
yield
return
(
t1
[
i
]
,
t2
[
i
]
,
t3
[
i
]
)
;
}
public
static
IEnumerable
<
(
T1
,
T2
)
>
zip
<
T1
,
T2
>
(
NDArray
t1
,
NDArray
t2
)
where
T1
:
unmanaged
where
T2
:
unmanaged
{
//var a = t1.AsIterator<T1>();
//var b = t2.AsIterator<T2>();
//while (a.HasNext() && b.HasNext())
//yield return (a.MoveNext(), b.MoveNext());
throw
new
NotImplementedException
(
""
)
;
}
public
static
IEnumerable
<
(
T1
,
T2
)
>
zip
<
T1
,
T2
>
(
IEnumerable
<
T1
>
e1
,
IEnumerable
<
T2
>
e2
)
{
return
e1
.
Zip
(
e2
,
(
t1
,
t2
)
=>
(
t1
,
t2
)
)
;
}
public
static
IEnumerable
<
(
TKey
,
TValue
)
>
enumerate
<
TKey
,
TValue
>
(
Dictionary
<
TKey
,
TValue
>
values
)
{
foreach
(
var
item
in
values
)
yield
return
(
item
.
Key
,
item
.
Value
)
;
}
public
static
IEnumerable
<
(
TKey
,
TValue
)
>
enumerate
<
TKey
,
TValue
>
(
KeyValuePair
<
TKey
,
TValue
>
[
]
values
)
{
var
len
=
values
.
Length
;
for
(
var
i
=
0
;
i
<
len
;
i
++
)
{
var
item
=
values
[
i
]
;
yield
return
(
item
.
Key
,
item
.
Value
)
;
}
}
public
static
IEnumerable
<
(
int
,
T
)
>
enumerate
<
T
>
(
IList
<
T
>
values
)
{
var
len
=
values
.
Count
;
for
(
int
i
=
0
;
i
<
len
;
i
++
)
yield
return
(
i
,
values
[
i
]
)
;
}
public
static
IEnumerable
<
(
int
,
T
)
>
enumerate
<
T
>
(
IEnumerable
<
T
>
values
,
int
start
=
0
,
int
step
=
1
)
{
int
i
=
0
;
foreach
(
var
val
in
values
)
{
if
(
i
++
<
start
)
continue
;
yield
return
(
i
-
1
,
val
)
;
}
}
[
DebuggerStepThrough
]
public
static
Dictionary
<
string
,
object
>
ConvertToDict
(
object
dyn
)
{
var
dictionary
=
new
Dictionary
<
string
,
object
>
(
)
;
foreach
(
PropertyDescriptor
propertyDescriptor
in
TypeDescriptor
.
GetProperties
(
dyn
)
)
{
object
obj
=
propertyDescriptor
.
GetValue
(
dyn
)
;
string
name
=
propertyDescriptor
.
Name
;
dictionary
.
Add
(
name
,
obj
)
;
}
return
dictionary
;
}
public
static
bool
all
(
IEnumerable
enumerable
)
{
foreach
(
var
e1
in
enumerable
)
{
if
(
!
Convert
.
ToBoolean
(
e1
)
)
return
false
;
}
return
true
;
}
public
static
bool
any
(
IEnumerable
enumerable
)
{
foreach
(
var
e1
in
enumerable
)
{
if
(
Convert
.
ToBoolean
(
e1
)
)
return
true
;
}
return
false
;
}
public
static
double
sum
(
IEnumerable
enumerable
)
{
var
typedef
=
new
Type
[
]
{
typeof
(
double
)
,
typeof
(
int
)
,
typeof
(
float
)
}
;
var
sum
=
0.0d
;
foreach
(
var
e1
in
enumerable
)
{
if
(
!
typedef
.
Contains
(
e1
.
GetType
(
)
)
)
throw
new
Exception
(
"Numeric array expected"
)
;
sum
+=
(
double
)
e1
;
}
return
sum
;
}
public
static
float
sum
(
IEnumerable
<
float
>
enumerable
)
=>
enumerable
.
Sum
(
)
;
public
static
int
sum
(
IEnumerable
<
int
>
enumerable
)
=>
enumerable
.
Sum
(
)
;
public
static
double
sum
<
TKey
,
TValue
>
(
Dictionary
<
TKey
,
TValue
>
values
)
{
return
sum
(
values
.
Keys
)
;
}
public
static
IEnumerable
<
double
>
slice
(
double
start
,
double
end
,
double
step
=
1
)
{
for
(
double
i
=
start
;
i
<
end
;
i
+=
step
)
yield
return
i
;
}
public
static
IEnumerable
<
float
>
slice
(
float
start
,
float
end
,
float
step
=
1
)
{
for
(
float
i
=
start
;
i
<
end
;
i
+=
step
)
yield
return
i
;
}
public
static
IEnumerable
<
int
>
slice
(
int
start
,
int
end
,
int
step
=
1
)
{
for
(
int
i
=
start
;
i
<
end
;
i
+=
step
)
yield
return
i
;
}
public
static
IEnumerable
<
int
>
slice
(
int
range
)
{
for
(
int
i
=
0
;
i
<
range
;
i
++
)
yield
return
i
;
}
public
static
bool
hasattr
(
object
obj
,
string
key
)
{
var
__type__
=
(
obj
)
.
GetType
(
)
;
var
__member__
=
__type__
.
GetMembers
(
)
;
var
__memberobject__
=
__type__
.
GetMember
(
key
)
;
return
(
__memberobject__
.
Length
>
0
)
?
true
:
false
;
}
public
static
IEnumerable
TupleToEnumerable
(
object
tuple
)
{
Type
t
=
tuple
.
GetType
(
)
;
if
(
t
.
IsGenericType
&&
(
t
.
FullName
.
StartsWith
(
"System.Tuple"
)
||
t
.
FullName
.
StartsWith
(
"System.ValueTuple"
)
)
)
{
var
flds
=
t
.
GetFields
(
)
;
for
(
int
i
=
0
;
i
<
flds
.
Length
;
i
++
)
{
yield
return
flds
[
i
]
.
GetValue
(
tuple
)
;
}
}
else
{
throw
new
System
.
Exception
(
"Expected Tuple."
)
;
}
}
public
static
bool
isinstance
(
object
Item1
,
Type
Item2
)
{
return
Item1
.
GetType
(
)
==
Item2
;
}
public
static
bool
isinstance
(
object
Item1
,
object
tuple
)
{
foreach
(
var
t
in
TupleToEnumerable
(
tuple
)
)
if
(
isinstance
(
Item1
,
(
Type
)
t
)
)
return
true
;
return
false
;
}
public
static
bool
issubset
<
T
>
(
this
IEnumerable
<
T
>
subset
,
IEnumerable
<
T
>
src
)
{
bool
issubset
=
true
;
foreach
(
var
element
in
subset
)
{
if
(
!
src
.
Contains
(
element
)
)
{
issubset
=
false
;
continue
;
}
}
return
true
;
}
public
static
void
extendleft
<
T
>
(
this
Queue
<
T
>
queue
,
IEnumerable
<
T
>
elements
)
{
foreach
(
var
element
in
elements
.
Reverse
(
)
)
queue
.
Enqueue
(
element
)
;
}
public
static
bool
empty
<
T
>
(
this
Queue
<
T
>
queue
)
=>
queue
.
Count
==
0
;
public
static
TValue
SetDefault
<
TKey
,
TValue
>
(
this
Dictionary
<
TKey
,
TValue
>
dic
,
TKey
key
,
TValue
defaultValue
)
{
if
(
dic
.
ContainsKey
(
key
)
)
return
dic
[
key
]
;
dic
[
key
]
=
defaultValue
;
return
defaultValue
;
}
public
static
TValue
Get
<
TKey
,
TValue
>
(
this
Dictionary
<
TKey
,
TValue
>
dic
,
TKey
key
,
TValue
defaultValue
)
{
if
(
dic
.
ContainsKey
(
key
)
)
return
dic
[
key
]
;
return
defaultValue
;
}
public
static
Shape
GetShape
(
this
object
data
)
{
if
(
data
is
NDArray
nd
)
return
nd
.
shape
;
else
if
(
data
is
Tensor
tensor
)
return
tensor
.
shape
;
else
if
(
data
is
Axis
axis
)
return
axis
.
IsScalar
?
Shape
.
Scalar
:
new
Shape
(
axis
.
axis
.
Length
)
;
else
if
(
data
is
Shape
shape
)
return
new
Shape
(
shape
.
rank
)
;
else
if
(
!
data
.
GetType
(
)
.
IsArray
)
return
Shape
.
Scalar
;
switch
(
data
)
{
case
Array
array
:
var
dims
=
range
(
array
.
Rank
)
.
Select
(
x
=>
(
long
)
array
.
GetLength
(
x
)
)
.
ToArray
(
)
;
return
new
Shape
(
dims
)
;
default
:
throw
new
NotImplementedException
(
""
)
;
}
}
public
static
NDArray
GetFlattenArray
(
NDArray
x
)
{
switch
(
x
.
GetDataType
(
)
)
{
case
TF_DataType
.
TF_FLOAT
:
x
=
x
.
ToArray
<
float
>
(
)
;
break
;
case
TF_DataType
.
TF_DOUBLE
:
x
=
x
.
ToArray
<
double
>
(
)
;
break
;
case
TF_DataType
.
TF_INT16
:
case
TF_DataType
.
TF_INT32
:
x
=
x
.
ToArray
<
int
>
(
)
;
break
;
case
TF_DataType
.
TF_INT64
:
x
=
x
.
ToArray
<
long
>
(
)
;
break
;
default
:
break
;
}
return
x
;
}
public
static
TF_DataType
GetDataType
(
this
object
data
)
{
var
type
=
data
.
GetType
(
)
;
switch
(
data
)
{
case
Shape
:
return
TF_DataType
.
TF_INT64
;
case
Axis
:
return
TF_DataType
.
TF_INT32
;
case
NDArray
nd
:
return
nd
.
dtype
;
case
Tensor
tensor
:
return
tensor
.
dtype
;
case
Tensors
tensors
:
return
tensors
.
dtype
;
case
IEnumerable
<
Tensor
>
tensors
:
return
tensors
.
Where
(
x
=>
x
is
not
null
)
.
First
(
)
.
dtype
;
case
RefVariable
variable
:
return
variable
.
dtype
;
case
ResourceVariable
variable
:
return
variable
.
dtype
;
default
:
return
type
.
as_tf_dtype
(
)
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL