質問編集履歴

2

回答のプログラムの追加

2015/12/26 13:37

投稿

jimmypage0311
jimmypage0311

スコア22

test CHANGED
File without changes
test CHANGED
@@ -112,6 +112,226 @@
112
112
 
113
113
  シグモイド関数内のthetaとsumを入れ替えると結果が合うのですが、シグモイド関数自体が1/1+exp(-αz)であるため、符号はこのままで良いと思うのですが。
114
114
 
115
+ 符号を変えると回答に近い結果が出ます。
116
+
115
117
 
116
118
 
117
119
  わかる方がいらっしゃればよろしくお願いいたします。
120
+
121
+ ```
122
+
123
+
124
+
125
+ #include <stdio.h>
126
+
127
+ #include <math.h>
128
+
129
+ #include <stdlib.h>
130
+
131
+
132
+
133
+ /* メインプログラム */
134
+
135
+ main()
136
+
137
+ {
138
+
139
+ int count_1, cycle, Num_of_cycle, Output, ideal, unit_index;
140
+
141
+ double Input[3], weight[3], theta, gain, net_input;
142
+
143
+ int probablistic_unit( double* weight, double theta, double* Input, double gain );
144
+
145
+ double sigmoid( double ney_input, double gain );
146
+
147
+
148
+
149
+ /* ゲインを1に設定する。 */
150
+
151
+ gain = 1.0;
152
+
153
+
154
+
155
+ /* 繰り返し回数を設定する。 */
156
+
157
+ Num_of_cycle = 1000;
158
+
159
+
160
+
161
+ /* 重みと閾値を設定する。 */
162
+
163
+ weight[0] = 3; weight[1] = 2; weight[2] = -1; theta = 1.0;
164
+
165
+
166
+
167
+ /* 乱数の種を設定する。 */
168
+
169
+ srand( 0 );
170
+
171
+
172
+
173
+ /* 素子に (1, 0, 1) を入力する時 */
174
+
175
+ Input[0] = 1; Input[1] = 0; Input[2] = 1;
176
+
177
+
178
+
179
+ /* 理論値 */
180
+
181
+ net_input = 0;
182
+
183
+ for ( unit_index = 0; unit_index < 3; unit_index++ )
184
+
185
+ {
186
+
187
+ net_input = net_input + weight[unit_index] * Input[unit_index];
188
+
189
+ }
190
+
191
+ net_input = net_input - theta;
192
+
193
+ ideal = (int) (Num_of_cycle * sigmoid( net_input, gain ));
194
+
195
+
196
+
197
+ /* 実験値 */
198
+
199
+ count_1 = 0;
200
+
201
+ for ( cycle = 0; cycle < Num_of_cycle; cycle++ )
202
+
203
+ {
204
+
205
+ Output = probablistic_unit(weight, theta, Input, gain);
206
+
207
+ if ( Output == 1)
208
+
209
+ count_1 = count_1 + 1;
210
+
211
+ }
212
+
213
+ printf("----- 素子に (1, 0, 1) を入力する時 ----- ¥n");
214
+
215
+ printf("素子が 1 を出力する頻度(実測値): %d / %d ¥n", count_1, Num_of_cycle);
216
+
217
+ printf("素子が 1 を出力する頻度(理論値): %d / %d ¥n", ideal, Num_of_cycle);
218
+
219
+
220
+
221
+ /* 素子に (1, -1, 0) を入力する時 */
222
+
223
+ Input[0] = 1; Input[1] = -1; Input[2] = 0;
224
+
225
+
226
+
227
+ /* 理論値 */
228
+
229
+ net_input = 0;
230
+
231
+ for ( unit_index = 0; unit_index < 3; unit_index++ )
232
+
233
+ {
234
+
235
+ net_input = net_input + weight[unit_index] * Input[unit_index];
236
+
237
+ }
238
+
239
+ net_input = net_input - theta;
240
+
241
+ ideal = (int) (Num_of_cycle * sigmoid( net_input, gain ));
242
+
243
+
244
+
245
+ /* 実験値 */
246
+
247
+ count_1 = 0;
248
+
249
+ for ( cycle = 0; cycle < Num_of_cycle; cycle++ )
250
+
251
+ {
252
+
253
+ Output = probablistic_unit(weight, theta, Input, gain);
254
+
255
+ if ( Output == 1)
256
+
257
+ count_1 = count_1 + 1;
258
+
259
+ }
260
+
261
+
262
+
263
+ printf("¥n");
264
+
265
+ printf("----- 素子に (1, -1, 0) を入力する時 ----- ¥n");
266
+
267
+ printf("素子が 1 を出力する頻度(実測値): %d / %d ¥n", count_1, Num_of_cycle);
268
+
269
+ printf("素子が 1 を出力する頻度(理論値): %d / %d ¥n", ideal, Num_of_cycle);
270
+
271
+ }
272
+
273
+
274
+
275
+ /* 素子動作を計算する関数 */
276
+
277
+ int probablistic_unit( double* weight, double theta, double* Input, double gain )
278
+
279
+ {
280
+
281
+ int unit_index;
282
+
283
+ double net_input;
284
+
285
+
286
+
287
+ net_input = 0;
288
+
289
+ for ( unit_index = 0; unit_index < 3; unit_index++ )
290
+
291
+ {
292
+
293
+ net_input = net_input + weight[unit_index] * Input[unit_index];
294
+
295
+ }
296
+
297
+ net_input = net_input - theta;
298
+
299
+
300
+
301
+ if ( rand() < sigmoid( net_input, gain ) * RAND_MAX )
302
+
303
+ return 1;
304
+
305
+ else
306
+
307
+ return 0;
308
+
309
+ }
310
+
311
+
312
+
313
+ /* シグモイド関数 */
314
+
315
+ double sigmoid( double z, double gain )
316
+
317
+ {
318
+
319
+ return 1.0 / (1.0 + exp( gain * z ));
320
+
321
+ }
322
+
323
+
324
+
325
+ ---------------------------------- プログラムの実行結果 ---------------------------------
326
+
327
+ ----- 素子に (1, 0, 1) を入力する時 -----
328
+
329
+ 素子が 1 を出力する頻度(実測値): 237 / 1000
330
+
331
+ 素子が 1 を出力する頻度(理論値): 268 / 1000
332
+
333
+
334
+
335
+ ```
336
+
337
+ 上のプログラムが回答となります。

