-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuron.java
More file actions
171 lines (144 loc) · 4.22 KB
/
Neuron.java
File metadata and controls
171 lines (144 loc) · 4.22 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
import java.util.Random;
import java.util.ArrayList;
import java.io.Serializable;
public class Neuron implements Serializable{
private float bias, biasWeight;
private ArrayList<Connection> inputConnections;
private boolean activated;
private float value;
private Random r;
private int id, layer;
public Neuron(float bias, int id){
this.bias = bias;
r = new Random();
this.biasWeight = r.nextFloat();
inputConnections = new ArrayList<Connection>();
activated = false;
value = 0.0f;
this.id = id;
layer = -1;
}
public Neuron(float bias, int id, float biasWeight){
this.bias = bias;
r = new Random();
this.biasWeight = biasWeight;
inputConnections = new ArrayList<Connection>();
activated = false;
value = 0.0f;
this.id = id;
layer = -1;
}
public float sum(){
float sum = 0;
for (Connection c : inputConnections){
// System.out.println("Val: " + c.neuron.getValue() + " - weight: " + c.weight);
sum += c.neuron.getValue() * c.weight;
}
// System.out.println("BeforeBias: " + sum);
sum += bias * biasWeight;
// System.out.println("FinalSum: " + sum);
return sum;
}
public void addConnection(Neuron n){
inputConnections.add(new Connection(n));
}
public void addConnection(Neuron n, float w){
inputConnections.add(new Connection(n, w));
}
//Use for input nodes.
public void setInput(float val){
activated = true;
value = val;
// System.out.println("Setting input --> " + value);
}
public void activate(){
float d = (float) Math.pow((double) Math.exp(1.0),(double) sum());
value = (float) (1.0/(1+d));
activated = true;
}
public void feed(){
for (Connection c : inputConnections){
if (!c.neuron.activated){
// System.out.println("Feeding: " + c.neuron + " / " + c.neuron.getID());
c.neuron.feed();
}
}
activate();
}
public ArrayList<Connection> getConnections(){
return inputConnections;
}
public float getValue(){
return value;
}
public void setActivation(boolean b){
activated = b;
}
public int getID(){
return id;
}
public void setLayer(int l){
layer = l;
for (Connection c : inputConnections){
c.neuron.setLayer(layer + 1);
}
}
public int getLayer(){
return layer;
}
public void removeConnection(Connection c1){
for (Connection c2 : inputConnections){
if (c1 == c2){
inputConnections.remove(c2);
return;
}
}
}
public Neuron copy(){
// Neuron copy = new Neuron(bias, id, biasWeight);
// for (Connection c : inputConnections){
// copy.inputConnections.add(c.copy());
// }
return new Neuron(bias, id, biasWeight);
}
public void removeConnection(Neuron n){
for (Connection c : inputConnections){
if (c.neuron == n){
inputConnections.remove(c);
return;
}
}
}
public static void main(String[] args){
Neuron n0 = new Neuron(1, 0);
Neuron n1 = new Neuron(1, 1);
Neuron n2 = new Neuron(1, 2);
Neuron n3 = new Neuron(1, 3);
n0.addConnection(n1);
n0.addConnection(n2);
Neuron copy = n0.copy();
System.out.println(n0.bias + " --> " + n0.biasWeight);
System.out.println(copy.bias + " --> " + copy.biasWeight);
}
}
class Connection implements Serializable{
public Neuron neuron;
public float weight;
private Random r;
public Connection(Neuron n){
neuron = n;
r = new Random();
randomizeWeight();
}
public Connection(Neuron n, float w){
neuron = n;
weight = w;
r = new Random();
}
public void randomizeWeight(){
weight = r.nextFloat();
//yea this sucks but I am lazy at the moment.
if (r.nextFloat() > 0.5)
weight = weight * -1;
}
}