-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNeuron_Object.java
More file actions
124 lines (68 loc) · 2.69 KB
/
Neuron_Object.java
File metadata and controls
124 lines (68 loc) · 2.69 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
package arkangel;
import java.util.*;
public class Neuron_Object {
static double leakiness_param = 0.08; //the value whch gets multiplied with the input value < 0 in 'leaky' ReLU function
double output; //what any neuron produces after processing an input
ArrayList<Connections> synapses; //synapses are the connections between neurons
public Neuron_Object() {
output =0;
synapses = new ArrayList<Connections>();
}
public Neuron_Object(double output) {
this.output = output;
synapses = new ArrayList<Connections>(); //synpases are connections b/w neurons
}
//this method allow a neuron to multiply all incoming inputs
//with corresponding weights and sum it all up to produce its own output
public void output_neuron_guess() {
double sum =0;
for(int i=0; i < synapses.size(); i++) {
//synapses is an ArrayList of Connection objects
Connections c = synapses.get(i);
//initializing the neuron "from" where the connection starts and
//the neuron "to" which it goes
Neuron_Object from = c.from;
Neuron_Object to = c.to;
//if the current neuron object is a "to" neuron i.e. connection going to it,
//then the weight of that connection times its input (output of the "from neuron")
//gets added to the sum i.e. which is the final output of a "to" neuron
if(this == to) {
sum+=(c.weight * from.output);
}
}
//we need the output restricted between -1 and 1 so we use a non-linear function
// to "squash" any output
output = tanh_func(sum);
}
public void hidden_neuron_guess() {
double sum =0;
for(int i=0; i < synapses.size(); i++) {
//synapses is an ArrayList of Connection objects
Connections c = synapses.get(i);
//initializing the neuron "from" where the connection starts and
//the neuron "to" which it goes
Neuron_Object from = c.from;
Neuron_Object to = c.to;
//if the current neuron object is a "to" neuron i.e. connection going to it,
//then the weight of that connection times its input (output of the "from neuron")
//gets added to the sum i.e. which is the final output of a "to" neuron
if(this == to) {
sum+=(c.weight * from.output);
}
}
output = leaky_relu_func(sum);
}
double tanh_func(double y) {
return (( Math.exp(y) - Math.exp(-y))/( Math.exp(y) + Math.exp(-y)));
}
static double leaky_relu_func(double y) {
return y >=0 ? y : y*leakiness_param;
}
//returning the array of connections between neurons
ArrayList<Connections> getConnections() {
return synapses;
}
void addConnections(Connections conn) {
synapses.add(conn); //adding a new connection
}
}