-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynapse.java
65 lines (63 loc) · 2.08 KB
/
Synapse.java
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
//simulates synapse between two neurons
//uses spike timing dependent plasticity (stdp) to alter weights and delays
//basic principle:
//input neurons which fire immediately before output neuron have connection strengthened
//otherwise weakened
import java.util.Vector;
public class Synapse {
private Neuron in; //input neuron
private Neuron out; //output neuron
private Vector<Double> memory; //contains all weight updates
int inNum; //index in input neuron's synapse list
int outNum; //index in output neuron's synapse list
double weight; //multiply input by this factor
//stdp weighting constant
private double ampPos = 1;
private double ampNeg = 1;
private double timeFactor = 1;
private double scalingFactor = 0.01;
double delay; //delay introduced from neuron firing
public Synapse (Neuron A, Neuron B, double x, double y) {
//initialize weight based on inherited value
if (Math.random()>x) {
weight = 1;
}
else {
weight = 0;
}
//initialize delay based on inherited value
delay = Math.max(0,Math.random()*y);
in = A;
out = B;
A.addOutputSynapse(this);
B.addInputSynapse(this);
memory = new Vector<Double>();
}
//fire synapse
public void fire(InputKey x) {
double output = x.value*weight;
InputKey key = new InputKey(x.time+delay,output);
out.read(key,outNum);
}
//update weights based on stdp
public void reweigh (double x) {
//exponential decay function for weight
double delta;
if (x<=0) {//equals needed
delta = ampPos*Math.exp(x/timeFactor);
}
else {
delta = -ampNeg*Math.exp(-x/timeFactor);
}
memory.add(delta);
//update weight based on past values
//more recent weights are valued more
//for input neurons firing just before output, increase weight and decrease delay
for (int i = 0; i<memory.size(); i++) {
weight+=memory.get(i)*Math.pow(1/2,memory.size()-i-1)/(Math.pow(2,memory.size()));
delay-=scalingFactor*memory.get(i)*Math.pow(1/2,memory.size()-i-1)/(Math.pow(2,memory.size()));
}
if (delay<0)
delay=0;
}
}