1

ソースコードの場所の変更

2015/12/26 13:37

投稿

jimmypage0311
jimmypage0311

スコア22

test CHANGED
File without changes
test CHANGED
@@ -1,21 +1,3 @@
1
- ニューラル・ネットワークについての質問です。
2
-
3
-
4
-
5
- 以下のようなモデルのニューラル・ネットワークについてプログラムをしたのですが、回答と答えが合いません。
6
-
7
- gain:1.0、繰り返し回数:1000回
8
-
9
-
10
-
11
- ![イメージ説明](81715c32f027078f0e87b1131c27726d.jpeg)
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
1
  ```
20
2
 
21
3
  #include<iostream>
@@ -102,6 +84,24 @@
102
84
 
103
85
  ```
104
86
 
87
+
88
+
89
+ ニューラル・ネットワークについての質問です。
90
+
91
+
92
+
93
+ 以下のようなモデルのニューラル・ネットワークについてプログラムをしたのですが、回答と答えが合いません。
94
+
95
+ gain:1.0、繰り返し回数:1000回
96
+
97
+
98
+
99
+ ![イメージ説明](81715c32f027078f0e87b1131c27726d.jpeg)
100
+
101
+
102
+
103
+
104
+
105
105
  ---結果---
106
106
 
107
107
  実験値:730 理論値:731.059