Skip to content

Add OOV token handling to character-level RNN tutorial #3284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2025

Conversation

rice-e
Copy link
Contributor

@rice-e rice-e commented Mar 4, 2025

Improves char-rnn tutorial code quality

Description

This PR adds proper handling for Out-Of-Vocabulary (OOV) characters in the character-level RNN tutorial.

Problem:

  • The current implementation doesn't properly handle characters not in the allowed set
  • Using string.find() returns -1 for unknown characters, causing them to be treated as apostrophes (the last character in the allowed_characters string)
  • This creates ambiguity between actual apostrophes in names (like O'Brien) and unknown characters

Solution:

  • Added an underscore character "_" as a dedicated OOV token
  • Modified letterToIndex() to explicitly handle unknown characters
  • Added comments explaining the purpose of OOV handling
  • Updated the comment about input nodes (57 → 58) to reflect the added character

This change follows best practices for NLP systems by explicitly handling unknown characters, improving both the model's accuracy and the tutorial's educational value.

Checklist

  • The issue that is being fixed is referred in the description (see above "Fixes #ISSUE_NUMBER")
  • Only one issue is addressed in this pull request
  • Labels from the issue that this PR is fixing are added to this pull request
  • No unnecessary issues are included into this pull request.

cc @vmoens @nairbv @albanD @jbschlosser

Copy link

pytorch-bot bot commented Mar 4, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/tutorials/3284

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 3ae82b7 with merge base cb2e4ed (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot
Copy link
Contributor

Hi @rice-e!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@svekars svekars added the rl Issues related to reinforcement learning tutorial, DQN, and so on label Mar 4, 2025
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@svekars svekars added the core Tutorials of any level of difficulty related to the core pytorch functionality label Mar 4, 2025
@malfet malfet merged commit 057341f into pytorch:main Mar 14, 2025
20 checks passed
@rice-e rice-e deleted the fix-char-rnn-tutorial-unk-token branch March 15, 2025 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed core Tutorials of any level of difficulty related to the core pytorch functionality rl Issues related to reinforcement learning tutorial, DQN, and so on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants