FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TensorFlow.NET/test/TensorFlowNET.UnitTest/SessionTest.cs at master · killbacon/TensorFlow.NET · GitHub
killbacon
/
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
/
SessionTest.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
204 lines (179 loc) · 7.38 KB
Breadcrumbs
TensorFlow.NET
/
test
/
TensorFlowNET.UnitTest
/
SessionTest.cs
Copy path
File metadata and controls
204 lines (179 loc) · 7.38 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
using
Microsoft
.
VisualStudio
.
TestTools
.
UnitTesting
;
using
NumSharp
;
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Reflection
;
using
System
.
Runtime
.
CompilerServices
;
using
System
.
Text
;
using
FluentAssertions
;
using
Google
.
Protobuf
;
using
NumSharp
.
Backends
;
using
Tensorflow
;
using
Tensorflow
.
Util
;
using
static
Tensorflow
.
Binding
;
namespace
TensorFlowNET
.
UnitTest
{
[
Ignore
]
[
TestClass
]
public
class
SessionTest
:
CApiTest
{
/// <summary>
/// tensorflow\c\c_api_test.cc
/// `TEST(CAPI, Session)`
/// </summary>
[
TestMethod
,
Ignore
]
public
void
Session
(
)
{
lock
(
Locks
.
ProcessWide
)
{
var
s
=
new
Status
(
)
;
var
graph
=
new
Graph
(
)
.
as_default
(
)
;
// Make a placeholder operation.
var
feed
=
c_test_util
.
Placeholder
(
graph
,
s
)
;
// Make a constant operation with the scalar "2".
var
two
=
c_test_util
.
ScalarConst
(
2
,
graph
,
s
)
;
// Add operation.
var
add
=
c_test_util
.
Add
(
feed
,
two
,
graph
,
s
)
;
var
csession
=
new
CSession
(
graph
,
s
)
;
ASSERT_EQ
(
TF_Code
.
TF_OK
,
s
.
Code
)
;
// Run the graph.
var
inputs
=
new
Dictionary
<
Operation
,
Tensor
>
(
)
;
inputs
.
Add
(
feed
,
new
Tensor
(
3
)
)
;
csession
.
SetInputs
(
inputs
)
;
var
outputs
=
new
TF_Output
[
]
{
new
TF_Output
(
add
,
0
)
}
;
csession
.
SetOutputs
(
outputs
)
;
csession
.
Run
(
s
)
;
Tensor
outTensor
=
csession
.
output_tensor
(
0
)
;
EXPECT_EQ
(
TF_DataType
.
TF_INT32
,
outTensor
.
dtype
)
;
EXPECT_EQ
(
0
,
outTensor
.
NDims
)
;
ASSERT_EQ
(
(
ulong
)
sizeof
(
uint
)
,
outTensor
.
bytesize
)
;
var
output_contents
=
outTensor
.
ToArray
<
int
>
(
)
;
EXPECT_EQ
(
3
+
2
,
output_contents
[
0
]
)
;
// Add another operation to the graph.
var
neg
=
c_test_util
.
Neg
(
add
,
graph
,
s
)
;
ASSERT_EQ
(
TF_Code
.
TF_OK
,
s
.
Code
)
;
// Run up to the new operation.
inputs
=
new
Dictionary
<
Operation
,
Tensor
>
(
)
;
inputs
.
Add
(
feed
,
new
Tensor
(
7
)
)
;
csession
.
SetInputs
(
inputs
)
;
outputs
=
new
TF_Output
[
]
{
new
TF_Output
(
neg
,
0
)
}
;
csession
.
SetOutputs
(
outputs
)
;
csession
.
Run
(
s
)
;
ASSERT_EQ
(
TF_Code
.
TF_OK
,
s
.
Code
)
;
outTensor
=
csession
.
output_tensor
(
0
)
;
ASSERT_TRUE
(
outTensor
!=
IntPtr
.
Zero
)
;
EXPECT_EQ
(
TF_DataType
.
TF_INT32
,
outTensor
.
dtype
)
;
EXPECT_EQ
(
0
,
outTensor
.
NDims
)
;
// scalar
ASSERT_EQ
(
(
ulong
)
sizeof
(
uint
)
,
outTensor
.
bytesize
)
;
output_contents
=
outTensor
.
ToArray
<
int
>
(
)
;
EXPECT_EQ
(
-
(
7
+
2
)
,
output_contents
[
0
]
)
;
// Clean up
csession
.
CloseAndDelete
(
s
)
;
ASSERT_EQ
(
TF_Code
.
TF_OK
,
s
.
Code
)
;
}
}
[
TestMethod
]
public
void
EvalTensor
(
)
{
lock
(
this
)
{
var
a
=
constant_op
.
constant
(
np
.
array
(
3.0
)
.
reshape
(
1
,
1
)
)
;
var
b
=
constant_op
.
constant
(
np
.
array
(
2.0
)
.
reshape
(
1
,
1
)
)
;
var
c
=
math_ops
.
matmul
(
a
,
b
,
name
:
"matmul"
)
;
using
(
var
sess
=
tf
.
Session
(
)
)
{
var
result
=
c
.
eval
(
sess
)
;
Assert
.
AreEqual
(
6
,
result
.
GetAtIndex
<
double
>
(
0
)
)
;
}
}
}
[
TestMethod
]
public
void
Eval_SmallString_Scalar
(
)
{
lock
(
this
)
{
var
a
=
constant_op
.
constant
(
"123 heythere 123 "
,
TF_DataType
.
TF_STRING
)
;
var
c
=
tf
.
strings
.
substr
(
a
,
4
,
8
)
;
using
(
var
sess
=
tf
.
Session
(
)
)
{
var
result
=
UTF8Encoding
.
UTF8
.
GetString
(
(
byte
[
]
)
c
.
eval
(
sess
)
)
;
Console
.
WriteLine
(
result
)
;
result
.
Should
(
)
.
Be
(
"heythere"
)
;
}
}
}
[
TestMethod
]
public
void
Eval_LargeString_Scalar
(
)
{
lock
(
this
)
{
const
int
size
=
30_000
;
var
a
=
constant_op
.
constant
(
new
string
(
'a'
,
size
)
,
TF_DataType
.
TF_STRING
)
;
var
c
=
tf
.
strings
.
substr
(
a
,
0
,
size
-
5000
)
;
using
(
var
sess
=
tf
.
Session
(
)
)
{
var
result
=
UTF8Encoding
.
UTF8
.
GetString
(
(
byte
[
]
)
c
.
eval
(
sess
)
)
;
Console
.
WriteLine
(
result
)
;
result
.
Should
(
)
.
HaveLength
(
size
-
5000
)
.
And
.
ContainAll
(
"a"
)
;
}
}
}
[
TestMethod
]
public
void
Autocast_Case0
(
)
{
var
sess
=
tf
.
Session
(
)
.
as_default
(
)
;
ITensorOrOperation
operation
=
tf
.
global_variables_initializer
(
)
;
// the cast to ITensorOrOperation is essential for the test of this method signature
var
ret
=
sess
.
run
(
operation
)
;
ret
.
Should
(
)
.
BeNull
(
)
;
}
[
TestMethod
]
public
void
Autocast_Case1
(
)
{
var
sess
=
tf
.
Session
(
)
.
as_default
(
)
;
var
input
=
tf
.
placeholder
(
tf
.
float32
,
shape
:
new
TensorShape
(
6
)
)
;
var
op
=
tf
.
reshape
(
input
,
new
int
[
]
{
2
,
3
}
)
;
sess
.
run
(
tf
.
global_variables_initializer
(
)
)
;
var
ret
=
sess
.
run
(
op
,
feed_dict
:
(
input
,
np
.
array
(
1
,
2
,
3
,
4
,
5
,
6
)
)
)
;
ret
.
Should
(
)
.
BeOfType
<
float
>
(
)
.
And
.
BeShaped
(
2
,
3
)
.
And
.
BeOfValues
(
1
,
2
,
3
,
4
,
5
,
6
)
;
print
(
ret
.
dtype
)
;
print
(
ret
)
;
}
[
TestMethod
]
public
void
Autocast_Case2
(
)
{
var
sess
=
tf
.
Session
(
)
.
as_default
(
)
;
var
input
=
tf
.
placeholder
(
tf
.
float64
,
shape
:
new
TensorShape
(
6
)
)
;
var
op
=
tf
.
reshape
(
input
,
new
int
[
]
{
2
,
3
}
)
;
sess
.
run
(
tf
.
global_variables_initializer
(
)
)
;
var
ret
=
sess
.
run
(
op
,
feed_dict
:
(
input
,
np
.
array
(
1
,
2
,
3
,
4
,
5
,
6
)
.
astype
(
NPTypeCode
.
Single
)
+
0.1f
)
)
;
ret
.
Should
(
)
.
BeOfType
<
double
>
(
)
.
And
.
BeShaped
(
2
,
3
)
.
And
.
BeOfValuesApproximately
(
0.001d
,
1.1
,
2.1
,
3.1
,
4.1
,
5.1
,
6.1
)
;
print
(
ret
.
dtype
)
;
print
(
ret
)
;
}
[
TestMethod
]
public
void
Autocast_Case3
(
)
{
var
sess
=
tf
.
Session
(
)
.
as_default
(
)
;
var
input
=
tf
.
placeholder
(
tf
.
int64
,
shape
:
new
TensorShape
(
6
)
)
;
var
op
=
tf
.
reshape
(
input
,
new
int
[
]
{
2
,
3
}
)
;
sess
.
run
(
tf
.
global_variables_initializer
(
)
)
;
var
ret
=
sess
.
run
(
op
,
feed_dict
:
(
input
,
np
.
array
(
1
,
2
,
3
,
4
,
5
,
6
)
.
astype
(
NPTypeCode
.
Single
)
+
0.1f
)
)
;
ret
.
Should
(
)
.
BeOfType
<
long
>
(
)
.
And
.
BeShaped
(
2
,
3
)
.
And
.
BeOfValues
(
1
,
2
,
3
,
4
,
5
,
6
)
;
print
(
ret
.
dtype
)
;
print
(
ret
)
;
}
[
TestMethod
]
public
void
Autocast_Case4
(
)
{
var
sess
=
tf
.
Session
(
)
.
as_default
(
)
;
var
input
=
tf
.
placeholder
(
tf
.
byte8
,
shape
:
new
TensorShape
(
6
)
)
;
var
op
=
tf
.
reshape
(
input
,
new
int
[
]
{
2
,
3
}
)
;
sess
.
run
(
tf
.
global_variables_initializer
(
)
)
;
var
ret
=
sess
.
run
(
op
,
feed_dict
:
(
input
,
np
.
array
(
1
,
2
,
3
,
4
,
5
,
6
)
.
astype
(
NPTypeCode
.
Single
)
+
0.1f
)
)
;
ret
.
Should
(
)
.
BeOfType
<
byte
>
(
)
.
And
.
BeShaped
(
2
,
3
)
.
And
.
BeOfValues
(
1
,
2
,
3
,
4
,
5
,
6
)
;
print
(
ret
.
dtype
)
;
print
(
ret
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL