質問編集履歴

6

やってみたことの追加

2017/05/25 06:36

投稿

AltT
AltT

スコア7

test CHANGED
File without changes
test CHANGED
@@ -84,14 +84,6 @@
84
84
 
85
85
 
86
86
 
87
- def predict_one(w, phi):
88
-
89
- score = np.dot(w, phi)
90
-
91
- return (1 if score > 0 else -1)
92
-
93
-
94
-
95
87
  def forward_nn(net, phi0):
96
88
 
97
89
  phi_list = [phi0, "*", "*"]
@@ -250,6 +242,198 @@
250
242
 
251
243
  変数の更新が行われないです
252
244
 
245
+ (追記)テストを行わずに学習だけ行うといったこと、つまり
246
+
247
+ ```
248
+
249
+ # -*- coding utf-8 -*-
250
+
251
+ from collections import defaultdict
252
+
253
+ import math
254
+
255
+ import numpy as np
256
+
257
+
258
+
259
+ def word2id(input_file):
260
+
261
+ ids = defaultdict(lambda: len(ids))
262
+
263
+ for line in input_file:
264
+
265
+ y, x = line.rstrip().split('\t')
266
+
267
+ words = x.split()
268
+
269
+ for word in words:
270
+
271
+ ids["UNI:" + word]
272
+
273
+ return ids
274
+
275
+
276
+
277
+ def word2id_test(ids, line):
278
+
279
+ words = line.rstrip().split()
280
+
281
+ for word in words:
282
+
283
+ ids["UNI:" + word]
284
+
285
+
286
+
287
+ def create_feature(x, ids):
288
+
289
+ phi = [0] * len(ids)
290
+
291
+ words = x.rstrip().split()
292
+
293
+ for word in words:
294
+
295
+ phi[ids["UNI:" + word]] += 1
296
+
297
+ return phi
298
+
299
+
300
+
301
+ def forward_nn(net, phi0):
302
+
303
+ phi_list = [phi0, "*", "*"]
304
+
305
+ for i, layer in enumerate(net):
306
+
307
+ w = layer["w"]
308
+
309
+ b = layer["b"]
310
+
311
+ phi_list[i + 1] = np.tanh(np.dot(w, phi_list[i]) + b)
312
+
313
+ return phi_list
314
+
315
+
316
+
317
+ def backward_nn(net, phi, y):
318
+
319
+ J = len(net)
320
+
321
+ delta = [0, 0, np.array([y - phi[J][0]])]
322
+
323
+ delta_prime = [0] * (J + 1)
324
+
325
+ for i in range(J - 1, 0, -1):
326
+
327
+ delta_prime[i + 1] = delta[i + 1] * (1 - pow(phi[i + 1], 2))
328
+
329
+ w = net[i]["w"]
330
+
331
+ b = net[i]["b"]
332
+
333
+ delta[i] = np.dot(delta_prime[i + 1], w)
334
+
335
+ return delta_prime
336
+
337
+
338
+
339
+ def update_weights(net, phi, delta_prime, _lambda):
340
+
341
+ for i in range(len(net) - 1):
342
+
343
+ w = net[i]["w"]
344
+
345
+ b = net[i]["b"]
346
+
347
+ w += _lambda * np.outer(delta_prime[i + 1], phi[i])
348
+
349
+ b += _lambda * delta_prime[i + 1]
350
+
351
+
352
+
353
+
354
+
355
+ if __name__ == "__main__":
356
+
357
+ input_file = open('data/titles-en-train.labeled')
358
+
359
+ test_file = open("data/titles-en-test.word")
360
+
361
+ ans_file = open('data/titles-en-test.labeled')
362
+
363
+
364
+
365
+ ids = word2id(input_file)
366
+
367
+
368
+
369
+ for line in test_file:
370
+
371
+ word2id_test(ids, line)
372
+
373
+ input_file.seek(0)
374
+
375
+ test_file.seek(0)
376
+
377
+
378
+
379
+ feat_lab = []
380
+
381
+
382
+
383
+ for line in input_file:
384
+
385
+ y, x = line.rstrip().split('\t')
386
+
387
+ phi = create_feature(x, ids)
388
+
389
+ feat_lab.append((phi, float(y)))
390
+
391
+
392
+
393
+ net = [{"w": np.random.rand(2, len(ids)) - 0.5, "b": np.random.rand(2) - 0.5}, {"w": np.random.rand(1, 2) - 0.5, "b": np.random.rand(1) - 0.5}]
394
+
395
+
396
+
397
+ print(net)
398
+
399
+
400
+
401
+ ans_file = open("data/titles-en-test.labeled")
402
+
403
+
404
+
405
+ iterations = 5
406
+
407
+
408
+
409
+ #学習する
410
+
411
+ for i in range(iterations):
412
+
413
+ c = 0
414
+
415
+ num = 0
416
+
417
+ test_file.seek(0)
418
+
419
+ ans_file.seek(0)
420
+
421
+
422
+
423
+ for phi, y in feat_lab:
424
+
425
+ phi_list = forward_nn(net, phi)
426
+
427
+ delta_prime = backward_nn(net, phi_list, y)
428
+
429
+ update_weights(net, phi, delta_prime, 0.1)
430
+
431
+ ```
432
+
433
+ のような場合はしっかりとnetの値は更新されます
434
+
435
+ そのため、このコード以外の場所に問題があると考えました
436
+
253
437
 
254
438
 
255
439
  ###補足情報(言語/FW/ツール等のバージョンなど)

5

コードの間違いがあったので修正しました

2017/05/25 06:36

投稿

AltT
AltT

スコア7

test CHANGED
File without changes
test CHANGED
@@ -220,9 +220,7 @@
220
220
 
221
221
  delta_prime = backward_nn(net, phi_list, y)
222
222
 
223
- if (phi_list[len(phi_list) - 1][0] < 0 and float(y) == 1) or (phi_list[len(phi_list) - 1][0] > 0 and float(y) == -1):
224
-
225
- update_weights(net, phi_list, delta_prime, 0.1)
223
+ update_weights(net, phi, delta_prime, 0.1)
226
224
 
227
225
 
228
226
 

4

細かい修正

2017/05/25 05:28

投稿

AltT
AltT

スコア7

test CHANGED
File without changes
test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
  一回実行してみるとよく分かります
28
28
 
29
- まずnetには各層の重みベクトル、cにはテストケースにおける二元符号{1, -1}のうちの1が出てくる回数が,numにはテストケースを実行した際の結果で0よりも大きくなる場合をカウントしています。1回目はcとnumの二つの値が各回数分変更がかかり数字が更新されます。netもupdate_weights関数によって変更が加えられ値の更新が行われます。ですが、2回目からどの変数の値も変わらず、何度やっても結果は1回目のものから変わらなくなってしまいます。
29
+ まずnetには各層の重みベクトル、cにはテストケースにおける二元符号{1, -1}のうちの1が出てくる回数が,numにはテストケースを実行した際の結果で0よりも大きくなる場合をカウントしています。1回目はcとnumの二つの値が各回数分変更がかかり数字が更新されます。netもupdate_weightsメソッドによって変更が加えられ値の更新が行われます。ですが、2回目からどの変数の値も変わらず、何度やっても結果は1回目のものから変わらなくなってしまいます。
30
30
 
31
31
 
32
32
 

3

問題箇所の説明を追加

2017/05/24 23:13

投稿

AltT
AltT

スコア7

test CHANGED
File without changes
test CHANGED
@@ -26,6 +26,10 @@
26
26
 
27
27
  一回実行してみるとよく分かります
28
28
 
29
+ まずnetには各層の重みベクトル、cにはテストケースにおける二元符号{1, -1}のうちの1が出てくる回数が,numにはテストケースを実行した際の結果で0よりも大きくなる場合をカウントしています。1回目はcとnumの二つの値が各回数分変更がかかり数字が更新されます。netもupdate_weights関数によって変更が加えられ値の更新が行われます。ですが、2回目からどの変数の値も変わらず、何度やっても結果は1回目のものから変わらなくなってしまいます。
30
+
31
+
32
+
29
33
  ```python
30
34
 
31
35
  # -*- coding utf-8 -*-
@@ -222,12 +226,6 @@
222
226
 
223
227
 
224
228
 
225
- test_file.seek(0)
226
-
227
- ans_file.seek(0)
228
-
229
-
230
-
231
229
  for line, line2 in zip(test_file, ans_file):
232
230
 
233
231
  phi = create_feature(line, ids)
@@ -236,8 +234,6 @@
236
234
 
237
235
  y, x = line2.rstrip().split('\t')
238
236
 
239
- y = float(y)
240
-
241
237
  if float(y) == 1:
242
238
 
243
239
  num += 1

2

タイトルの編集

2017/05/24 22:50

投稿

AltT
AltT

スコア7

test CHANGED
@@ -1 +1 @@
1
- pythonでのファイル読み込み、変数の値の更新が上手くいかない
1
+ pythonでのファイル読み込み時に、変数の更新が行われない
test CHANGED
File without changes

1

内容の修正

2017/05/22 01:30

投稿

AltT
AltT

スコア7

test CHANGED
File without changes
test CHANGED
@@ -22,6 +22,10 @@
22
22
 
23
23
  また、このチュートリアルには学習方法は乗っているのですがテスト方法が載っていないので自分なりの考え方でテストを行ってしまっているので間違っていたら正しい方法を教えてもらいたいです。
24
24
 
25
+ (追記)後半の方にあるc,num, netといった変数が更新されません
26
+
27
+ 一回実行してみるとよく分かります
28
+
25
29
  ```python
26
30
 
27
31
  # -*- coding utf-8 -*-