23
23
of origin, and predict which language a name is from based on the
24
24
spelling:
25
25
26
- .. code-block:: sh
27
-
28
- $ python predict.py Hinton
29
- (-0.47) Scottish
30
- (-1.52) English
31
- (-3.57) Irish
32
-
33
- $ python predict.py Schmidhuber
34
- (-0.19) German
35
- (-2.48) Czech
36
- (-2.68) Dutch
37
-
38
-
39
26
Recommended Preparation
40
27
=======================
41
28
@@ -344,10 +331,10 @@ def forward_multi(self, line_tensor):
344
331
345
332
return output , hidden
346
333
347
- def categoryFromOutput (self , output ):
334
+ def label_from_output (self , output ):
348
335
top_n , top_i = output .topk (1 )
349
- category_i = top_i [0 ].item ()
350
- return self .output_labels [category_i ], category_i
336
+ label_i = top_i [0 ].item ()
337
+ return self .output_labels [label_i ], label_i
351
338
352
339
###########################
353
340
#Now we can score the output for names!
@@ -360,7 +347,7 @@ def categoryFromOutput(self, output):
360
347
input = NameData (label = 'none' , text = 'Albert' ).tensor
361
348
output , next_hidden = rnn .forward_multi (input )
362
349
print (output )
363
- print (rnn .categoryFromOutput (output ))
350
+ print (rnn .label_from_output (output ))
364
351
365
352
######################################################################
366
353
#
@@ -426,10 +413,10 @@ def forward_multi(self, line_tensor):
426
413
427
414
return output , hidden
428
415
429
- def categoryFromOutput (self , output ):
416
+ def label_from_output (self , output ):
430
417
top_n , top_i = output .topk (1 )
431
- category_i = top_i [0 ].item ()
432
- return self .output_labels [category_i ], category_i
418
+ label_i = top_i [0 ].item ()
419
+ return self .output_labels [label_i ], label_i
433
420
434
421
def learn_single (self , label_tensor , line_tensor , learning_rate = 0.005 ):
435
422
#Train the RNN for one example with a learning rate that defaults to 0.005.
@@ -531,9 +518,9 @@ def evaluate(rnn, testing_data):
531
518
for i in range (len (testing_data )):
532
519
(label_tensor , text_tensor , label , text ) = testing_data [i ]
533
520
(output , hidden ) = rnn .forward_multi (text_tensor )
534
- guess , guess_i = rnn .categoryFromOutput (output )
535
- category_i = rnn .output_labels .index (label )
536
- confusion [category_i ][guess_i ] += 1
521
+ guess , guess_i = rnn .label_from_output (output )
522
+ label_i = rnn .output_labels .index (label )
523
+ confusion [label_i ][guess_i ] += 1
537
524
538
525
# Normalize by dividing every row by its sum
539
526
for i in range (len (rnn .output_labels )):
@@ -571,7 +558,7 @@ def evaluate(rnn, testing_data):
571
558
# Exercises
572
559
# =========
573
560
#
574
- # - Try with a different dataset of line -> category , for example:
561
+ # - Try with a different dataset of line -> label , for example:
575
562
#
576
563
# - Any word -> language
577
564
# - First name -> gender
0 commit comments