forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc_test_util.cs
More file actions
208 lines (187 loc) · 6.31 KB
/
c_test_util.cs
File metadata and controls
208 lines (187 loc) · 6.31 KB
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
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Tensorflow;
using Buffer = Tensorflow.Buffer;
namespace TensorFlowNET.UnitTest
{
/// <summary>
/// Port from `tensorflow\c\c_test_util.cc`
/// </summary>
public static class c_test_util
{
public static Operation Add(Operation l, Operation r, Graph graph, Status s, string name = "add")
{
var desc = c_api.TF_NewOperation(graph, "AddN", name);
var inputs = new TF_Output[]
{
new TF_Output(l, 0),
new TF_Output(r, 0),
};
c_api.TF_AddInputList(desc, inputs, inputs.Length);
var op = c_api.TF_FinishOperation(desc, s);
s.Check();
return op;
}
public static bool GetAttrValue(Operation oper, string attr_name, ref AttrValue attr_value, Status s)
{
var buffer = new Buffer();
c_api.TF_OperationGetAttrValueProto(oper, attr_name, buffer, s);
attr_value = AttrValue.Parser.ParseFrom(buffer);
buffer.Dispose();
return s.Code == TF_Code.TF_OK;
}
public static GraphDef GetGraphDef(Graph graph)
{
var s = new Status();
var buffer = new Buffer();
c_api.TF_GraphToGraphDef(graph, buffer, s);
s.Check();
var def = GraphDef.Parser.ParseFrom(buffer);
buffer.Dispose();
s.Dispose();
return def;
}
public static bool IsAddN(NodeDef node_def, int n)
{
if (node_def.Op != "AddN" || node_def.Name != "add" ||
node_def.Input.Count != n)
{
return false;
}
bool found_t = false;
bool found_n = false;
foreach (var attr in node_def.Attr)
{
if (attr.Key == "T")
{
if (attr.Value.Type == DataType.DtInt32)
{
found_t = true;
}
else
{
return false;
}
}
else if (attr.Key == "N")
{
if (attr.Value.I == n)
{
found_n = true;
}
else
{
return false;
}
}
}
return found_t && found_n;
}
public static bool IsNeg(NodeDef node_def, string input)
{
return node_def.Op == "Neg" && node_def.Name == "neg" &&
node_def.Input.Count == 1 && node_def.Input[0] == input;
}
public static bool IsPlaceholder(NodeDef node_def)
{
if (node_def.Op != "Placeholder" || node_def.Name != "feed")
{
return false;
}
bool found_dtype = false;
bool found_shape = false;
foreach (var attr in node_def.Attr)
{
if (attr.Key == "dtype")
{
if (attr.Value.Type == DataType.DtInt32)
{
found_dtype = true;
}
else
{
return false;
}
}
else if (attr.Key == "shape")
{
found_shape = true;
}
}
return found_dtype && found_shape;
}
public static bool IsScalarConst(NodeDef node_def, int v)
{
if (node_def.Op != "Const" || node_def.Name != "scalar")
{
return false;
}
bool found_dtype = false;
bool found_value = false;
foreach (var attr in node_def.Attr) {
if (attr.Key == "dtype")
{
if (attr.Value.Type == DataType.DtInt32)
{
found_dtype = true;
}
else
{
return false;
}
}
else if (attr.Key == "value")
{
if (attr.Value.Tensor != null &&
attr.Value.Tensor.IntVal.Count == 1 &&
attr.Value.Tensor.IntVal[0] == v)
{
found_value = true;
}
else
{
return false;
}
}
}
return found_dtype && found_value;
}
public static Operation Neg(Operation n, Graph graph, Status s, string name = "neg")
{
OperationDescription desc = c_api.TF_NewOperation(graph, "Neg", name);
var neg_input = new TF_Output(n, 0);
c_api.TF_AddInput(desc, neg_input);
var op = c_api.TF_FinishOperation(desc, s);
s.Check();
return op;
}
public static Operation Placeholder(Graph graph, Status s, string name = "feed", TF_DataType dtype = TF_DataType.TF_INT32, long[] dims = null)
{
var desc = c_api.TF_NewOperation(graph, "Placeholder", name);
c_api.TF_SetAttrType(desc, "dtype", dtype);
if (dims != null)
{
c_api.TF_SetAttrShape(desc, "shape", dims, dims.Length);
}
var op = c_api.TF_FinishOperation(desc, s);
s.Check();
return op;
}
public static Operation Const(Tensor t, Graph graph, Status s, string name)
{
var desc = c_api.TF_NewOperation(graph, "Const", name);
c_api.TF_SetAttrTensor(desc, "value", t, s);
s.Check();
c_api.TF_SetAttrType(desc, "dtype", t.dtype);
var op = c_api.TF_FinishOperation(desc, s);
s.Check();
return op;
}
public static Operation ScalarConst(int v, Graph graph, Status s, string name = "scalar")
{
return Const(new Tensor(v), graph, s, name);
}
}
}