Artificial Neural Network

This is a project I have done at university as a homework. Application calculates multiplication table with a artificial neural network. Network could be trained as in supervised training by giving fact values. Well, people interested in this subject would understand the work flow of the application, but it is not considered as a tutorial for newbies. There is a ANN class in files which could be handy if you are willing to create a solution based on ANN computing. neural_network class should be improved to be used in a production environment. Also sigmoid resulting operator works as expected but other operators will fail to work because they added to project in last minute and have a wrong implementation. It is a basic feed forward network. Input, output, hidden layer count and processor counts in hidden layers are independent, so programmer can decide how many of them should be in runtime. Error back propagation integrated into network. Network can be started with random weights on connections which of their values will be between -1 and 1, but never exactly -1,0 or 1.

I know there is not enough information neither on source code or on this post, but feel free to use and study it anyway. You are free to use neural_network class on your project. But if you improve it I would like to hear about it. Also if you like feel free to give a comment or reference.

This code is not designed to be used in production environment, it is not subject to thread-safe test or critical application test.

Multiplication Table Calculator

Multiplication Table Calculator

You can download full source code and project files here. Project was implemented on C#, Visual Studio 2008 (Express version will work fine :D ).

And here is the neural_network class:

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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace MultNetwork
{
    public class synaptic
    {
        public double error_rate;
        private double previous_weight;
        private double weight;
        private neuron home;
        private neuron target;
        public synaptic(neuron connection_to_home, double weight, neuron connection_to_target)
        {
            this.home = connection_to_home;
            this.target = connection_to_target;
            this.weight = weight;
            this.error_rate = 1;
            this.previous_weight = weight;
        }
        public synaptic(neuron connection_to_home, neuron connection_to_target)
        {
            this.home = connection_to_home;
            this.target = connection_to_target;
            this.error_rate = 1;
            this.previous_weight = weight;
            this.randomize(new Random());
        }
        public neuron get_target()
        {
            return this.target;
        }
        public neuron get_home()
        {
            return this.home;
        }
        public double get_weight()
        {
            return this.weight;
        }
        public void set_weight(double weight, bool save_old_value)
        {
            if (save_old_value)
                this.previous_weight = this.weight;
            this.weight = weight;
        }
        public double get_previous_weight()
        {
            return this.previous_weight;
        }
        public void set_weight_via_error_rate(double error_rate)
        {
            this.error_rate = error_rate;
            this.set_weight(this.get_weight() + this.error_rate, true);
        }
        public void randomize(Random random)
        {
            this.previous_weight = this.weight;
            if(random == null)
                random = new Random();
            double random_weight = 0;
            while ((random_weight == -1) || (random_weight == 0) || (random_weight == 1))
            {
                random_weight = (random.NextDouble() * 2) - 1;
            }
            this.weight = random_weight;
        }
    }
    public class neuron
    {
        private bool linear_output;
        private bool step_output;
        private bool sigmoid_output;
        private bool threshold;
        private double S;
        private double output_value = 0;
        private List connections = new List();
        public double input_value = 0;
        public neuron()
        {
            this.threshold = false;
            this.sigmoid_output = true;
            this.linear_output = false;
            this.step_output = false;
        }
        public neuron(bool threshold)
        {
            if (threshold)
            {
                this.threshold = true;
                this.input_value = 1;
                this.output_value = 1;
            }
            this.sigmoid_output = true;
            this.linear_output = false;
            this.step_output = false;
        }
        public void set_linear_output()
        {
            this.sigmoid_output = false;
            this.linear_output = true;
            this.step_output = false;
        }
        public void set_step_output()
        {
            this.sigmoid_output = false;
            this.linear_output = false;
            this.step_output = true;
        }
        public void set_sigmoid_output()
        {
            this.sigmoid_output = true;
            this.linear_output = false;
            this.step_output = false;
        }
        public void clear_connections()
        {
            this.connections.Clear();
        }
        public void connect(neuron target)
        {
            this.connections.Add(new synaptic(this, target));
        }
        private void calculate_output_value()
        {
            if (this.threshold)
            {
                this.output_value = 1;
            }
            else
            {
                if (this.sigmoid_output)
                {
                    this.output_value = 1 / (1 + Math.Pow(Math.E, -1 * this.input_value));
                }
                if (this.step_output)
                {
                    if (this.input_value > 0)
                    {
                        this.output_value = 1;
                    }
                    else
                    {
                        this.output_value = 0;
                    }
                }
                if (this.linear_output)
                {
                    this.output_value = this.input_value;
                }
            }
        }
        public void calculate(bool direct_pass)
        {
            if (direct_pass)
                this.output_value = this.input_value;
            else
                this.calculate_output_value();
            for (int i = 0; i < this.connections.Count(); i++)
                this.connections[i].get_target().input_value += this.connections[i].get_weight() * this.output_value;
        }
        public double get_output()
        {
            return this.output_value;
        }
        public void set_S_via_error_rate(double error_rate)
        {
            this.S = this.output_value * (1 - this.output_value) * error_rate;
        }
        private void calculate_synaptics_errors(double lambda, double alfa)
        {
            for (int i = 0; i < this.connections.Count(); i++)
                this.connections[i].set_weight_via_error_rate(lambda * this.connections[i].get_target().get_S() * this.output_value + alfa * this.connections[i].error_rate);
        }
        private void calculate_S()
        {
            double temp_S = 0;
            for (int i = 0; i < this.connections.Count(); i++)
                temp_S += this.connections[i].get_previous_weight() * this.connections[i].get_target().get_S();
            temp_S *= this.get_output() * (1 - this.get_output());
            this.S = temp_S;
        }
        public void calculate_error(double lambda, double alfa)
        {
            this.calculate_synaptics_errors(lambda, alfa);
            this.calculate_S();
        }
        public double get_S()
        {
            return this.S;
        }
        public void randomize(Random random)
        {
            if (random == null)
                random = new Random();
            for (int i = 0; i < this.connections.Count(); i++)
                this.connections[i].randomize(random);
        }
    }
    public class neuron_layer
    {
        public List neurons = new List();
        private neuron threshold;
        public void set_step_output()
        {
            for (int i = 0; i < this.neurons.Count(); i++)
                this.neurons[i].set_step_output();
        }
        public void set_linear_output()
        {
            for (int i = 0; i < this.neurons.Count(); i++)
                this.neurons[i].set_linear_output();
        }
        public void set_sigmoid_output()
        {
            for (int i = 0; i < this.neurons.Count(); i++)
                this.neurons[i].set_sigmoid_output();
        }
        public void set_neuron_count(int count, bool threshold)
        {
            this.neurons.Clear();
            for (uint i = 0; i < count; i++)
                this.neurons.Add(new neuron());
            if (threshold)
                this.threshold = new neuron(true);
            else
                this.threshold = null;
        }
        public int get_neuron_count()
        {
            return this.neurons.Count();
        }
        public void connect_to_layer(neuron_layer layer)
        {
            for (int i = 0; i < this.neurons.Count(); i++)
            {
                this.neurons[i].clear_connections();
                for (int j = 0; j < layer.neurons.Count(); j++)
                    this.neurons[i].connect(layer.neurons[j]);
            }
            if (this.threshold != null)
                for (int j = 0; j < layer.neurons.Count(); j++)
                    this.threshold.connect(layer.neurons[j]);
        }
        public void set_input_value(int identifier, double value)
        {
            if (identifier < this.neurons.Count())
                this.neurons[identifier].input_value = value;
        }
        public double get_output_value(int identifier)
        {
            if (identifier < this.neurons.Count())
                return this.neurons[identifier].get_output();
            else
                return 0;
        }
        public void calculate(bool direct_pass)
        {
            for (int i = 0; i < this.neurons.Count(); i++)
                this.neurons[i].calculate(direct_pass);
            if (this.threshold != null)
                this.threshold.calculate(direct_pass);
        }
        public void clear_input_values()
        {
            for (int i = 0; i < this.neurons.Count(); i++)
                this.neurons[i].input_value = 0;
        }
        public void set_S_via_error_rate(double error_rate)
        {
            for (int i = 0; i < this.neurons.Count(); i++)
                this.neurons[i].set_S_via_error_rate(error_rate);
        }
        public void calculate_error(double lambda, double alfa)
        {
            for (int i = 0; i < this.neurons.Count(); i++)
                this.neurons[i].calculate_error(lambda, alfa);
            if (this.threshold != null)
                this.threshold.calculate_error(lambda, alfa);
        }
        public void randomize(Random random)
        {
            if (random == null)
                random = new Random();
            for (int i = 0; i < this.neurons.Count(); i++)
                this.neurons[i].randomize(random);
            if (this.threshold != null)
                this.threshold.randomize(random);
        }
    }
    public class neural_network
    {
        private neuron_layer input;
        private List processors = new List();
        private neuron_layer output;
        public void set_step_output()
        {
            this.input.set_step_output();
            for (int i = 0; i < this.processors.Count(); i++)
                this.processors[i].set_step_output();
            this.output.set_step_output();
        }
        public void set_sigmoid_output()
        {
            this.input.set_sigmoid_output();
            for (int i = 0; i < this.processors.Count(); i++)
                this.processors[i].set_sigmoid_output();
            this.output.set_sigmoid_output();
        }
        public void set_linear_output()
        {
            this.input.set_linear_output();
            for (int i = 0; i < this.processors.Count(); i++)
                this.processors[i].set_linear_output();
            this.output.set_linear_output();
        }
        public void set_input_count(int count)
        {
            if (count < 1)
                return;
            this.input = new neuron_layer();
            this.input.set_neuron_count(count, true);
        }
        public void set_output_count(int count)
        {
            if (count < 1)
                return;
            this.output = new neuron_layer();
            this.output.set_neuron_count(count, false);
        }
        public void set_proc_layer_depth(int depth)
        {
            if (depth < 1)
                return;
            this.processors.Clear();
            for (uint i = 0; i < depth; i++)                 this.processors.Add(new neuron_layer());         }         public void set_proc_layer_count(int depth, int count)         {             if ((depth > this.processors.Count()) || (count < 1))
                return;
            this.processors[depth].set_neuron_count(count, true);
        }
        public void connect()
        {
            this.input.connect_to_layer(this.processors[0]);
            int proc_count = this.processors.Count();
            for (int i = 0; i < proc_count - 1; i++)
                this.processors[i].connect_to_layer(this.processors[i + 1]);
            this.processors[proc_count - 1].connect_to_layer(this.output);
        }
        public void set_inputs(List inputs)
        {
            if (inputs.Count.Equals(this.input.get_neuron_count()))
                for (int i = 0; i < inputs.Count(); i++)
                    this.input.set_input_value(i, inputs[i]);
        }
        public void calculate()
        {
            this.output.clear_input_values();
            for (int i = 0; i < this.processors.Count(); i++)
                this.processors[i].clear_input_values();
            this.input.calculate(true);
            for (int i = 0; i < this.processors.Count(); i++)
                this.processors[i].calculate(false);
            this.output.calculate(false);
        }
        public List get_outputs()
        {
            List result = new List();
            for (int i = 0; i < this.output.get_neuron_count(); i++)                 result.Add(this.output.get_output_value(i));             return result;         }         public void calculate_error(double lambda, double alfa)         {             for (int i = this.processors.Count() - 1; i >= 0; i--)
                this.processors[i].calculate_error(lambda, alfa);
            this.input.calculate_error(lambda, alfa);
        }
        public void set_output_error_rate(int identifier, double error_rate)
        {
            if (identifier < this.output.neurons.Count())
                this.output.neurons[identifier].set_S_via_error_rate(error_rate);
        }
        public void train(List inputs, List outputs, double lambda, double alfa)
        {
            if (!this.input.neurons.Count.Equals(inputs.Count()))
                return;
            if (!this.output.neurons.Count.Equals(outputs.Count()))
                return;
            this.set_inputs(inputs);
            this.calculate();
            List calculated_outputs = this.get_outputs();
            for (int i = 0; i < outputs.Count(); i++)
            {
                this.set_output_error_rate(i, outputs[i] - calculated_outputs[i]);
            }
            this.calculate_error(lambda, alfa);
        }
        public void randomize()
        {
            Random random = new Random();
            this.input.randomize(random);
            int proc_count = this.processors.Count();
            for (int i = 0; i < proc_count; i++)
                this.processors[i].randomize(random);
        }
    }
}
No comments yet.
No trackbacks yet.