-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirr.cpp
269 lines (213 loc) · 4.97 KB
/
mirr.cpp
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include<cmath>
using namespace std;
double randuni()
{
return double(rand())/RAND_MAX;
}
int random(int n)
{
return int(randuni()*n);
}
double randn()
{
const int p = 12;
double s=0;
for(int i=0;i<p;i++)
s+=randuni();
return s-p/2;
}
void randomize()
{
srand(time(0));
}
double sigmoid(double x)
{
return 1/(1+exp(-10*(x-0.5)));
}
int main()
{
const int n = 5;
const int numF5 = n;
const int numPF = 1000*n;
const int numSTS = n;
double wF5_PF[numF5][numPF];
double wSTS_PF[numSTS][numPF];
double thPF[numPF];
double alpha = 0.01;
randomize();
for(int i=0;i<numF5;i++)
for(int j=0;j<numPF;j++)
wF5_PF[i][j] = randn();
for(int i=0;i<numSTS;i++)
for(int j=0;j<numPF;j++)
wSTS_PF[i][j] = randn();
for(int i=0;i<numPF;i++)
thPF[i] = randn();
double fatiquePF[numPF];
for(int i=0;i<numPF;i++)
fatiquePF[i] = 0;
for(int ii=0;ii<1000;ii++)
{
double sumext = 0;
double sumsum=0;
double sumfatique = 0;
double sumth = 0;
double sumw = 0;
double ew=0;
for(int what=0;what<3;what++)
for(int action=0;action<n;action++){
double exF5[numF5];
for(int i=0;i<numF5;i++)
{
double x = randn()/60; //small noise
if(i/(numF5/n) == action && what == 1) // when doing this action
x+=1;
exF5[i] = sigmoid(x);
}
double exSTS[numSTS];
for(int i=0;i<numSTS;i++)
{
double x = randn()/60; //small noise
if(i/(numSTS/n) == action && what!=0)//when doing or observing this action
x+=1;
exSTS[i] = sigmoid(x);
}
for(int pf=0;pf<numPF;pf++)
{
double sum = 0;
for(int i=0;i<numF5;i++)
sum+=wF5_PF[i][pf]*exF5[i];
for(int i=0;i<numSTS;i++)
sum+=wSTS_PF[i][pf]*exSTS[i];
sum-=thPF[pf];
double excitation = sigmoid(sum);
sumext+=excitation;
sumsum+=sum;
double sw=0;
for(int f5=0;f5<numF5;f5++)
{
wF5_PF[f5][pf]=(wF5_PF[f5][pf]+exF5[f5]*excitation*alpha);
sumw+=wF5_PF[f5][pf];
sw+=wF5_PF[f5][pf];
}
for(int sts=0;sts<numSTS;sts++)
{
wSTS_PF[sts][pf]=(wSTS_PF[sts][pf]+exSTS[sts]*excitation*alpha);
sumw+=wSTS_PF[sts][pf];
sw+=wSTS_PF[sts][pf];
}
sw/=(numF5+numSTS);
sw/=0.2;
for(int f5=0;f5<numF5;f5++)
wF5_PF[f5][pf]/=sw;
for(int sts=0;sts<numSTS;sts++)
wSTS_PF[sts][pf]/=sw;
fatiquePF[pf]*=0.95;
fatiquePF[pf]+=excitation;
thPF[pf]+=(fatiquePF[pf]-20.0/n*2/3)*alpha;
sumfatique+=fatiquePF[pf];
sumth += thPF[pf];
}
}
cerr << "ii=" << ii << "\tE(ext) = " << sumext/numPF/3/n << "\tE(sum)=" << sumsum/numPF/3/n << "\tE(fatique)="<<sumfatique/numPF/3/n << "\tE(th)="<<sumth/numPF/3/n<<"\tE(w)="<<(ew=sumw/(numF5+numSTS)/numPF/3/n) << endl;
// alpha*=0.9999;
}
double responseAction[n][numPF];
for(int action=0;action<n;action++)
{
double exF5[numF5];
for(int i=0;i<numF5;i++)
{
double x = randn()/60; //small noise
if(i/(numF5/n) == action ) // when doing this action
x+=1;
exF5[i] = sigmoid(x);
}
double exSTS[numSTS];
for(int i=0;i<numSTS;i++)
{
double x = randn()/60; //small noise
exSTS[i] = sigmoid(x);
}
double sumex=0;
for(int pf=0;pf<numPF;pf++)
{
double sum = 0;
for(int i=0;i<numF5;i++)
sum+=wF5_PF[i][pf]*exF5[i];
for(int i=0;i<numSTS;i++)
sum+=wSTS_PF[i][pf]*exSTS[i];
sum-=thPF[pf];
double excitation = sigmoid(sum);
sumex+=excitation;
responseAction[action][pf] = excitation;
}
}
double responseObservation[n][numPF];
for(int action=0;action<n;action++)
{
double exF5[numF5];
for(int i=0;i<numF5;i++)
{
double x = randn()/60; //small noise
exF5[i] = sigmoid(x);
}
double exSTS[numSTS];
for(int i=0;i<numSTS;i++)
{
double x = randn()/60; //small noise
if(i/(numSTS/n) == action)//when doing or observing this action
x+=1;
exSTS[i] = sigmoid(x);
}
double sumex=0;
for(int pf=0;pf<numPF;pf++)
{
double sum = 0;
for(int i=0;i<numF5;i++)
sum+=wF5_PF[i][pf]*exF5[i];
for(int i=0;i<numSTS;i++)
sum+=wSTS_PF[i][pf]*exSTS[i];
sum-=thPF[pf];
double excitation = sigmoid(sum);
sumex+=excitation;
responseObservation[action][pf] = excitation;
}
}
for(int pf=0;pf<numPF;pf++)
{
int count = 0;
double maxAction = -1;
double maxExcitation = 0;
for(int action=0;action<n;action++)
{
if(responseAction[action][pf] > 0.5)
count++;
if(responseAction[action][pf] > maxExcitation)
{
maxAction=action;
maxExcitation=responseAction[action][pf];
}
}
double maxObservation = -1;
maxExcitation = 0;
for(int action=0;action<n;action++)
{
if(responseObservation[action][pf] > 0.5)
count++;
if(responseObservation[action][pf] > maxExcitation)
{
maxObservation=action;
maxExcitation=responseObservation[action][pf];
}
}
cout << "pf=" << pf << "\tcountActions=" << count <<"\tmaxAction=" << maxAction << "\tmaxObservation=" << maxObservation;
if(maxAction==maxObservation && count==1)
cout << "\tmirror neuron";
cout << endl;
}
}