-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathNet.cs
More file actions
226 lines (185 loc) · 7.25 KB
/
Net.cs
File metadata and controls
226 lines (185 loc) · 7.25 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using System;
using System.Collections.Generic;
using System.IO;
using ConvNetSharp.Core.Layers;
using ConvNetSharp.Volume;
namespace ConvNetSharp.Core
{
public class Net<T> : INet<T> where T : struct, IEquatable<T>, IFormattable
{
public List<LayerBase<T>> Layers { get; } = new List<LayerBase<T>>();
public Volume<T> Forward(Volume<T> input, bool isTraining = false)
{
var activation = this.Layers[0].DoForward(input, isTraining);
for (var i = 1; i < this.Layers.Count; i++)
{
var layer = this.Layers[i];
activation = layer.DoForward(activation, isTraining);
}
return activation;
}
public T GetCostLoss(Volume<T> input, Volume<T> y)
{
this.Forward(input);
var n = this.Layers.Count;
if (this.Layers[n - 1] is ILastLayer<T> lastLayer)
{
lastLayer.Backward(y, out var loss);
return loss;
}
throw new Exception("Last layer doesn't implement ILastLayer interface");
}
public T Backward(Volume<T> y)
{
var n = this.Layers.Count;
if (!(this.Layers[n - 1] is ILastLayer<T> lastLayer))
{
throw new Exception("Last layer doesn't implement ILastLayer interface");
}
lastLayer.Backward(y, out var loss); // last layer assumed to be loss layer
for (var i = n - 2; i >= 0; i--)
{
// first layer assumed input
this.Layers[i].Backward(this.Layers[i + 1].InputActivationGradients);
}
return loss;
}
public int[] GetPrediction()
{
// this is a convenience function for returning the argmax
// prediction, assuming the last layer of the net is a softmax
var ln = this.Layers.Count;
if (!(this.Layers[ln - 1] is SoftmaxLayer<T> softmaxLayer))
{
throw new Exception("GetPrediction function assumes softmax as last layer of the net!");
}
var activation = softmaxLayer.OutputActivation;
var N = activation.Shape.Dimensions[3];
var C = activation.Shape.Dimensions[2];
var result = new int[N];
for (var n = 0; n < N; n++)
{
var maxv = activation.Get(0, 0, 0, n);
var maxi = 0;
for (var i = 1; i < C; i++)
{
var output = activation.Get(0, 0, i, n);
if (Ops<T>.GreaterThan(output, maxv))
{
maxv = output;
maxi = i;
}
}
result[n] = maxi;
}
return result;
}
public List<ParametersAndGradients<T>> GetParametersAndGradients()
{
var response = new List<ParametersAndGradients<T>>();
foreach (var t in this.Layers)
{
var parametersAndGradients = t.GetParametersAndGradients();
response.AddRange(parametersAndGradients);
}
return response;
}
public void AddLayer(LayerBase<T> layer)
{
int inputWidth = 0, inputHeight = 0, inputDepth = 0;
LayerBase<T> lastLayer = null;
if (this.Layers.Count > 0)
{
var n = this.Layers.Count;
inputWidth = this.Layers[n - 1].OutputWidth;
inputHeight = this.Layers[n - 1].OutputHeight;
inputDepth = this.Layers[n - 1].OutputDepth;
lastLayer = this.Layers[n - 1];
}
else if (!(layer is InputLayer<T>))
{
throw new ArgumentException("First layer should be an InputLayer");
}
if (layer is IClassificationLayer classificationLayer)
{
if (!(lastLayer is FullyConnLayer<T> fullconLayer))
{
throw new ArgumentException(
$"Previously added layer should be a FullyConnLayer with {classificationLayer.ClassCount} Neurons");
}
if (fullconLayer.NeuronCount != classificationLayer.ClassCount)
{
throw new ArgumentException(
$"Previous FullyConnLayer should have {classificationLayer.ClassCount} Neurons");
}
}
if (layer is ReluLayer<T> || layer is LeakyReluLayer<T>)
{
if (lastLayer is IDotProductLayer<T> dotProductLayer)
{
// relus like a bit of positive bias to get gradients early
// otherwise it's technically possible that a relu unit will never turn on (by chance)
// and will never get any gradient and never contribute any computation. Dead relu.
dotProductLayer.BiasPref = (T)Convert.ChangeType(0.1, typeof(T)); // can we do better?
}
}
if (this.Layers.Count > 0)
{
layer.Init(inputWidth, inputHeight, inputDepth);
}
this.Layers.Add(layer);
}
public void Dump(string filename)
{
using var stream = File.Create(filename);
using var sw = new StreamWriter(stream);
for (var index = 0; index < this.Layers.Count; index++)
{
var layerBase = this.Layers[index];
sw.WriteLine($"=== Layer {index}");
sw.WriteLine("Input");
sw.Write(layerBase.InputActivation.ToString());
if (layerBase is ConvLayer<T> conv)
{
sw.WriteLine("Filter");
sw.Write(conv.Filters.ToString());
sw.WriteLine("Bias");
sw.Write(conv.Bias.ToString());
}
if (layerBase is FullyConnLayer<T> full)
{
sw.WriteLine("Filter");
sw.Write(full.Filters.ToString());
sw.WriteLine("Bias");
sw.Write(full.Bias.ToString());
}
}
}
public Volume<T> Forward(Volume<T>[] inputs, bool isTraining = false)
{
return this.Forward(inputs[0], isTraining);
}
public static Net<T> FromData(IDictionary<string, object> dico)
{
var net = new Net<T>();
var layers = dico["Layers"] as IEnumerable<IDictionary<string, object>>;
foreach (var layerData in layers)
{
var layer = LayerBase<T>.FromData(layerData);
net.Layers.Add(layer);
}
return net;
}
public Dictionary<string, object> GetData()
{
var dico = new Dictionary<string, object>();
var layers = new List<Dictionary<string, object>>();
foreach (var layer in this.Layers)
{
layers.Add(layer.GetData());
}
dico["Layers"] = layers;
return dico;
}
}
}