Skip to content

Commit 126f7ad

Browse files
committed
use label instead of category for class and remove old cmd line code-block
1 parent 52889cf commit 126f7ad

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

intermediate_source/char_rnn_classification_tutorial.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,6 @@
2323
of origin, and predict which language a name is from based on the
2424
spelling:
2525
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-
3926
Recommended Preparation
4027
=======================
4128
@@ -344,10 +331,10 @@ def forward_multi(self, line_tensor):
344331

345332
return output, hidden
346333

347-
def categoryFromOutput(self, output):
334+
def label_from_output(self, output):
348335
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
351338

352339
###########################
353340
#Now we can score the output for names!
@@ -360,7 +347,7 @@ def categoryFromOutput(self, output):
360347
input = NameData(label='none', text='Albert').tensor
361348
output, next_hidden = rnn.forward_multi(input)
362349
print(output)
363-
print(rnn.categoryFromOutput(output))
350+
print(rnn.label_from_output(output))
364351

365352
######################################################################
366353
#
@@ -426,10 +413,10 @@ def forward_multi(self, line_tensor):
426413

427414
return output, hidden
428415

429-
def categoryFromOutput(self, output):
416+
def label_from_output(self, output):
430417
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
433420

434421
def learn_single(self, label_tensor, line_tensor, learning_rate = 0.005):
435422
#Train the RNN for one example with a learning rate that defaults to 0.005.
@@ -531,9 +518,9 @@ def evaluate(rnn, testing_data):
531518
for i in range(len(testing_data)):
532519
(label_tensor, text_tensor, label, text) = testing_data[i]
533520
(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
537524

538525
# Normalize by dividing every row by its sum
539526
for i in range(len(rnn.output_labels)):
@@ -571,7 +558,7 @@ def evaluate(rnn, testing_data):
571558
# Exercises
572559
# =========
573560
#
574-
# - Try with a different dataset of line -> category, for example:
561+
# - Try with a different dataset of line -> label, for example:
575562
#
576563
# - Any word -> language
577564
# - First name -> gender

0 commit comments

Comments
 (0)