Skip to content

Commit

Permalink
Address Err in char_rnn tutorial issue (#2374)
Browse files Browse the repository at this point in the history
* address bug; do a little editing
Signed-off-by: Mike Brown <brownwm@us.ibm.com>
* Update intermediate_source/char_rnn_classification_tutorial.py

Signed-off-by: Mike Brown <brownwm@us.ibm.com>
Co-authored-by: Svetlana Karslioglu <svekars@fb.com>
  • Loading branch information
mikebrow and Svetlana Karslioglu authored Jun 1, 2023
1 parent d078756 commit 56a2faf
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions intermediate_source/char_rnn_classification_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
**************************************************************
**Author**: `Sean Robertson <https://github.com/spro>`_
We will be building and training a basic character-level RNN to classify
words. This tutorial, along with the following two, show how to do
preprocess data for NLP modeling "from scratch", in particular not using
many of the convenience functions of `torchtext`, so you can see how
preprocessing for NLP modeling works at a low level.
We will be building and training a basic character-level Recurrent Neural
Network (RNN) to classify words. This tutorial, along with two other
Natural Language Processing (NLP) "from scratch" tutorials
:doc:`/intermediate/char_rnn_generation_tutorial` and
:doc:`/intermediate/seq2seq_translation_tutorial`, show how to
preprocess data to model NLP. In particular these tutorials do not
use many of the convenience functions of `torchtext`, so you can see how
preprocessing to model NLP works at a low level.
A character-level RNN reads words as a series of characters -
outputting a prediction and "hidden state" at each step, feeding its
Expand All @@ -32,13 +35,15 @@
(-2.68) Dutch
**Recommended Reading:**
Recommended Preparation
=======================
I assume you have at least installed PyTorch, know Python, and
understand Tensors:
Before starting this tutorial it is recommended that you have installed PyTorch,
and have a basic understanding of Python programming language and Tensors:
- https://pytorch.org/ For installation instructions
- :doc:`/beginner/deep_learning_60min_blitz` to get started with PyTorch in general
and learn the basics of Tensors
- :doc:`/beginner/pytorch_with_examples` for a wide and deep overview
- :doc:`/beginner/former_torchies_tutorial` if you are former Lua Torch user
Expand Down Expand Up @@ -181,10 +186,6 @@ def lineToTensor(line):
# is just 2 linear layers which operate on an input and hidden state, with
# a ``LogSoftmax`` layer after the output.
#
# .. figure:: https://i.imgur.com/Z2xbySO.png
# :alt:
#
#

import torch.nn as nn

Expand All @@ -195,13 +196,13 @@ def __init__(self, input_size, hidden_size, output_size):
self.hidden_size = hidden_size

self.i2h = nn.Linear(input_size + hidden_size, hidden_size)
self.i2o = nn.Linear(input_size + hidden_size, output_size)
self.h2o = nn.Linear(hidden_size, output_size)
self.softmax = nn.LogSoftmax(dim=1)

def forward(self, input, hidden):
combined = torch.cat((input, hidden), 1)
hidden = self.i2h(combined)
output = self.i2o(combined)
output = self.h2o(hidden)
output = self.softmax(output)
return output, hidden

Expand Down

0 comments on commit 56a2faf

Please sign in to comment.