質問編集履歴
2
機体の結果が得られないことについての予測原因の記入・追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -267,3 +267,5 @@
|
|
267
267
|
###一言
|
268
268
|
|
269
269
|
自分のできる範囲で参考書を調べ、導出式などを確認してみたのですが、改善されませんでした。どうか相談に乗っていただけると助かります。
|
270
|
+
|
271
|
+
計算過程で二乗誤差が小さくならないなど計算過程に問題があるようなのですが・・
|
1
ご指摘いただいた配列の変数や要素数について修正を行いました。まだ目的の解決には至っていないのでご協力お願いします。
test
CHANGED
File without changes
|
test
CHANGED
@@ -40,238 +40,224 @@
|
|
40
40
|
|
41
41
|
|
42
42
|
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
//重みの初期値設定(乱数)
|
48
|
+
|
49
|
+
|
50
|
+
|
43
|
-
double
|
51
|
+
double default_value(){//重みを−1~1で初期設定
|
44
|
-
|
52
|
+
|
45
|
-
return
|
53
|
+
return (rand() / (double)RAND_MAX)*2-1;
|
54
|
+
|
55
|
+
|
46
56
|
|
47
57
|
}
|
48
58
|
|
49
59
|
|
50
60
|
|
61
|
+
|
62
|
+
|
63
|
+
int main() {
|
64
|
+
|
65
|
+
srand((unsigned) time(NULL));
|
66
|
+
|
67
|
+
double num = -M_PI;
|
68
|
+
|
69
|
+
FILE*fp;
|
70
|
+
|
71
|
+
fp = fopen("data.txt","w");
|
72
|
+
|
73
|
+
//教師データ
|
74
|
+
|
75
|
+
double x[50];
|
76
|
+
|
77
|
+
double y[50];
|
78
|
+
|
79
|
+
int count = 0;
|
80
|
+
|
81
|
+
//入力信号,教師データ生成
|
82
|
+
|
83
|
+
for(;num <= M_PI;num = num + (double)(2*M_PI/49)){
|
84
|
+
|
85
|
+
x[count] = num;
|
86
|
+
|
87
|
+
y[count] = sin(num);
|
88
|
+
|
89
|
+
count++;
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
|
51
|
-
//重みの初期値設定
|
95
|
+
//バイアス・重みの初期値設定
|
96
|
+
|
52
|
-
|
97
|
+
double bias[9] = {0,0,0,0,0,0,0,0,0};
|
98
|
+
|
99
|
+
double w1[9]; //入力層→隠れ層の重み
|
100
|
+
|
101
|
+
double w2[10];//隠れ層→出力層の重み(w_2[0]はバイアス)
|
102
|
+
|
103
|
+
for(count = 0;count<=9;count++){
|
104
|
+
|
105
|
+
if(count == 9){//訂正
|
106
|
+
|
107
|
+
w2[count] = default_value();
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
else{
|
112
|
+
|
113
|
+
w1[count] = default_value();
|
114
|
+
|
115
|
+
w2[count] = default_value();
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
w2[0] = 0;//バイアスの初期値は0に設定
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
double p = 0.01;//学習率
|
126
|
+
|
127
|
+
double net2[9];//隠れ層への入力
|
128
|
+
|
129
|
+
double out2[10];//出力層への出力
|
130
|
+
|
131
|
+
out2[0] = 1/(1 + exp(-1));
|
132
|
+
|
133
|
+
double net3 = 0;//出力層への入力
|
134
|
+
|
135
|
+
double out3[50];//出力信号
|
136
|
+
|
137
|
+
double E = 0;//二乗誤差
|
138
|
+
|
139
|
+
double delta3;
|
140
|
+
|
53
|
-
/
|
141
|
+
//double delta2;
|
142
|
+
|
54
|
-
|
143
|
+
//double del_E;//∂E/∂w(or ∂E/∂bias)
|
144
|
+
|
145
|
+
int i = 0;
|
146
|
+
|
147
|
+
for(count = 0;count<50;count++){//サンプルされた50点のに対するそ処理
|
148
|
+
|
149
|
+
for(int count2 = 0;count2<=9;count2++){//重みの初期値設定
|
150
|
+
|
151
|
+
if(count2 == 9){//訂正
|
152
|
+
|
153
|
+
w2[count2] = default_value();
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
else{
|
158
|
+
|
159
|
+
w1[count2] = default_value();
|
160
|
+
|
161
|
+
w2[count2] = default_value();
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
w2[0] = 0;//バイアスの初期値は0に設定
|
168
|
+
|
169
|
+
for(int learing_count=0;learing_count < ITERATIONS+1;learing_count++){//学習回数10万回についての処理,*「ITERATIONS+1」にした理由→10万回学習させた後に最終的な出力を出すため
|
170
|
+
|
55
|
-
|
171
|
+
for(i = 0;i < 9;i++){
|
172
|
+
|
56
|
-
|
173
|
+
net2[i] = w1[i]*x[count] + bias[i];
|
174
|
+
|
175
|
+
out2[i+1] = 1/(1 + exp(-net2[i]));
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
for(i=0;i < 10;i++){
|
182
|
+
|
183
|
+
net3 += w2[i]*out2[i];
|
184
|
+
|
185
|
+
}
|
186
|
+
|
57
|
-
|
187
|
+
out3[count] = 1/(1 + exp(-net3));
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
//二乗誤差
|
192
|
+
|
193
|
+
E = 0.50*(y[count] - out3[count])*(y[count] - out3[count]);
|
194
|
+
|
195
|
+
if(E == 0.0){
|
196
|
+
|
197
|
+
//printf("%d %lf\n",count,out3[count]);
|
198
|
+
|
199
|
+
break;
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
else{
|
204
|
+
|
205
|
+
//NR
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
//重み更新(出力~隠れ層間)
|
212
|
+
|
213
|
+
delta3 = (out3[count] - y[count])*out3[count]*(1-out3[count]);//(out3[count] - y[count])*exp(-net3)/((1+exp(-net3))*(1+exp(-net3)));
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
for(i = 0;i < 10;i++){//w_2の各重み更新
|
218
|
+
|
219
|
+
w2[i] = w2[i] - p*delta3*out2[i];
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
for(i = 0;i < 9;i++){
|
224
|
+
|
225
|
+
bias[i] = bias[i] - p*delta3 * w2[i+1]*out2[i+1]*(1-out2[i+1]);//bias[i] -= p*delta3 * w2[i+1]*exp(-net2[i])/((1+exp(-net2[i]))*(1+exp(-net2[i])));
|
226
|
+
|
227
|
+
w1[i] = w1[i] - p*x[count]*delta3 * w2[i+1]*out2[i+1]*(1-out2[i+1]);//w1[i] -= p*x[count]*delta3 * w2[i+1]*exp(-net2[i])/((1+exp(-net2[i]))*(1+exp(-net2[i])));
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
if(learing_count < 100){
|
232
|
+
|
233
|
+
printf("%lf %lf %lf %lf %lf %lf %lfだよ\n",E,delta3*out2[3],w1[3],w2[3],net2[3],out2[3],out3[count]);
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
for(count = 0;count<50;count++){
|
248
|
+
|
249
|
+
fprintf(fp,"%lf\t%lf\t%lf\n",x[count],y[count],out3[count]);
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
fclose(fp);
|
256
|
+
|
257
|
+
return 0;
|
58
258
|
|
59
259
|
}
|
60
260
|
|
61
|
-
double rand_normal( double mu, double sigma ){//mu:平均,sigma:標準偏差
|
62
|
-
|
63
|
-
double z=sqrt( -2.0*log(Uniform()) ) * sin( 2.0*M_PI*Uniform() );
|
64
|
-
|
65
|
-
return mu + sigma*z;
|
66
|
-
|
67
|
-
}
|
68
|
-
|
69
|
-
*/
|
70
|
-
|
71
|
-
double default_value(){//重みを−1~1で初期設定
|
72
|
-
|
73
|
-
return (rand() / (double)RAND_MAX)*2-1;
|
74
|
-
|
75
|
-
}
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
int main() {
|
82
|
-
|
83
|
-
srand((unsigned) time(NULL));
|
84
|
-
|
85
|
-
double num = -M_PI;
|
86
|
-
|
87
|
-
FILE*fp;
|
88
|
-
|
89
|
-
fp = fopen("data.txt","w");
|
90
|
-
|
91
|
-
//教師データ
|
92
|
-
|
93
|
-
double x[50];
|
94
|
-
|
95
|
-
double y[50];
|
96
|
-
|
97
|
-
int count = 0;
|
98
|
-
|
99
|
-
//入力信号,教師データ生成
|
100
|
-
|
101
|
-
for(;num <= M_PI;num = num + (double)(2*M_PI/49)){
|
102
|
-
|
103
|
-
x[count] = num;
|
104
|
-
|
105
|
-
y[count] = sin(num);
|
106
|
-
|
107
|
-
count++;
|
108
|
-
|
109
|
-
}
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
//バイアス・重みの初期値設定
|
114
|
-
|
115
|
-
double bias[9] = {0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1};//{0,0,0,0,0,0,0,0,0};
|
116
|
-
|
117
|
-
double w1[9]; //入力層→隠れ層の重み
|
118
|
-
|
119
|
-
double w2[10];//隠れ層→出力層の重み(w_2[0]はバイアス)
|
120
|
-
|
121
|
-
for(count = 0;count<=9;count++){
|
122
|
-
|
123
|
-
if(count == 10){
|
124
|
-
|
125
|
-
w2[count] = default_value();
|
126
|
-
|
127
|
-
}
|
128
|
-
|
129
|
-
else{
|
130
|
-
|
131
|
-
w1[count] = default_value();
|
132
|
-
|
133
|
-
w2[count] = default_value();
|
134
|
-
|
135
|
-
}
|
136
|
-
|
137
|
-
}
|
138
|
-
|
139
|
-
w2[0] = 0;//バイアスの初期値は0に設定
|
140
|
-
|
141
|
-
/*重みの初期値の確認
|
142
|
-
|
143
|
-
double w_11 = 0;
|
144
|
-
|
145
|
-
double w_22 = 0;
|
146
|
-
|
147
|
-
for(count = 0;count<10;count++){
|
148
|
-
|
149
|
-
w_11 += w1[count];
|
150
|
-
|
151
|
-
w_22 += w2[count];
|
152
|
-
|
153
|
-
printf("%lf と %lf\n",w1[count],w2[count]);
|
154
|
-
|
155
|
-
}
|
156
|
-
|
157
|
-
*/
|
158
|
-
|
159
|
-
double p = 0.01;//学習率
|
160
|
-
|
161
|
-
double net2[9];//隠れ層への入力
|
162
|
-
|
163
|
-
double out2[10];//出力層への出力
|
164
|
-
|
165
|
-
out2[0] = 1/(1 + exp(-1));
|
166
|
-
|
167
|
-
double net3 = 0;//出力層への入力
|
168
|
-
|
169
|
-
double out3[50];//出力信号
|
170
|
-
|
171
|
-
double E = 0;//二乗誤差
|
172
|
-
|
173
|
-
double delta3;
|
174
|
-
|
175
|
-
//double delta2;
|
176
|
-
|
177
|
-
//double del_E;//∂E/∂w(or ∂E/∂bias)
|
178
|
-
|
179
|
-
int i = 0;
|
180
|
-
|
181
|
-
for(count = 0;count<50;count++){//サンプルされた50点のに対するそ処理
|
182
|
-
|
183
|
-
for(int learing_count=0;learing_count < ITERATIONS+1;learing_count++){//学習回数10万回についての処理,*「ITERATIONS+1」にした理由→10万回学習させた後に最終的な出力を出すため
|
184
|
-
|
185
|
-
for(i = 0;i < 9;i++){
|
186
|
-
|
187
|
-
net2[i] = w1[i]*x[count] + bias[i];
|
188
|
-
|
189
|
-
out2[i+1] = 1/(1 + exp(-net2[i]));
|
190
|
-
|
191
|
-
}
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
for(i=0;i < 10;i++){
|
196
|
-
|
197
|
-
net3 += w2[i]*out2[i];
|
198
|
-
|
199
|
-
}
|
200
|
-
|
201
|
-
out3[count] = 1/(1 + exp(-net3));
|
202
|
-
|
203
|
-
if(learing_count % 10000 == 0){
|
204
|
-
|
205
|
-
printf("%lf %lf %lfだよ\n",net2[3],out2[3],out3[count]);
|
206
|
-
|
207
|
-
}
|
208
|
-
|
209
|
-
//二乗誤差
|
210
|
-
|
211
|
-
E =0.50*(y[count] - out3[count])*(y[count] - out3[count]);
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
if(E == 0.0){
|
216
|
-
|
217
|
-
//printf("%d %lf\n",count,out3[count]);
|
218
|
-
|
219
|
-
break;
|
220
|
-
|
221
|
-
}
|
222
|
-
|
223
|
-
else{
|
224
|
-
|
225
|
-
//NR
|
226
|
-
|
227
|
-
}
|
228
|
-
|
229
|
-
//重み更新(出力~隠れ層間)
|
230
|
-
|
231
|
-
delta3 = (out3[count] - y[count])*out3[count]*(1-out3[count]);//(out3[count] - y[count])*exp(-net3)/((1+exp(-net3))*(1+exp(-net3)));
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
for(i = 0;i < 10;i++){//w_2の各重み更新
|
236
|
-
|
237
|
-
w2[i] = w2[i] - p*delta3*out2[i];
|
238
|
-
|
239
|
-
}
|
240
|
-
|
241
|
-
for(i = 0;i < 9;i++){
|
242
|
-
|
243
|
-
bias[i] = bias[i] - p*delta3 * w2[i+1]*out2[count]*(1-out2[count]);//bias[i] - p*delta3 * w2[i+1]*exp(-net2[i])/((1+exp(-net2[i]))*(1+exp(-net2[i])));
|
244
|
-
|
245
|
-
w1[i] = w1[i] - p*x[count]*delta3 * w2[i+1]*out2[count]*(1-out2[count]);//w1[i] - p*x[count]*delta3 * w2[i+1]*exp(-net2[i])/((1+exp(-net2[i]))*(1+exp(-net2[i])));
|
246
|
-
|
247
|
-
}
|
248
|
-
|
249
|
-
if(learing_count % 10000 == 0){
|
250
|
-
|
251
|
-
printf("%lf %lf %lf %lf %lf\n",E,delta3,bias[3],w1[3],w2[3]);
|
252
|
-
|
253
|
-
}
|
254
|
-
|
255
|
-
}
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
for(count = 0;count<50;count++){
|
266
|
-
|
267
|
-
fprintf(fp,"%lf\t%lf\t%lf\n",x[count],y[count],out3[count]);
|
268
|
-
|
269
|
-
}
|
270
|
-
|
271
|
-
return 0;
|
272
|
-
|
273
|
-
}
|
274
|
-
|
275
261
|
```
|
276
262
|
|
277
263
|
|