-
Notifications
You must be signed in to change notification settings - Fork 109
/
ExampleCPUSingle.cs
137 lines (109 loc) · 4.09 KB
/
ExampleCPUSingle.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using ConvNetSharp.Flow;
using ConvNetSharp.Flow.Ops;
using ConvNetSharp.Flow.Serialization;
using ConvNetSharp.Flow.Training;
using ConvNetSharp.Volume;
namespace FlowDemo
{
internal static class ExampleCpuSingle
{
/// <summary>
/// Solves y = x * W + b (CPU single version)
/// for y = 1 and x = -2
///
/// This also demonstrates how to save and load a graph
/// </summary>
public static void Example1()
{
var cns = new ConvNetSharp<float>();
// Graph creation
Op<float> cost;
Op<float> fun;
if (File.Exists("test.graphml"))
{
Console.WriteLine("Loading graph from disk.");
var ops = SerializationExtensions.Load<float>("test", true);
fun = ops[0];
cost = ops[1];
}
else
{
var x = cns.PlaceHolder("x");
var y = cns.PlaceHolder("y");
var W = cns.Variable(1.0f, "W", true);
var b = cns.Variable(2.0f, "b", true);
fun = x * W + b;
cost = (fun - y) * (fun - y);
}
var optimizer = new AdamOptimizer<float>(cns, 0.01f, 0.9f, 0.999f, 1e-08f);
using (var session = new Session<float>())
{
session.Differentiate(cost); // computes dCost/dW at every node of the graph
float currentCost;
do
{
var dico = new Dictionary<string, Volume<float>> { { "x", -2.0f }, { "y", 1.0f } };
currentCost = session.Run(cost, dico);
Console.WriteLine($"cost: {currentCost}");
var result = session.Run(fun, dico);
session.Run(optimizer, dico);
} while (currentCost > 1e-5);
float finalW = session.GetVariableByName(fun, "W").Result;
float finalb = session.GetVariableByName(fun, "b").Result;
Console.WriteLine($"fun = x * {finalW} + {finalb}");
fun.Save("test", cost);
//// Display graph
//var vm = new ViewModel<float>(cost);
//var app = new Application();
//app.Run(new GraphControl { DataContext = vm });
}
Console.ReadKey();
}
/// <summary>
/// Computes and displays d(f(x))/dx = d(2x)/x
/// </summary>
public static void Example2()
{
var cns = new ConvNetSharp<float>();
// Graph creation
var x = cns.PlaceHolder("x");
var fun = 2.0f * x;
using (var session = new Session<float>())
{
session.Differentiate(fun); // computes dCost/dW at every node of the graph
//// Display graph
//var vm = new ViewModel<float>(x.Derivate);
//var app = new Application();
//app.Run(new GraphControl { DataContext = vm });
}
Console.ReadKey();
}
/// <summary>
/// Computes and displays t = t + 1
/// </summary>
public static void Example3()
{
var cns = new ConvNetSharp<float>();
// Graph creation
var t = cns.PlaceHolder("t");
var fun = cns.Assign(t, t + 1);
using (var session = new Session<float>())
{
session.InitializePlaceHolders(fun, new Dictionary<string, Volume<float>> { { "t", 1.0f } });
do
{
session.Run(fun, null);
var x = t.Result.Get(0);
Console.WriteLine(x);
} while (!Console.KeyAvailable);
//// Display graph
//var vm = new ViewModel<float>(fun);
//var app = new Application();
//app.Run(new GraphControl { DataContext = vm });
}
}
}
}