-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuron.c
160 lines (133 loc) · 3.59 KB
/
neuron.c
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
#include "neuron.h"
#include "comm.h"
void neuronInit(neuron_t *n)
{
// This doesn't need to be a function. Init with gcc command
uint8_t i;
n->potential = 0;
n->state = INTEGRATE;
n->fire_time = 0;
n->fire_potential = 0;
n->hebb_time = 0;
n->learning_state = NONE;
n->leaky_current = 0;
for (i=0;i<NUM_DENDS;i++){
n->dendrites[i].state = OFF;
n->dendrites[i].current_value = 0;
n->dendrites[i].type = EXCITATORY;
n->dendrites[i].timestamp = LEARNING_WINDOW;
n->dendrites[i].pulse_time = LEARNING_WINDOW;
n->dendrites[i].alive_time = LEARNING_WINDOW;
}
}
void checkDendrites(neuron_t * n)
{
uint8_t i;
for (i=NUM_AXONS; i<NUM_INPUTS; i++){
// check if dendrite has received a new ping
if (dendrite_ping_flag[i] != 0){
dendrite_ping_flag[i] = 0;
n->dendrite_ping_time[i] = DEND_PING_TIME;
} else if (n->dendrite_ping_time[i] == 1){
// dendrite ping has expired; reset the dendrite to inputs
setAsInput(complimentary_ports[i], complimentary_pins[i]);
active_output_pins[COMPLIMENTARY_I(i)] = 0;
}
// incremenet dendrite_ping_time
if (n->dendrite_ping_time[i] > 0){
n->dendrite_ping_time[i] -= 1;
}
// check if dendrite has received a pulse
if (dendrite_pulse_flag[i] != 0){
dendrite_pulse_flag[i] = 0;
n->dendrites[DENDRITE_I(i)].type = ((IS_EXCITATORY(i)) ? EXCITATORY : INHIBITORY);
n->dendrites[DENDRITE_I(i)].state = ON;
n->dendrites[DENDRITE_I(i)].pulse_time = 0;
}
}
for (i=0; i < NUM_DENDS; i++){
// switch dendrite off when pulse has expired
if(n->dendrites[i].state == ON){
if (n->dendrites[i].pulse_time == 0)
n->dendrites[i].timestamp = 0;
n->dendrites[i].pulse_time += 1;
if (n->dendrites[i].pulse_time >= PULSE_LENGTH){
dendriteSwitchOff(&(n->dendrites[i]));
}
}
if (n->dendrites[i].timestamp < LEARNING_WINDOW)
n->dendrites[i].timestamp++;
}
}
void calcDendriteWeightings(neuron_t * n)
{
uint8_t i;
for (i=0; i<NUM_DENDS; i++){
if (n->dendrites[i].timestamp < LEARNING_WINDOW){
n->dendrites[i].magnitude += (LEARNING_WINDOW - n->dendrites[i].timestamp) * LEARNING_CHANGE / LEARNING_WINDOW;
if (n->dendrites[i].magnitude > MAX_WEIGHTING)
n->dendrites[i].magnitude = MAX_WEIGHTING;
}
n->dendrites[i].timestamp = LEARNING_WINDOW;
}
}
void incrementHebbTime(neuron_t * n)
{
uint8_t i;
n->ms_count++;
if (n->ms_count == n->time_multiple){
n->hebb_time++;
n->ms_count = 0;
}
if (n->hebb_time == UINT16_MAX - 1){
n->hebb_time /= 2;
for (i=0; i<NUM_DENDS; i++){
n->dendrites[i].timestamp /= 2;
}
n->time_multiple *= 2;
}
}
void dendriteSwitchOff(dendrite_t *dendrite)
{
dendrite->state = OFF;
dendrite->pulse_time = 0;
switch(dendrite->type){
case EXCITATORY:
dendrite->current_value += dendrite->magnitude;
break;
case INHIBITORY:
if (dendrite->current_value > 2 * HYPERPOLARIZATION)
dendrite->current_value -= dendrite->magnitude;
break;
}
}
void dendriteDecayStep(neuron_t * n)
{
uint8_t i;
for(i=0; i<NUM_DENDS; i++){
n->dendrites[i].current_value = (n->dendrites[i].current_value * 63 ) / 64;
}
}
void membraneDecayStep(neuron_t * n)
{
n->fire_potential = (n->fire_potential * 63) / 64;
}
int16_t calcNeuronPotential(neuron_t *n)
{
uint8_t i;
int16_t new_v = 0;
for (i=0; i<NUM_DENDS; i++){
if (n->dendrites[i].state == ON){
switch(n->dendrites[i].type){
case EXCITATORY:
new_v += n->dendrites[i].magnitude;
break;
case INHIBITORY:
new_v -= n->dendrites[i].magnitude;
break;
}
}
new_v += n->dendrites[i].current_value; // each dendrite contributes its decay (*.current_value) and magnitude
}
return new_v;
}