FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TensorFlow.NET/src/TensorFlowNET.Core/Tensors/tensor_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
/
Tensors
/
tensor_util.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
725 lines (654 loc) · 27.2 KB
Breadcrumbs
TensorFlow.NET
/
src
/
TensorFlowNET.Core
/
Tensors
/
tensor_util.cs
Copy path
File metadata and controls
725 lines (654 loc) · 27.2 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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/*****************************************************************************
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
.
Generic
;
using
System
.
Linq
;
using
System
.
Text
;
using
Tensorflow
.
Eager
;
using
Tensorflow
.
Graphs
;
using
static
Tensorflow
.
Binding
;
using
System
.
Diagnostics
;
namespace
Tensorflow
{
public
static
class
tensor_util
{
/// <summary>
/// Returns the constant value of the given tensor, if efficiently calculable.
/// </summary>
/// <param name="tensor"></param>
/// <param name="partial"></param>
/// <returns></returns>
public
static
NDArray
constant_value
(
Tensor
tensor
,
bool
partial
=
false
)
{
if
(
tensor
is
NDArray
nd
)
return
nd
;
else
if
(
tensor
is
EagerTensor
)
return
tensor
.
numpy
(
)
;
NDArray
ret
=
_ConstantValue
(
tensor
,
partial
)
;
if
(
!
(
ret
is
null
)
)
tensor
.
graph
.
prevent_feeding
(
tensor
)
;
return
ret
;
}
private
static
NDArray
_ConstantValue
(
Tensor
tensor
,
bool
partial
)
{
switch
(
tensor
.
op
.
type
)
{
case
"Const"
:
return
MakeNdarray
(
tensor
.
op
.
get_attr
(
"value"
)
as
TensorProto
)
;
default
:
return
null
;
}
}
public
static
NDArray
MakeNdarray
(
TensorProto
tensor
)
{
var
shape
=
new
Shape
(
tensor
.
TensorShape
.
Dim
.
Select
(
x
=>
x
.
Size
)
.
ToArray
(
)
)
;
var
num_elements
=
shape
.
size
;
var
tensor_dtype
=
tensor
.
Dtype
.
as_tf_dtype
(
)
;
T
[
]
ExpandArrayToSize
<
T
>
(
IList
<
T
>
src
)
{
if
(
src
.
Count
==
0
)
{
return
new
T
[
0
]
;
}
var
pad_count
=
num_elements
-
src
.
Count
;
var
pre
=
pad_count
/
2
;
var
after
=
pad_count
-
pre
;
var
first_elem
=
src
[
0
]
;
var
last_elem
=
src
[
src
.
Count
-
1
]
;
T
[
]
res
=
new
T
[
num_elements
]
;
for
(
long
i
=
0
;
i
<
num_elements
;
i
++
)
{
if
(
i
<
pre
)
res
[
i
]
=
first_elem
;
else
if
(
i
>=
num_elements
-
after
)
res
[
i
]
=
last_elem
;
else
res
[
i
]
=
src
[
(
int
)
(
i
-
pre
)
]
;
}
return
res
;
}
if
(
shape
.
ndim
>
0
&&
tensor
.
TensorContent
.
Length
>
0
)
{
return
np
.
frombuffer
(
tensor
.
TensorContent
.
ToByteArray
(
)
,
shape
,
tensor_dtype
)
;
}
NDArray
values
;
if
(
tensor
.
Dtype
==
DataType
.
DtHalf
||
tensor
.
Dtype
==
DataType
.
DtBfloat16
)
{
values
=
np
.
array
(
ExpandArrayToSize
(
tensor
.
HalfVal
)
)
;
}
else
if
(
tensor
.
Dtype
==
DataType
.
DtFloat
)
{
values
=
np
.
array
(
ExpandArrayToSize
(
tensor
.
FloatVal
)
)
;
}
else
if
(
new
DataType
[
]
{
DataType
.
DtInt32
,
DataType
.
DtUint8
}
.
Contains
(
tensor
.
Dtype
)
)
{
values
=
np
.
array
(
ExpandArrayToSize
(
tensor
.
IntVal
)
)
;
}
else
if
(
new
DataType
[
]
{
DataType
.
DtInt64
}
.
Contains
(
tensor
.
Dtype
)
)
{
values
=
np
.
array
(
ExpandArrayToSize
(
tensor
.
Int64Val
)
)
;
}
else
if
(
new
DataType
[
]
{
DataType
.
DtUint64
}
.
Contains
(
tensor
.
Dtype
)
)
{
values
=
np
.
array
(
ExpandArrayToSize
(
tensor
.
Uint64Val
)
)
;
}
else
if
(
tensor
.
Dtype
==
DataType
.
DtBool
)
{
values
=
np
.
array
(
ExpandArrayToSize
(
tensor
.
BoolVal
)
)
;
}
else
{
throw
new
TypeError
(
$
"Unsupported tensor type:
{
tensor
.
Dtype
}
. See "
+
$
"https://www.tensorflow.org/api_docs/python/tf/dtypes for supported TF dtypes."
)
;
}
if
(
values
.
size
==
0
)
{
return
np
.
zeros
(
shape
,
tensor_dtype
)
;
}
return
values
.
reshape
(
shape
)
;
}
private
static
readonly
TF_DataType
[
]
quantized_types
=
new
TF_DataType
[
]
{
TF_DataType
.
TF_QINT8
,
TF_DataType
.
TF_QUINT8
,
TF_DataType
.
TF_QINT16
,
TF_DataType
.
TF_QUINT16
,
TF_DataType
.
TF_QINT32
}
;
private
static
Array
ConvertArray
<
TOut
>
(
Array
inputArray
,
Func
<
object
,
TOut
>
converter
)
{
if
(
inputArray
==
null
)
throw
new
ArgumentNullException
(
nameof
(
inputArray
)
)
;
var
elementType
=
typeof
(
TOut
)
;
var
lengths
=
new
int
[
inputArray
.
Rank
]
;
for
(
var
i
=
0
;
i
<
inputArray
.
Rank
;
i
++
)
{
lengths
[
i
]
=
inputArray
.
GetLength
(
i
)
;
}
var
outputArray
=
Array
.
CreateInstance
(
elementType
,
lengths
)
;
FillArray
(
inputArray
,
outputArray
,
converter
,
new
int
[
inputArray
.
Rank
]
,
0
)
;
return
outputArray
;
}
private
static
void
FillArray
<
TIn
,
TOut
>
(
Array
inputArray
,
Array
outputArray
,
Func
<
TIn
,
TOut
>
converter
,
int
[
]
indices
,
int
dimension
)
{
if
(
dimension
==
inputArray
.
Rank
-
1
)
{
for
(
int
i
=
0
;
i
<
inputArray
.
GetLength
(
dimension
)
;
i
++
)
{
indices
[
dimension
]
=
i
;
var
inputValue
=
(
TIn
)
inputArray
.
GetValue
(
indices
)
;
var
convertedValue
=
converter
(
inputValue
)
;
outputArray
.
SetValue
(
convertedValue
,
indices
)
;
}
}
else
{
for
(
int
i
=
0
;
i
<
inputArray
.
GetLength
(
dimension
)
;
i
++
)
{
indices
[
dimension
]
=
i
;
FillArray
(
inputArray
,
outputArray
,
converter
,
indices
,
dimension
+
1
)
;
}
}
}
/// <summary>
/// Create a TensorProto, invoked in graph mode
/// </summary>
/// <param name="values"></param>
/// <param name="dtype"></param>
/// <param name="shape"></param>
/// <param name="verify_shape"></param>
/// <param name="allow_broadcast"></param>
/// <returns></returns>
public
static
TensorProto
make_tensor_proto
(
object
values
,
TF_DataType
dtype
=
TF_DataType
.
DtInvalid
,
Shape
?
shape
=
null
,
bool
verify_shape
=
false
,
bool
allow_broadcast
=
false
)
{
if
(
allow_broadcast
&&
verify_shape
)
throw
new
ValueError
(
"allow_broadcast and verify_shape are not both allowed."
)
;
if
(
values
is
TensorProto
tp
)
return
tp
;
var
origin_dtype
=
values
.
GetDataType
(
)
;
if
(
dtype
==
TF_DataType
.
DtInvalid
)
dtype
=
origin_dtype
;
else
if
(
origin_dtype
!=
dtype
)
{
var
new_system_dtype
=
dtype
.
as_system_dtype
(
)
;
if
(
dtype
!=
TF_DataType
.
TF_STRING
&&
dtype
!=
TF_DataType
.
TF_VARIANT
&&
dtype
!=
TF_DataType
.
TF_RESOURCE
)
{
if
(
values
is
Array
arrayValues
)
{
values
=
dtype
switch
{
TF_DataType
.
TF_INT32
=>
ConvertArray
(
arrayValues
,
Convert
.
ToInt32
)
,
TF_DataType
.
TF_FLOAT
=>
ConvertArray
(
arrayValues
,
Convert
.
ToSingle
)
,
TF_DataType
.
TF_DOUBLE
=>
ConvertArray
(
arrayValues
,
Convert
.
ToDouble
)
,
_
=>
values
,
}
;
}
else
{
values
=
Convert
.
ChangeType
(
values
,
new_system_dtype
)
;
}
}
else
{
}
dtype
=
values
.
GetDataType
(
)
;
}
shape
=
shape
??
values
.
GetShape
(
)
;
var
tensor_proto
=
new
TensorProto
{
Dtype
=
dtype
.
as_datatype_enum
(
)
,
TensorShape
=
shape
.
as_shape_proto
(
)
}
;
if
(
values
is
NDArray
nd
)
{
// scalar
if
(
nd
.
shape
.
IsScalar
)
{
switch
(
nd
.
dtype
)
{
case
TF_DataType
.
TF_BOOL
:
tensor_proto
.
BoolVal
.
AddRange
(
nd
.
ToArray
<
bool
>
(
)
)
;
break
;
case
TF_DataType
.
TF_UINT8
:
tensor_proto
.
IntVal
.
AddRange
(
nd
.
ToArray
<
byte
>
(
)
.
Select
(
x
=>
(
int
)
x
)
.
ToArray
(
)
)
;
break
;
case
TF_DataType
.
TF_INT32
:
tensor_proto
.
IntVal
.
AddRange
(
nd
.
ToArray
<
int
>
(
)
)
;
break
;
case
TF_DataType
.
TF_INT64
:
tensor_proto
.
Int64Val
.
AddRange
(
nd
.
ToArray
<
long
>
(
)
)
;
break
;
case
TF_DataType
.
TF_FLOAT
:
tensor_proto
.
FloatVal
.
AddRange
(
nd
.
ToArray
<
float
>
(
)
)
;
break
;
case
TF_DataType
.
TF_DOUBLE
:
tensor_proto
.
DoubleVal
.
AddRange
(
nd
.
ToArray
<
double
>
(
)
)
;
break
;
default
:
throw
new
Exception
(
"make_tensor_proto Not Implemented"
)
;
}
}
else
{
var
len
=
nd
.
dtypesize
*
nd
.
size
;
byte
[
]
bytes
=
nd
.
ToByteArray
(
)
;
tensor_proto
.
TensorContent
=
Google
.
Protobuf
.
ByteString
.
CopyFrom
(
bytes
)
;
}
}
else
if
(
dtype
==
TF_DataType
.
TF_STRING
&&
!
(
values
is
NDArray
)
)
{
if
(
values
is
string
str
)
tensor_proto
.
StringVal
.
Add
(
Google
.
Protobuf
.
ByteString
.
CopyFromUtf8
(
str
)
)
;
else
if
(
values
is
string
[
]
str_values
)
tensor_proto
.
StringVal
.
AddRange
(
str_values
.
Select
(
x
=>
Google
.
Protobuf
.
ByteString
.
CopyFromUtf8
(
x
)
)
)
;
else
if
(
values
is
byte
[
]
byte_values
)
tensor_proto
.
TensorContent
=
Google
.
Protobuf
.
ByteString
.
CopyFrom
(
byte_values
)
;
}
else
if
(
values
is
Array
array
)
{
// array
var
len
=
dtype
.
get_datatype_size
(
)
*
(
int
)
shape
.
size
;
byte
[
]
bytes
=
new
byte
[
len
]
;
System
.
Buffer
.
BlockCopy
(
array
,
0
,
bytes
,
0
,
len
)
;
tensor_proto
.
TensorContent
=
Google
.
Protobuf
.
ByteString
.
CopyFrom
(
bytes
)
;
}
else
{
switch
(
values
)
{
case
Axis
val
:
tensor_proto
.
IntVal
.
AddRange
(
val
.
axis
)
;
break
;
case
Shape
val
:
tensor_proto
.
Int64Val
.
AddRange
(
val
.
dims
)
;
break
;
case
bool
val
:
tensor_proto
.
BoolVal
.
AddRange
(
new
[
]
{
val
}
)
;
break
;
case
sbyte
val
:
tensor_proto
.
IntVal
.
AddRange
(
new
[
]
{
(
int
)
val
}
)
;
break
;
case
byte
val
:
tensor_proto
.
IntVal
.
AddRange
(
new
[
]
{
(
int
)
val
}
)
;
break
;
case
int
val
:
tensor_proto
.
IntVal
.
AddRange
(
new
[
]
{
val
}
)
;
break
;
case
long
val
:
tensor_proto
.
Int64Val
.
AddRange
(
new
[
]
{
val
}
)
;
break
;
case
float
val
:
tensor_proto
.
FloatVal
.
AddRange
(
new
[
]
{
val
}
)
;
break
;
case
double
val
:
tensor_proto
.
DoubleVal
.
AddRange
(
new
[
]
{
val
}
)
;
break
;
default
:
throw
new
Exception
(
$
"make_tensor_proto Not Implemented
{
values
.
GetType
(
)
.
Name
}
"
)
;
}
}
return
tensor_proto
;
}
public
static
Shape
constant_value_as_shape
(
Tensor
tensor
)
{
bool
hasattr
(
Graph
property
,
string
attr
)
{
var
t
=
property
.
GetType
(
)
.
GetProperties
(
)
;
foreach
(
System
.
Reflection
.
PropertyInfo
pi
in
t
)
{
if
(
pi
.
Name
==
attr
)
return
true
;
}
return
false
;
}
if
(
tensor
is
EagerTensor
eagerTensor
)
{
if
(
tensor
.
dtype
==
tf
.
int64
)
return
new
Shape
(
tensor
.
ToArray
<
long
>
(
)
)
;
else
return
new
Shape
(
tensor
.
ToArray
<
int
>
(
)
)
;
}
if
(
tensor
.
shape
.
ndim
==
0
)
{
var
value_
=
constant_value
(
tensor
)
;
if
(
value_
==
null
)
throw
new
ValueError
(
@"Received a scalar with unknown value as shape; require a statically
known scalar with value '-1' to describe an unknown shape."
)
;
if
(
(
int
)
value_
!=
-
1
)
throw
new
ValueError
(
String
.
Format
(
@"Received a scalar value {0} as shape; require a statically known
scalar with value '-1' to describe an unknown shape."
,
value_
)
)
;
return
tensor
.
shape
.
unknown_shape
(
-
1
)
;
}
var
shape
=
tensor
.
shape
.
with_rank
(
1
)
;
if
(
shape
==
new
Shape
(
new
int
[
]
{
1
}
)
)
{
return
new
Shape
(
new
int
[
]
{
}
)
;
}
else
if
(
tensor
.
op
.
type
==
"Cast"
)
{
var
pre_cast
=
constant_value_as_shape
(
tensor
.
op
.
inputs
[
0
]
)
;
if
(
pre_cast
.
dims
==
null
)
return
pre_cast
;
var
cast_dtype
=
dtypes
.
as_tf_dtype
(
(
Type
)
tensor
.
op
.
get_attr
(
"DstT"
)
)
;
if
(
!
Array
.
Exists
(
new
[
]
{
dtypes
.
int32
,
dtypes
.
int64
}
,
cast_dtype_
=>
cast_dtype_
==
cast_dtype
)
)
return
tensor
.
shape
.
unknown_shape
(
(
int
)
shape
.
dims
[
0
]
)
;
long
[
]
x_
=
{
}
;
foreach
(
var
x
in
pre_cast
.
dims
)
if
(
x
!=
-
1
)
x_
[
x_
.
Length
]
=
x
;
else
x_
[
x_
.
Length
]
=
-
1
;
var
dest_dtype_shape_array
=
np
.
array
(
x_
)
.
astype
(
cast_dtype
)
;
long
[
]
y_
=
{
}
;
foreach
(
int
y
in
dest_dtype_shape_array
.
ToArray
<
int
>
(
)
)
if
(
y
>=
0
)
y_
[
y_
.
Length
]
=
y
;
else
y_
[
y_
.
Length
]
=
-
1
;
return
new
Shape
(
y_
)
;
}
else
if
(
tensor
.
op
.
type
==
"Shape"
)
{
return
tensor
.
op
.
inputs
[
0
]
.
shape
;
}
else
if
(
tensor
.
op
.
type
==
"Pack"
)
{
var
ret_
=
new
Shape
(
new
int
[
]
{
}
)
;
if
(
(
int
)
tensor
.
op
.
get_attr
(
"axis"
)
!=
0
)
throw
new
ValueError
(
String
.
Format
(
@"Since rank 1 inputs are expected, Pack's axis: {0} must be 0, otherwise it
would not be rank 1."
,
tensor
.
op
.
get_attr
(
"axis"
)
)
)
;
foreach
(
Tensor
pack_input
in
tensor
.
op
.
inputs
)
{
var
pack_input_val
=
(
int
)
constant_value
(
pack_input
)
;
Dimension
new_dim
;
if
(
pack_input_val
<
0
)
{
new_dim
=
new
Dimension
(
-
1
)
;
}
else
if
(
pack_input_val
==
null
)
{
new_dim
=
new
Dimension
(
-
1
)
;
}
else
{
new_dim
=
new
Dimension
(
pack_input_val
)
;
}
ret_
=
ret_
.
concatenate
(
new
long
[
]
{
new_dim
}
)
;
}
return
ret_
;
}
else
if
(
tensor
.
op
.
type
==
"Concat"
)
{
var
ret_
=
new
Shape
(
new
int
[
]
{
}
)
;
var
inputlist_
=
new
ArraySegment
<
Tensor
>
(
tensor
.
op
.
inputs
,
1
,
tensor
.
op
.
inputs
.
Length
-
1
)
;
foreach
(
var
concat_input
in
inputlist_
)
{
ret_
=
ret_
.
concatenate
(
constant_value_as_shape
(
concat_input
)
)
;
}
return
ret_
;
}
else
if
(
tensor
.
op
.
type
==
"StridedSlice"
)
{
try
{
var
begin
=
constant_value
(
tensor
.
op
.
inputs
[
1
]
)
;
var
end
=
constant_value
(
tensor
.
op
.
inputs
[
2
]
)
;
var
strides
=
constant_value
(
tensor
.
op
.
inputs
[
3
]
)
;
if
(
new
[
]
{
begin
,
end
,
strides
}
.
All
(
x
=>
x
==
null
)
)
{
begin
=
begin
[
0
]
;
end
=
end
[
0
]
;
strides
=
strides
[
0
]
;
var
begin_mask
=
tensor
.
op
.
get_attr
(
"begin_mask"
)
;
if
(
(
int
)
begin_mask
==
1
)
{
begin
=
null
;
}
var
end_mask
=
tensor
.
op
.
get_attr
(
"end_mask"
)
;
if
(
(
int
)
end_mask
==
1
)
{
end
=
null
;
}
var
ellipsis_mask
=
tensor
.
op
.
get_attr
(
"ellipsis_mask"
)
;
var
new_axis_mask
=
tensor
.
op
.
get_attr
(
"new_axis_mask"
)
;
var
shrink_axis_mask
=
tensor
.
op
.
get_attr
(
"shrink_axis_mask"
)
;
bool
valid_attributes
;
if
(
!
(
bool
)
ellipsis_mask
&&
!
(
bool
)
new_axis_mask
&&
!
(
bool
)
shrink_axis_mask
&&
!
(
(
bool
)
begin_mask
||
(
int
)
begin_mask
==
1
)
&&
!
(
(
bool
)
end_mask
||
(
int
)
end_mask
==
1
)
)
{
valid_attributes
=
true
;
}
else
{
valid_attributes
=
false
;
}
if
(
valid_attributes
)
{
// sorry for the mess here, but this hacky solution was the best way
// i could come up with to implement the things done in python in c#
var
prev_
=
constant_value_as_shape
(
tensor
.
op
.
inputs
[
0
]
)
.
dims
;
var
prev
=
prev_
.
Skip
(
(
int
)
begin
)
.
Take
(
(
int
)
end
-
(
int
)
begin
)
.
ToArray
(
)
;
// 100 being the comparison doesn't really matter here; it's going to break anyway
for
(
int
iter
=
0
;
iter
!=
100
;
iter
=
iter
+
(
int
)
strides
)
{
prev
[
prev
.
Length
]
=
prev_
[
iter
]
;
if
(
(
iter
+
(
int
)
strides
)
>
prev_
.
Length
)
break
;
}
var
ret_
=
new
Shape
(
prev
)
;
return
ret_
;
}
}
}
catch
(
Exception
ex
)
{
if
(
ex
is
ValueError
||
ex
is
TypeError
)
{
}
}
}
else
if
(
tensor
.
op
.
type
==
"Placeholder"
&&
tensor
.
op
.
graph
.
building_function
&&
tensor
.
op
.
graph
is
FuncGraph
func_graph
)
{
int
i
=
0
;
foreach
(
Tensor
capture
in
func_graph
.
internal_captures
)
{
if
(
capture
.
GetType
(
)
==
typeof
(
Tensor
)
)
{
var
external_capture
=
func_graph
.
external_captures
[
i
]
;
return
constant_value_as_shape
(
external_capture
)
;
}
i
++
;
}
}
var
ret
=
tensor
.
shape
.
unknown_shape
(
(
int
)
shape
.
dims
[
0
]
)
;
var
value
=
constant_value
(
tensor
)
;
if
(
value
is
not
null
)
{
var
d_
=
new
int
[
value
.
size
]
;
foreach
(
var
(
index
,
d
)
in
enumerate
(
value
.
ToArray
<
int
>
(
)
)
)
d_
[
index
]
=
d
>=
0
?
d
:
-
1
;
ret
=
ret
.
merge_with
(
new
Shape
(
d_
)
)
;
}
return
ret
;
}
public
static
TensorShapeProto
as_shape
<
T
>
(
T
[
]
dims
)
{
TensorShapeProto
shape
=
new
TensorShapeProto
(
)
;
for
(
int
i
=
0
;
i
<
dims
.
Length
;
i
++
)
{
var
dim
=
new
TensorShapeProto
.
Types
.
Dim
(
)
;
switch
(
dims
[
i
]
)
{
case
int
n
:
dim
.
Size
=
n
;
break
;
case
long
l
:
dim
.
Size
=
l
;
break
;
default
:
throw
new
NotImplementedException
(
"as_shape Not Implemented"
)
;
}
// dim.Name = $"dim_{i}";
shape
.
Dim
.
Add
(
dim
)
;
}
return
shape
;
}
public
static
Shape
to_shape
(
long
[
]
dims
)
{
return
new
Shape
(
dims
.
Select
(
x
=>
(
int
)
x
)
.
ToArray
(
)
)
;
}
public
static
Shape
to_shape
(
int
[
]
dims
)
{
return
new
Shape
(
dims
)
;
}
public
static
TensorShapeProto
as_shape_proto
(
this
Shape
tshape
)
{
TensorShapeProto
shape
=
new
TensorShapeProto
(
)
;
for
(
int
i
=
0
;
i
<
tshape
.
ndim
;
i
++
)
{
var
dim
=
new
TensorShapeProto
.
Types
.
Dim
(
)
;
dim
.
Size
=
tshape
.
dims
[
i
]
;
//dim.Name = $"dim_{i}";
shape
.
Dim
.
Add
(
dim
)
;
}
return
shape
;
}
public
static
Shape
reshape
(
this
Shape
shape
,
int
[
]
dims
)
{
return
new
Shape
(
dims
)
;
}
public
static
TensorShapeProto
as_proto
(
this
Shape
tshape
)
{
TensorShapeProto
shape
=
new
TensorShapeProto
(
)
;
for
(
int
i
=
0
;
i
<
tshape
.
ndim
;
i
++
)
{
var
dim
=
new
TensorShapeProto
.
Types
.
Dim
(
)
;
dim
.
Size
=
tshape
.
dims
[
i
]
;
//dim.Name = $"dim_{i}";
shape
.
Dim
.
Add
(
dim
)
;
}
return
shape
;
}
public
static
Tensor
shape_tensor
(
int
[
]
shape
)
{
return
ops
.
convert_to_tensor
(
shape
,
dtype
:
TF_DataType
.
TF_INT32
,
name
:
"shape"
)
;
}
public
static
ParsedSliceArgs
ParseSlices
(
Slice
[
]
slices
)
{
var
begin
=
new
List
<
int
>
(
)
;
var
end
=
new
List
<
int
>
(
)
;
var
strides
=
new
List
<
int
>
(
)
;
var
index
=
0
;
var
(
new_axis_mask
,
shrink_axis_mask
)
=
(
0
,
0
)
;
var
(
begin_mask
,
end_mask
)
=
(
0
,
0
)
;
var
ellipsis_mask
=
0
;
foreach
(
var
s
in
slices
)
{
if
(
s
.
IsNewAxis
)
{
begin
.
Add
(
0
)
;
end
.
Add
(
0
)
;
strides
.
Add
(
1
)
;
new_axis_mask
|=
(
1
<<
index
)
;
}
else
if
(
s
.
IsEllipsis
)
{
begin
.
Add
(
0
)
;
end
.
Add
(
0
)
;
strides
.
Add
(
1
)
;
ellipsis_mask
|=
(
1
<<
index
)
;
}
else
{
if
(
s
.
Start
.
HasValue
)
{
begin
.
Add
(
s
.
Start
.
Value
)
;
}
else
{
begin
.
Add
(
0
)
;
begin_mask
|=
(
1
<<
index
)
;
}
if
(
s
.
Stop
.
HasValue
)
{
end
.
Add
(
s
.
Stop
.
Value
)
;
}
else
{
end
.
Add
(
0
)
;
end_mask
|=
(
1
<<
index
)
;
}
strides
.
Add
(
s
.
Step
)
;
if
(
s
.
IsIndex
)
shrink_axis_mask
|=
(
1
<<
index
)
;
}
index
+=
1
;
}
return
new
ParsedSliceArgs
{
Begin
=
begin
.
ToArray
(
)
,
End
=
end
.
ToArray
(
)
,
Strides
=
strides
.
ToArray
(
)
,
BeginMask
=
begin_mask
,
EndMask
=
end_mask
,
EllipsisMask
=
ellipsis_mask
,
ShrinkAxisMask
=
shrink_axis_mask
,
NewAxisMask
=
new_axis_mask
}
;
}
public
static
ParsedSliceArgs
ParseSlices
(
Tensor
start
,
Tensor
stop
=
null
,
Tensor
step
=
null
)
{
var
begin
=
new
List
<
Tensor
>
(
)
;
var
end
=
new
List
<
Tensor
>
(
)
;
var
strides
=
new
List
<
Tensor
>
(
)
;
var
index
=
0
;
var
(
new_axis_mask
,
shrink_axis_mask
)
=
(
0
,
0
)
;
var
(
begin_mask
,
end_mask
)
=
(
0
,
0
)
;
var
ellipsis_mask
=
0
;
begin
.
Add
(
start
)
;
if
(
stop
==
null
)
end
.
Add
(
start
+
1
)
;
else
end
.
Add
(
stop
)
;
shrink_axis_mask
|=
(
1
<<
index
)
;
if
(
step
==
null
)
strides
.
Add
(
tf
.
constant
(
1
,
dtype
:
start
.
dtype
)
)
;
else
strides
.
Add
(
step
)
;
return
new
ParsedSliceArgs
{
PackedBegin
=
array_ops
.
stack
(
begin
)
,
PackedEnd
=
array_ops
.
stack
(
end
)
,
PackedStrides
=
array_ops
.
stack
(
strides
)
,
BeginMask
=
begin_mask
,
EndMask
=
end_mask
,
EllipsisMask
=
ellipsis_mask
,
ShrinkAxisMask
=
shrink_axis_mask
,
NewAxisMask
=
new_axis_mask
}
;
}
/// <summary>
/// Warning: this method is an extremely dangerous method. It directly changes the dtype inside the tensor
/// and security is not guaranteed at all. Currently this method is only used for some conditions to reuse
/// the existing memory. Any other usage should be prevented. If you are sure you want to use it when
/// developing tensorflow.net, please ask @Oceanic2018 or @AsakusaRinne first.
/// </summary>
/// <param name="handle"></param>
/// <param name="dtype"></param>
internal
static
unsafe
void
DangerousManuallySetTensorDType
(
SafeTensorHandle
handle
,
TF_DataType
dtype
)
{
long
tf_tensor_address
=
handle
.
DangerousGetHandle
(
)
.
ToInt64
(
)
;
long
interface_address
=
*
(
long
*
)
(
tf_tensor_address
)
;
long
tensor_shape_address
=
interface_address
+
8
;
long
tensor_dtype_address
=
tensor_shape_address
+
13
;
byte
*
dtype_pointer
=
(
byte
*
)
tensor_dtype_address
;
*
dtype_pointer
=
(
byte
)
dtype
;
Debug
.
Assert
(
c_api
.
TF_TensorType
(
handle
)
==
dtype
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL