FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TensorFlow.NET/test/TensorFlowNET.UnitTest/TensorTest.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
/
test
/
TensorFlowNET.UnitTest
/
TensorTest.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
277 lines (244 loc) · 10.9 KB
Breadcrumbs
TensorFlow.NET
/
test
/
TensorFlowNET.UnitTest
/
TensorTest.cs
Copy path
File metadata and controls
277 lines (244 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
using
Microsoft
.
VisualStudio
.
TestTools
.
UnitTesting
;
using
NumSharp
;
using
System
;
using
System
.
Linq
;
using
System
.
Runtime
.
InteropServices
;
using
System
.
Threading
;
using
FluentAssertions
;
using
Tensorflow
;
using
static
Tensorflow
.
Binding
;
using
Tensorflow
.
Framework
;
namespace
TensorFlowNET
.
UnitTest
{
[
TestClass
]
public
class
TensorTest
:
CApiTest
{
[
TestMethod
]
public
unsafe
void
TensorFromFixed
(
)
{
var
array
=
new
float
[
1000
]
;
var
span
=
new
Span
<
float
>
(
array
,
100
,
500
)
;
fixed
(
float
*
ptr
=
&
MemoryMarshal
.
GetReference
(
span
)
)
{
using
(
var
t
=
new
Tensor
(
(
IntPtr
)
ptr
,
new
long
[
]
{
span
.
Length
}
,
tf
.
float32
,
4
*
span
.
Length
)
)
{
Assert
.
IsFalse
(
t
.
IsDisposed
)
;
Assert
.
AreEqual
(
2000
,
(
int
)
t
.
bytesize
)
;
}
}
fixed
(
float
*
ptr
=
&
array
[
0
]
)
{
using
(
var
t
=
new
Tensor
(
(
IntPtr
)
ptr
,
new
long
[
]
{
array
.
Length
}
,
tf
.
float32
,
4
*
array
.
Length
)
)
{
Assert
.
IsFalse
(
t
.
IsDisposed
)
;
Assert
.
AreEqual
(
4000
,
(
int
)
t
.
bytesize
)
;
}
}
}
[
TestMethod
]
public
unsafe
void
TensorFromArray
(
)
{
var
array
=
new
float
[
1000
]
;
using
(
var
t
=
new
Tensor
(
array
,
new
long
[
]
{
array
.
Length
}
,
tf
.
float32
)
)
{
Assert
.
IsFalse
(
t
.
IsDisposed
)
;
Assert
.
AreEqual
(
1000
*
sizeof
(
float
)
,
(
int
)
t
.
bytesize
)
;
}
using
(
var
t
=
new
Tensor
(
new
float
[
]
{
1
}
,
new
long
[
]
{
1
}
,
tf
.
float32
)
)
{
Assert
.
IsFalse
(
t
.
IsDisposed
)
;
Assert
.
AreEqual
(
1
*
sizeof
(
float
)
,
(
int
)
t
.
bytesize
)
;
}
using
(
var
t
=
new
Tensor
(
new
float
[
]
{
1
}
,
null
,
tf
.
float32
)
)
{
Assert
.
IsFalse
(
t
.
IsDisposed
)
;
Assert
.
AreEqual
(
1
*
sizeof
(
float
)
,
(
int
)
t
.
bytesize
)
;
t
.
shape
.
Should
(
)
.
BeEmpty
(
)
;
}
}
[
TestMethod
]
public
void
AllocateTensor
(
)
{
ulong
num_bytes
=
6
*
sizeof
(
float
)
;
long
[
]
dims
=
{
2
,
3
}
;
Tensor
t
=
c_api
.
TF_AllocateTensor
(
TF_DataType
.
TF_FLOAT
,
dims
,
2
,
num_bytes
)
;
EXPECT_EQ
(
TF_DataType
.
TF_FLOAT
,
t
.
dtype
)
;
EXPECT_EQ
(
2
,
t
.
NDims
)
;
EXPECT_EQ
(
(
int
)
dims
[
0
]
,
t
.
shape
[
0
]
)
;
EXPECT_EQ
(
num_bytes
,
t
.
bytesize
)
;
t
.
Dispose
(
)
;
}
/// <summary>
/// Port from c_api_test.cc
/// `TEST(CAPI, MaybeMove)`
/// </summary>
[
TestMethod
]
public
void
MaybeMove
(
)
{
NDArray
nd
=
np
.
array
(
2
,
3
)
;
Tensor
t
=
new
Tensor
(
nd
)
;
Tensor
o
=
t
.
MaybeMove
(
)
;
ASSERT_TRUE
(
o
==
IntPtr
.
Zero
)
;
// It is unsafe to move memory TF might not own.
t
.
Dispose
(
)
;
}
/// <summary>
/// Port from c_api_test.cc
/// `TEST(CAPI, Tensor)`
/// </summary>
[
TestMethod
]
public
void
Tensor
(
)
{
var
nd
=
np
.
array
(
1f
,
2f
,
3f
,
4f
,
5f
,
6f
)
.
reshape
(
2
,
3
)
;
var
tensor
=
new
Tensor
(
nd
)
;
var
array
=
tensor
.
ToArray
<
float
>
(
)
;
EXPECT_EQ
(
tensor
.
dtype
,
TF_DataType
.
TF_FLOAT
)
;
EXPECT_EQ
(
tensor
.
rank
,
nd
.
ndim
)
;
EXPECT_EQ
(
(
int
)
tensor
.
shape
[
0
]
,
nd
.
shape
[
0
]
)
;
EXPECT_EQ
(
(
int
)
tensor
.
shape
[
1
]
,
nd
.
shape
[
1
]
)
;
EXPECT_EQ
(
tensor
.
bytesize
,
(
ulong
)
nd
.
size
*
sizeof
(
float
)
)
;
Assert
.
IsTrue
(
Enumerable
.
SequenceEqual
(
nd
.
Data
<
float
>
(
)
,
new
float
[
]
{
1
,
2
,
3
,
4
,
5
,
6
}
)
)
;
}
/// <summary>
/// Port from tensorflow\c\c_api_test.cc
/// `TEST(CAPI, SetShape)`
/// </summary>
[
TestMethod
]
public
void
SetShape
(
)
{
var
s
=
new
Status
(
)
;
var
graph
=
new
Graph
(
)
.
as_default
(
)
;
var
feed
=
c_test_util
.
Placeholder
(
graph
,
s
)
;
var
feed_out_0
=
new
TF_Output
(
feed
,
0
)
;
// Fetch the shape, it should be completely unknown.
int
num_dims
=
c_api
.
TF_GraphGetTensorNumDims
(
graph
,
feed_out_0
,
s
)
;
Assert
.
IsTrue
(
s
.
Code
==
TF_Code
.
TF_OK
)
;
EXPECT_EQ
(
-
1
,
num_dims
)
;
// Set the shape to be unknown, expect no change.
c_api
.
TF_GraphSetTensorShape
(
graph
,
feed_out_0
,
null
,
-
1
,
s
)
;
Assert
.
IsTrue
(
s
.
Code
==
TF_Code
.
TF_OK
)
;
num_dims
=
c_api
.
TF_GraphGetTensorNumDims
(
graph
,
feed_out_0
,
s
)
;
EXPECT_EQ
(
-
1
,
num_dims
)
;
// Set the shape to be 2 x Unknown
long
[
]
dims
=
{
2
,
-
1
}
;
c_api
.
TF_GraphSetTensorShape
(
graph
,
feed_out_0
,
dims
,
dims
.
Length
,
s
)
;
Assert
.
IsTrue
(
s
.
Code
==
TF_Code
.
TF_OK
)
;
num_dims
=
c_api
.
TF_GraphGetTensorNumDims
(
graph
,
feed_out_0
,
s
)
;
EXPECT_EQ
(
2
,
num_dims
)
;
// Get the dimension vector appropriately.
var
returned_dims
=
new
long
[
dims
.
Length
]
;
c_api
.
TF_GraphGetTensorShape
(
graph
,
feed_out_0
,
returned_dims
,
num_dims
,
s
)
;
Assert
.
IsTrue
(
s
.
Code
==
TF_Code
.
TF_OK
)
;
Assert
.
IsTrue
(
Enumerable
.
SequenceEqual
(
dims
,
returned_dims
)
)
;
// Set to a new valid shape: [2, 3]
dims
[
1
]
=
3
;
c_api
.
TF_GraphSetTensorShape
(
graph
,
feed_out_0
,
dims
,
dims
.
Length
,
s
)
;
Assert
.
IsTrue
(
s
.
Code
==
TF_Code
.
TF_OK
)
;
// Fetch and see that the new value is returned.
c_api
.
TF_GraphGetTensorShape
(
graph
,
feed_out_0
,
returned_dims
,
num_dims
,
s
)
;
Assert
.
IsTrue
(
s
.
Code
==
TF_Code
.
TF_OK
)
;
Assert
.
IsTrue
(
Enumerable
.
SequenceEqual
(
dims
,
returned_dims
)
)
;
// Try to set 'unknown' with unknown rank on the shape and see that
// it doesn't change.
c_api
.
TF_GraphSetTensorShape
(
graph
,
feed_out_0
,
null
,
-
1
,
s
)
;
Assert
.
IsTrue
(
s
.
Code
==
TF_Code
.
TF_OK
)
;
c_api
.
TF_GraphGetTensorShape
(
graph
,
feed_out_0
,
returned_dims
,
num_dims
,
s
)
;
Assert
.
IsTrue
(
s
.
Code
==
TF_Code
.
TF_OK
)
;
EXPECT_EQ
(
2
,
num_dims
)
;
EXPECT_EQ
(
2
,
(
int
)
returned_dims
[
0
]
)
;
EXPECT_EQ
(
3
,
(
int
)
returned_dims
[
1
]
)
;
// Try to set 'unknown' with same rank on the shape and see that
// it doesn't change.
dims
[
0
]
=
-
1
;
dims
[
1
]
=
-
1
;
c_api
.
TF_GraphSetTensorShape
(
graph
,
feed_out_0
,
dims
,
2
,
s
)
;
Assert
.
IsTrue
(
s
.
Code
==
TF_Code
.
TF_OK
)
;
c_api
.
TF_GraphGetTensorShape
(
graph
,
feed_out_0
,
returned_dims
,
num_dims
,
s
)
;
Assert
.
IsTrue
(
s
.
Code
==
TF_Code
.
TF_OK
)
;
EXPECT_EQ
(
2
,
num_dims
)
;
EXPECT_EQ
(
2
,
(
int
)
returned_dims
[
0
]
)
;
EXPECT_EQ
(
3
,
(
int
)
returned_dims
[
1
]
)
;
// Try to fetch a shape with the wrong num_dims
c_api
.
TF_GraphGetTensorShape
(
graph
,
feed_out_0
,
returned_dims
,
5
,
s
)
;
Assert
.
IsTrue
(
s
.
Code
==
TF_Code
.
TF_INVALID_ARGUMENT
)
;
// Try to set an invalid shape (cannot change 2x3 to a 2x5).
dims
[
1
]
=
5
;
c_api
.
TF_GraphSetTensorShape
(
graph
,
feed_out_0
,
dims
,
2
,
s
)
;
Assert
.
IsTrue
(
s
.
Code
==
TF_Code
.
TF_INVALID_ARGUMENT
)
;
// Test for a scalar.
var
three
=
c_test_util
.
ScalarConst
(
3
,
graph
,
s
)
;
Assert
.
IsTrue
(
s
.
Code
==
TF_Code
.
TF_OK
)
;
var
three_out_0
=
new
TF_Output
(
three
,
0
)
;
num_dims
=
c_api
.
TF_GraphGetTensorNumDims
(
graph
,
three_out_0
,
s
)
;
Assert
.
IsTrue
(
s
.
Code
==
TF_Code
.
TF_OK
)
;
EXPECT_EQ
(
0
,
num_dims
)
;
c_api
.
TF_GraphGetTensorShape
(
graph
,
feed_out_0
,
null
,
num_dims
,
s
)
;
//Assert.IsTrue(s.Code == TF_Code.TF_OK);
// graph.Dispose();
s
.
Dispose
(
)
;
}
[
TestMethod
]
public
void
sparse_to_dense
(
)
{
var
indices
=
tf
.
reshape
(
tf
.
range
(
0
,
5
)
,
new
int
[
]
{
5
,
1
}
)
;
var
labels
=
tf
.
expand_dims
(
tf
.
constant
(
new
[
]
{
0
,
1
,
2
,
3
,
4
}
)
,
1
)
;
var
st
=
tf
.
concat
(
values
:
new
[
]
{
indices
,
labels
}
,
axis
:
1
)
;
var
onehot
=
tf
.
sparse_to_dense
(
st
,
(
5
,
5
)
,
1
)
;
using
(
var
sess
=
tf
.
Session
(
)
)
{
var
result
=
sess
.
run
(
onehot
)
;
Assert
.
IsTrue
(
Enumerable
.
SequenceEqual
(
new
int
[
]
{
1
,
0
,
0
,
0
,
0
}
,
result
[
0
]
.
ToArray
<
int
>
(
)
)
)
;
Assert
.
IsTrue
(
Enumerable
.
SequenceEqual
(
new
int
[
]
{
0
,
1
,
0
,
0
,
0
}
,
result
[
1
]
.
ToArray
<
int
>
(
)
)
)
;
Assert
.
IsTrue
(
Enumerable
.
SequenceEqual
(
new
int
[
]
{
0
,
0
,
1
,
0
,
0
}
,
result
[
2
]
.
ToArray
<
int
>
(
)
)
)
;
Assert
.
IsTrue
(
Enumerable
.
SequenceEqual
(
new
int
[
]
{
0
,
0
,
0
,
1
,
0
}
,
result
[
3
]
.
ToArray
<
int
>
(
)
)
)
;
Assert
.
IsTrue
(
Enumerable
.
SequenceEqual
(
new
int
[
]
{
0
,
0
,
0
,
0
,
1
}
,
result
[
4
]
.
ToArray
<
int
>
(
)
)
)
;
}
;
}
[
TestMethod
]
public
void
sparse_tensor_to_dense
(
)
{
var
decoded_list
=
tf
.
SparseTensor
(
new
[
,
]
{
{
0L
,
0L
}
,
{
1L
,
2L
}
}
,
new
int
[
]
{
1
,
2
}
,
new
[
]
{
3L
,
4L
}
)
;
var
onehot
=
tf
.
sparse_tensor_to_dense
(
decoded_list
)
;
using
(
var
sess
=
tf
.
Session
(
)
)
{
var
result
=
sess
.
run
(
onehot
)
;
Assert
.
IsTrue
(
Enumerable
.
SequenceEqual
(
new
int
[
]
{
1
,
0
,
0
,
0
}
,
result
[
0
]
.
ToArray
<
int
>
(
)
)
)
;
Assert
.
IsTrue
(
Enumerable
.
SequenceEqual
(
new
int
[
]
{
0
,
0
,
2
,
0
}
,
result
[
1
]
.
ToArray
<
int
>
(
)
)
)
;
Assert
.
IsTrue
(
Enumerable
.
SequenceEqual
(
new
int
[
]
{
0
,
0
,
0
,
0
}
,
result
[
2
]
.
ToArray
<
int
>
(
)
)
)
;
}
}
[
TestMethod
]
public
void
batch_to_space_nd
(
)
{
var
inputs
=
np
.
arange
(
24
)
.
reshape
(
4
,
2
,
3
)
;
var
block_shape
=
new
[
]
{
2
,
2
}
;
int
[
,
]
crops
=
{
{
0
,
0
}
,
{
0
,
0
}
}
;
var
tensor
=
tf
.
batch_to_space_nd
(
inputs
,
block_shape
,
crops
)
;
using
(
var
sess
=
tf
.
Session
(
)
)
{
var
result
=
sess
.
run
(
tensor
)
;
Assert
.
IsTrue
(
Enumerable
.
SequenceEqual
(
new
int
[
]
{
0
,
6
,
1
,
7
,
2
,
8
}
,
result
[
0
,
0
]
.
ToArray
<
int
>
(
)
)
)
;
Assert
.
IsTrue
(
Enumerable
.
SequenceEqual
(
new
int
[
]
{
12
,
18
,
13
,
19
,
14
,
20
}
,
result
[
0
,
1
]
.
ToArray
<
int
>
(
)
)
)
;
Assert
.
IsTrue
(
Enumerable
.
SequenceEqual
(
new
int
[
]
{
3
,
9
,
4
,
10
,
5
,
11
}
,
result
[
0
,
2
]
.
ToArray
<
int
>
(
)
)
)
;
Assert
.
IsTrue
(
Enumerable
.
SequenceEqual
(
new
int
[
]
{
15
,
21
,
16
,
22
,
17
,
23
}
,
result
[
0
,
3
]
.
ToArray
<
int
>
(
)
)
)
;
}
}
[
TestMethod
]
public
void
boolean_mask
(
)
{
var
tensor
=
new
[
]
{
0
,
1
,
2
,
3
}
;
var
mask
=
np
.
array
(
new
[
]
{
true
,
false
,
true
,
false
}
)
;
var
masked
=
tf
.
boolean_mask
(
tensor
,
mask
)
;
using
(
var
sess
=
tf
.
Session
(
)
)
{
var
result
=
sess
.
run
(
masked
)
;
Assert
.
IsTrue
(
Enumerable
.
SequenceEqual
(
new
int
[
]
{
0
,
2
}
,
result
.
ToArray
<
int
>
(
)
)
)
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL