FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TensorFlow.NET/src/TensorFlowNET.Core/Tensors/TensorShape.cs at master · mm86133/TensorFlow.NET · GitHub
mm86133
/
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
/
TensorShape.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
282 lines (241 loc) · 10.9 KB
Breadcrumbs
TensorFlow.NET
/
src
/
TensorFlowNET.Core
/
Tensors
/
TensorShape.cs
Copy path
File metadata and controls
282 lines (241 loc) · 10.9 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
using
NumSharp
;
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Diagnostics
.
CodeAnalysis
;
using
System
.
Linq
;
using
System
.
Runtime
.
CompilerServices
;
#if
SERIALIZABLE
using
Newtonsoft
.
Json
;
#endif
using
static
Tensorflow
.
Binding
;
namespace
Tensorflow
{
/// <summary>
/// Represents the shape of a `Tensor`.
/// </summary>
/// <remarks>https://www.tensorflow.org/api_docs/python/tf/TensorShape</remarks>
public
class
TensorShape
{
private
readonly
Shape
shape
;
/// <summary>
/// Returns a list of Dimensions, or None if the shape is unspecified.
/// </summary>
public
int
[
]
dims
=>
shape
.
Dimensions
;
/// <summary>
/// Returns the rank of this shape.
/// </summary>
public
int
ndim
=>
rank
;
private
int
_rank
;
/// <summary>
/// Returns the rank of this shape.
/// </summary>
public
int
rank
=>
_rank
>
-
1
?
shape
.
NDim
:
-
1
;
/// <summary>
/// Returns the size this shape represents.
/// </summary>
#if
SERIALIZABLE
[
JsonIgnore
]
#endif
public
int
size
{
get
{
var
dims
=
shape
.
Dimensions
;
var
computed
=
1
;
for
(
int
i
=
0
;
i
<
dims
.
Length
;
i
++
)
{
var
val
=
dims
[
i
]
;
if
(
val
<=
0
)
continue
;
computed
*=
val
;
}
return
computed
;
}
}
public
TensorShape
(
)
{
_rank
=
-
1
;
shape
=
new
Shape
(
)
;
}
public
TensorShape
(
TensorShapeProto
proto
)
{
if
(
proto
.
UnknownRank
)
return
;
switch
(
proto
.
Dim
.
Count
)
{
case
0
:
shape
=
new
Shape
(
new
int
[
0
]
)
;
break
;
case
1
:
shape
=
Shape
.
Vector
(
(
int
)
proto
.
Dim
[
0
]
.
Size
)
;
break
;
case
2
:
shape
=
Shape
.
Matrix
(
(
int
)
proto
.
Dim
[
0
]
.
Size
,
(
int
)
proto
.
Dim
[
1
]
.
Size
)
;
break
;
default
:
var
protodims
=
proto
.
Dim
;
var
len
=
protodims
.
Count
;
var
dims
=
new
int
[
len
]
;
for
(
int
i
=
0
;
i
<
len
;
i
++
)
dims
[
i
]
=
(
int
)
protodims
[
i
]
.
Size
;
shape
=
new
Shape
(
dims
)
;
break
;
}
}
public
TensorShape
(
params
int
[
]
dims
)
{
switch
(
dims
.
Length
)
{
case
0
:
shape
=
new
Shape
(
new
int
[
0
]
)
;
break
;
case
1
:
shape
=
Shape
.
Vector
(
dims
[
0
]
)
;
break
;
case
2
:
shape
=
Shape
.
Matrix
(
dims
[
0
]
,
dims
[
1
]
)
;
break
;
default
:
shape
=
new
Shape
(
dims
)
;
break
;
}
}
public
TensorShape
(
int
[
]
[
]
dims
)
{
if
(
dims
.
Length
==
1
)
{
switch
(
dims
[
0
]
.
Length
)
{
case
0
:
shape
=
new
Shape
(
new
int
[
0
]
)
;
break
;
case
1
:
shape
=
Shape
.
Vector
(
(
int
)
dims
[
0
]
[
0
]
)
;
break
;
case
2
:
shape
=
Shape
.
Matrix
(
dims
[
0
]
[
0
]
,
dims
[
1
]
[
2
]
)
;
break
;
default
:
shape
=
new
Shape
(
dims
[
0
]
)
;
break
;
}
}
else
{
throw
new
NotImplementedException
(
"TensorShape int[][] dims"
)
;
}
}
/// <summary>
///
/// </summary>
/// <param name="slice"></param>
/// <returns></returns>
/// <exception cref="ArgumentException">When <see cref="Slice"/> is not an Index.</exception>
[
SuppressMessage
(
"ReSharper"
,
"PossibleInvalidOperationException"
)
]
public
TensorShape
this
[
Slice
slice
]
{
get
{
if
(
!
slice
.
Stop
.
HasValue
)
slice
.
Stop
=
dims
.
Length
-
slice
.
Start
+
1
;
if
(
slice
.
Start
.
HasValue
==
false
||
slice
.
Length
.
HasValue
==
false
)
throw
new
ArgumentException
(
"Slice must has Start and Length."
)
;
return
new
TensorShape
(
dims
.
Skip
(
slice
.
Start
.
Value
)
.
Take
(
slice
.
Length
.
Value
)
.
ToArray
(
)
)
;
}
}
/// <summary>
/// Returns True iff `self` is fully defined in every dimension.
/// </summary>
/// <returns></returns>
public
bool
is_fully_defined
(
)
{
return
rank
>
-
1
&&
dims
!=
null
&&
dims
.
Count
(
x
=>
x
<
1
)
==
0
;
}
public
bool
is_compatible_with
(
TensorShape
shape2
)
{
throw
new
NotImplementedException
(
"TensorShape is_compatible_with"
)
;
}
[
SuppressMessage
(
"ReSharper"
,
"ParameterHidesMember"
)
]
public
TensorShape
with_rank_at_least
(
int
rank
)
{
if
(
rank
!=
ndim
)
throw
new
ValueError
(
$
"Shape
{
this
}
must have rank at least
{
rank
}
"
)
;
else
return
this
;
}
public
TensorShape
with_rank
(
int
rank
)
{
return
merge_with
(
unknown_shape
(
rank
:
rank
)
)
;
}
/// <summary>
/// Returns an unknown TensorShape, optionally with a known rank.
/// </summary>
/// <param name="rank"></param>
/// <returns></returns>
public
TensorShape
unknown_shape
(
int
rank
=
-
1
)
{
if
(
rank
==
-
1
)
return
new
TensorShape
(
-
1
)
;
else
return
new
TensorShape
(
Enumerable
.
Repeat
(
-
1
,
rank
)
.
ToArray
(
)
)
;
}
/// <summary>
/// Returns the concatenation of the dimension in `self` and `other`.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
[
MethodImpl
(
MethodImplOptions
.
AggressiveInlining
)
]
public
TensorShape
concatenate
(
int
[
]
other
)
{
return
concatenate
(
new
TensorShape
(
other
)
)
;
}
/// <summary>
/// Returns the concatenation of the dimension in `self` and `other`.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public
TensorShape
concatenate
(
TensorShape
other
)
{
var
otherShape
=
other
;
if
(
ndim
<
0
||
otherShape
.
ndim
<
0
)
return
new
TensorShape
(
)
;
else
{
var
concatenate_dims
=
new
int
[
ndim
+
otherShape
.
ndim
]
;
for
(
int
i
=
0
;
i
<
ndim
;
i
++
)
concatenate_dims
[
i
]
=
dims
[
i
]
;
for
(
int
i
=
0
;
i
<
otherShape
.
ndim
;
i
++
)
concatenate_dims
[
ndim
+
i
]
=
otherShape
.
dims
[
i
]
;
return
new
TensorShape
(
concatenate_dims
)
;
}
}
/// <summary>
/// Returns a `TensorShape` combining the information in `self` and `other`.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public
TensorShape
merge_with
(
TensorShape
other
)
{
if
(
dims
==
null
)
return
other
;
var
new_dims
=
new
List
<
int
>
(
)
;
foreach
(
var
i
in
range
(
ndim
)
)
{
var
dim
=
new
Dimension
(
dims
[
i
]
)
;
var
merged
=
dim
.
merge_with
(
new
Dimension
(
other
.
dims
[
i
]
)
)
;
new_dims
.
Add
(
merged
.
value
)
;
}
return
new
TensorShape
(
new_dims
.
ToArray
(
)
)
;
}
/// <summary>
/// Returns a cloned array from <see cref="dims"/>.
/// </summary>
public
int
[
]
as_list
(
)
{
if
(
shape
.
IsEmpty
)
throw
new
ValueError
(
"as_list() is not defined on an unknown TensorShape."
)
;
return
(
int
[
]
)
dims
.
Clone
(
)
;
}
public
override
string
ToString
(
)
{
return
shape
.
ToString
(
)
;
}
public
static
implicit
operator
TensorShape
(
Shape
shape
)
=>
new
TensorShape
(
(
int
[
]
)
shape
.
Dimensions
.
Clone
(
)
)
;
public
static
implicit
operator
Shape
(
TensorShape
shape
)
=>
new
Shape
(
(
int
[
]
)
shape
.
dims
.
Clone
(
)
)
;
public
static
implicit
operator
int
[
]
(
TensorShape
shape
)
=>
(
int
[
]
)
shape
.
dims
.
Clone
(
)
;
//we clone to avoid any changes
public
static
implicit
operator
TensorShape
(
int
[
]
dims
)
=>
new
TensorShape
(
dims
)
;
public
static
explicit
operator
int
(
TensorShape
shape
)
=>
shape
.
size
;
public
static
implicit
operator
TensorShape
(
int
dim
)
=>
new
TensorShape
(
dim
)
;
public
static
explicit
operator
(
int
,
int
)
(
TensorShape
shape
)
=>
shape
.
dims
.
Length
==
2
?
(
shape
.
dims
[
0
]
,
shape
.
dims
[
1
]
)
:
(
0
,
0
)
;
public
static
implicit
operator
TensorShape
(
(
int
,
int
)
dims
)
=>
new
TensorShape
(
dims
.
Item1
,
dims
.
Item2
)
;
public
static
explicit
operator
(
int
,
int
,
int
)
(
TensorShape
shape
)
=>
shape
.
dims
.
Length
==
3
?
(
shape
.
dims
[
0
]
,
shape
.
dims
[
1
]
,
shape
.
dims
[
2
]
)
:
(
0
,
0
,
0
)
;
public
static
implicit
operator
TensorShape
(
(
int
,
int
,
int
)
dims
)
=>
new
TensorShape
(
dims
.
Item1
,
dims
.
Item2
,
dims
.
Item3
)
;
public
static
explicit
operator
(
int
,
int
,
int
,
int
)
(
TensorShape
shape
)
=>
shape
.
dims
.
Length
==
4
?
(
shape
.
dims
[
0
]
,
shape
.
dims
[
1
]
,
shape
.
dims
[
2
]
,
shape
.
dims
[
3
]
)
:
(
0
,
0
,
0
,
0
)
;
public
static
implicit
operator
TensorShape
(
(
int
,
int
,
int
,
int
)
dims
)
=>
new
TensorShape
(
dims
.
Item1
,
dims
.
Item2
,
dims
.
Item3
,
dims
.
Item4
)
;
public
static
explicit
operator
(
int
,
int
,
int
,
int
,
int
)
(
TensorShape
shape
)
=>
shape
.
dims
.
Length
==
5
?
(
shape
.
dims
[
0
]
,
shape
.
dims
[
1
]
,
shape
.
dims
[
2
]
,
shape
.
dims
[
3
]
,
shape
.
dims
[
4
]
)
:
(
0
,
0
,
0
,
0
,
0
)
;
public
static
implicit
operator
TensorShape
(
(
int
,
int
,
int
,
int
,
int
)
dims
)
=>
new
TensorShape
(
dims
.
Item1
,
dims
.
Item2
,
dims
.
Item3
,
dims
.
Item4
,
dims
.
Item5
)
;
public
static
explicit
operator
(
int
,
int
,
int
,
int
,
int
,
int
)
(
TensorShape
shape
)
=>
shape
.
dims
.
Length
==
6
?
(
shape
.
dims
[
0
]
,
shape
.
dims
[
1
]
,
shape
.
dims
[
2
]
,
shape
.
dims
[
3
]
,
shape
.
dims
[
4
]
,
shape
.
dims
[
5
]
)
:
(
0
,
0
,
0
,
0
,
0
,
0
)
;
public
static
implicit
operator
TensorShape
(
(
int
,
int
,
int
,
int
,
int
,
int
)
dims
)
=>
new
TensorShape
(
dims
.
Item1
,
dims
.
Item2
,
dims
.
Item3
,
dims
.
Item4
,
dims
.
Item5
,
dims
.
Item6
)
;
public
static
explicit
operator
(
int
,
int
,
int
,
int
,
int
,
int
,
int
)
(
TensorShape
shape
)
=>
shape
.
dims
.
Length
==
7
?
(
shape
.
dims
[
0
]
,
shape
.
dims
[
1
]
,
shape
.
dims
[
2
]
,
shape
.
dims
[
3
]
,
shape
.
dims
[
4
]
,
shape
.
dims
[
5
]
,
shape
.
dims
[
6
]
)
:
(
0
,
0
,
0
,
0
,
0
,
0
,
0
)
;
public
static
implicit
operator
TensorShape
(
(
int
,
int
,
int
,
int
,
int
,
int
,
int
)
dims
)
=>
new
TensorShape
(
dims
.
Item1
,
dims
.
Item2
,
dims
.
Item3
,
dims
.
Item4
,
dims
.
Item5
,
dims
.
Item6
,
dims
.
Item7
)
;
public
static
explicit
operator
(
int
,
int
,
int
,
int
,
int
,
int
,
int
,
int
)
(
TensorShape
shape
)
=>
shape
.
dims
.
Length
==
8
?
(
shape
.
dims
[
0
]
,
shape
.
dims
[
1
]
,
shape
.
dims
[
2
]
,
shape
.
dims
[
3
]
,
shape
.
dims
[
4
]
,
shape
.
dims
[
5
]
,
shape
.
dims
[
6
]
,
shape
.
dims
[
7
]
)
:
(
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
)
;
public
static
implicit
operator
TensorShape
(
(
int
,
int
,
int
,
int
,
int
,
int
,
int
,
int
)
dims
)
=>
new
TensorShape
(
dims
.
Item1
,
dims
.
Item2
,
dims
.
Item3
,
dims
.
Item4
,
dims
.
Item5
,
dims
.
Item6
,
dims
.
Item7
,
dims
.
Item8
)
;
}
}
Back
|
FazBrowse Home
|
New Git URL