-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerceptron.cs
77 lines (68 loc) · 1.86 KB
/
Perceptron.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
using System;
using System.Collections.Generic;
using System.Text;
namespace Neuroley
{
class Perceptron
{
private float[] weights;
private float learningRate = 0.01f;
public Perceptron(int numberOfInputs, float learningRate)
{
this.learningRate = learningRate;
Initialse(numberOfInputs);
}
public Perceptron(int numberOfInputs)
{
Initialse(numberOfInputs);
}
private void Initialse(int numberOfInputs)
{
Random random = new Random();
weights = new float[numberOfInputs];
for (int i = 0; i < weights.Length; i++)
weights[i] = (float)random.NextDouble() * 2 - 1;
}
public float Guess(float[] input)
{
float output = 0;
for(int i = 0; i < input.Length; i++)
{
for(int j = 1; j < input.Length; j++)
{
output += weights[i] * input[j];
}
}
return activate(output+0.1f);
}
public void Train(float[] input, float goal)
{
float guess = Guess(input);
float error = goal-guess;
for (int i = 0; i < input.Length; i++)
{
for (int j = 1; j < input.Length; j++)
{
weights[i] += input[j]*error*learningRate;
}
}
}
public float Evaluate(float[] input, float goal)
{
float guess = Guess(input);
float error = goal - guess;
return error;
}
private int activate(float sum)
{
if(sum > 0)
{
return 1;
}
else
{
return -1;
}
}
}
}