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
64 lines (57 loc) · 2.12 KB
/
Copy pathc_test_util.cs
File metadata and controls
64 lines (57 loc) · 2.12 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
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Tensorflow;
using Buffer = Tensorflow.Buffer;
namespace TensorFlowNET.UnitTest
{
public static class c_test_util
{
public static bool GetAttrValue(Operation oper, string attr_name, AttrValue attr_value, Status s)
{
var buffer = c_api.TF_NewBuffer();
return s.Code == TF_Code.TF_OK;
}
public static void PlaceholderHelper(Graph graph, Status s, string name, TF_DataType dtype, long[] dims, ref Operation op)
{
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);
}
op = c_api.TF_FinishOperation(desc, s);
s.Check();
}
public static Operation Placeholder(Graph graph, Status s, string name = "feed", TF_DataType dtype = TF_DataType.TF_INT32, long[] dims = null)
{
Operation op = null;
PlaceholderHelper(graph, s, name, dtype, dims, ref op);
return op;
}
public static void ConstHelper(Tensor t, Graph graph, Status s, string name, ref Operation op)
{
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);
op = c_api.TF_FinishOperation(desc, s);
s.Check();
if(op == null)
{
throw new Exception("c_api.TF_FinishOperation failed.");
}
}
public static Operation Const(Tensor t, Graph graph, Status s, string name)
{
Operation op = null;
ConstHelper(t, graph, s, name, ref op);
return op;
}
public static Operation ScalarConst(int v, Graph graph, Status s, string name = "Const")
{
return Const(new Tensor(v), graph, s, name);
}
}
}