FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TensorFlow.NET/src/TensorFlowNET.Core/NumPy/ShapeHelper.cs at master · 591094733/TensorFlow.NET · GitHub
591094733
/
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
/
NumPy
/
ShapeHelper.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
154 lines (132 loc) · 4.83 KB
Breadcrumbs
TensorFlow.NET
/
src
/
TensorFlowNET.Core
/
NumPy
/
ShapeHelper.cs
Copy path
File metadata and controls
154 lines (132 loc) · 4.83 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
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Linq
;
using
System
.
Text
;
namespace
Tensorflow
.
NumPy
{
public
class
ShapeHelper
{
public
static
long
GetSize
(
Shape
shape
)
{
// scalar
if
(
shape
.
ndim
==
0
)
return
1
;
var
computed
=
1L
;
for
(
int
i
=
0
;
i
<
shape
.
ndim
;
i
++
)
{
var
val
=
shape
.
dims
[
i
]
;
if
(
val
==
0
)
return
0
;
else
if
(
val
<
0
)
continue
;
computed
*=
val
;
}
return
computed
;
}
public
static
long
[
]
GetStrides
(
Shape
shape
)
{
var
strides
=
new
long
[
shape
.
ndim
]
;
if
(
shape
.
ndim
==
0
)
return
strides
;
strides
[
strides
.
Length
-
1
]
=
1
;
for
(
int
idx
=
strides
.
Length
-
1
;
idx
>=
1
;
idx
--
)
strides
[
idx
-
1
]
=
strides
[
idx
]
*
shape
.
dims
[
idx
]
;
return
strides
;
}
public
static
Shape
GetShape
(
Shape
shape1
,
params
Slice
[
]
slices
)
{
var
new_dims
=
shape1
.
dims
.
ToArray
(
)
;
slices
=
SliceHelper
.
AlignWithShape
(
shape1
,
slices
)
;
for
(
int
i
=
0
;
i
<
shape1
.
dims
.
Length
;
i
++
)
{
Slice
slice
=
slices
[
i
]
;
if
(
slice
.
Equals
(
Slice
.
All
)
)
new_dims
[
i
]
=
shape1
.
dims
[
i
]
;
else
if
(
slice
.
IsIndex
)
new_dims
[
i
]
=
1
;
else
// range
new_dims
[
i
]
=
(
slice
.
Stop
??
shape1
.
dims
[
i
]
)
-
(
slice
.
Start
??
0
)
;
}
// strip first dim if is index
var
return_dims
=
new
List
<
long
>
(
)
;
for
(
int
i
=
0
;
i
<
new_dims
.
Length
;
i
++
)
{
if
(
slices
[
i
]
.
IsIndex
)
continue
;
return_dims
.
add
(
new_dims
[
i
]
)
;
}
return
new
Shape
(
return_dims
.
ToArray
(
)
)
;
}
public
static
Shape
AlignWithShape
(
Shape
shape
,
Shape
preShape
)
{
if
(
shape
.
ndim
==
preShape
.
ndim
)
return
preShape
;
var
newShape
=
shape
.
dims
.
Select
(
x
=>
1L
)
.
ToArray
(
)
;
if
(
preShape
.
IsScalar
)
return
new
Shape
(
newShape
)
;
for
(
int
i
=
0
;
i
<
preShape
.
ndim
;
i
++
)
{
newShape
[
i
+
shape
.
ndim
-
preShape
.
ndim
]
=
preShape
[
i
]
;
}
return
new
Shape
(
newShape
)
;
}
public
static
bool
Equals
(
Shape
shape
,
object
target
)
{
if
(
shape
is
null
&&
target
is
null
)
return
true
;
else
if
(
shape
is
null
&&
target
is
not
null
)
return
false
;
else
if
(
shape
is
not
null
&&
target
is
null
)
return
false
;
switch
(
target
)
{
case
Shape
shape1
:
if
(
shape
.
ndim
==
-
1
&&
shape1
.
ndim
==
-
1
)
return
false
;
else
if
(
shape
.
ndim
!=
shape1
.
ndim
)
return
false
;
return
Enumerable
.
SequenceEqual
(
shape1
.
dims
,
shape
.
dims
)
;
case
long
[
]
shape2
:
if
(
shape
.
ndim
!=
shape2
.
Length
)
return
false
;
return
Enumerable
.
SequenceEqual
(
shape
.
dims
,
shape2
)
;
case
int
[
]
shape3
:
if
(
shape
.
ndim
!=
shape3
.
Length
)
return
false
;
return
Enumerable
.
SequenceEqual
(
shape
.
as_int_list
(
)
,
shape3
)
;
case
List
<
long
>
shape4
:
if
(
shape
.
ndim
!=
shape4
.
Count
)
return
false
;
return
Enumerable
.
SequenceEqual
(
shape
.
dims
,
shape4
)
;
case
List
<
int
>
shape5
:
if
(
shape
.
ndim
!=
shape5
.
Count
)
return
false
;
return
Enumerable
.
SequenceEqual
(
shape
.
as_int_list
(
)
,
shape5
)
;
default
:
return
false
;
}
}
public
static
string
ToString
(
Shape
shape
)
{
return
shape
.
ndim
switch
{
-
1
=>
"<unknown>"
,
0
=>
"()"
,
1
=>
$
"(
{
shape
.
dims
[
0
]
.
ToString
(
)
.
Replace
(
"-1"
,
"None"
)
}
,)"
,
_
=>
$
"(
{
string
.
Join
(
", "
,
shape
.
dims
)
.
Replace
(
"-1"
,
"None"
)
}
)"
}
;
}
public
static
long
GetOffset
(
Shape
shape
,
params
int
[
]
indices
)
{
if
(
shape
.
ndim
==
0
&&
indices
.
Length
==
1
)
return
indices
[
0
]
;
long
offset
=
0
;
var
strides
=
shape
.
strides
;
for
(
int
i
=
0
;
i
<
indices
.
Length
;
i
++
)
offset
+=
strides
[
i
]
*
indices
[
i
]
;
if
(
offset
<
0
)
throw
new
NotImplementedException
(
""
)
;
return
offset
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL