Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 539
Expand file tree
/
Copy pathAutoGraph.cs
More file actions
Latest commit
83 lines (72 loc) · 2.65 KB
/
Copy pathAutoGraph.cs
File metadata and controls
83 lines (72 loc) · 2.65 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
usingSystem;
usingSystem.Diagnostics;
usingSystem.Linq;
usingstaticTensorflow.Binding;
namespaceTensorflow.Graphs
{
publicclassAutoGraph
{
publicFunc<Tensor,Tensor>to_graph(Func<Tensor,Tensor>func,TF_DataTypedtype=TF_DataType.TF_INT32)
{
stringfunc_name=$"{func.Method.Name}_{ops.uid_function()}";
vargraph=newFuncGraph(func_name);
graph.as_default();
varinput=tf.placeholder(dtype);
varoutput=func(input);
varopers=graph._nodes_by_name.Values.Select(x =>xasOperation).ToArray();
graph.ToGraph(opers,
new[]{input},
new[]{output},
null);
graph.Exit();
return(Tensorinput)=>
{
if(tf.executing_eagerly())
{
varresult=tf.Runner.TFE_Execute(tf.Context,
tf.Context.DeviceName,
func_name,
new[]{input},
null,
1);
returnresult[0];
}
vars=tf.Session(input.graph);
varoutput=func(input);
returnoutput;
};
}
publicFunc<Tensor,Tensor,Tensor>to_graph(Func<Tensor,Tensor,Tensor>func,paramsTF_DataType[]dtypes)
{
stringfunc_name=$"{func.Method.Name}_{ops.uid_function()}";
vargraph=newFuncGraph(func_name);
graph.as_default();
varinput1=tf.placeholder(dtypes.Length>=1?dtypes[0]:tf.int32);
varinput2=tf.placeholder(dtypes.Length>=2?dtypes[1]:tf.int32);
varoutput=func(input1,input2);
varopers=graph._nodes_by_name.Values.Select(x =>xasOperation).ToArray();
graph.ToGraph(opers,
new[]{input1,input2},
new[]{output},
null);
graph.Exit();
return(Tensora,Tensorb)=>
{
if(tf.executing_eagerly())
{
varresult=tf.Runner.TFE_Execute(tf.Context,
tf.Context.DeviceName,
func_name,
new[]{a,b},
null,
1);
returnresult[0];
}
vars=tf.Session(a.graph);
Debug.Assert(a.graph==b.graph);
varoutput=func(a,b);
returnoutput;
};
}
}
}