Skip to content

Remove redundant epoch_number variable in training tutorial (issue 3714)#3787

Open
sheralskumar wants to merge 1 commit intopytorch:mainfrom
sheralskumar:issue/3714
Open

Remove redundant epoch_number variable in training tutorial (issue 3714)#3787
sheralskumar wants to merge 1 commit intopytorch:mainfrom
sheralskumar:issue/3714

Conversation

@sheralskumar
Copy link

Fixes #3714

Description

Removed redundant epoch_number variable and used the loop variable epoch directly for logging and checkpointing. This simplifies the code, improving clarity for beginners.

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.

@pytorch-bot
Copy link

pytorch-bot bot commented Mar 2, 2026

🔗 Helpful Links

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

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

❌ 1 New Failure

As of commit e90f18e with merge base 3e6cc9b (image):

NEW FAILURE - The following job has failed:

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

@meta-cla meta-cla bot added the cla signed label Mar 2, 2026
@sekyondaMeta
Copy link
Contributor

@claude

@claude
Copy link

claude bot commented Mar 3, 2026

Claude finished @sekyondaMeta's task in 1m 33s —— View job


PR Review: #3787

Summary

This PR removes the redundant epoch_number variable from beginner_source/introyt/trainingyt.py and replaces all its usages with the epoch loop variable from for epoch in range(EPOCHS). The change is functionally equivalent and simplifies the code.

Content Quality

No concerns. This is a minor cleanup that improves clarity for beginners by eliminating a redundant variable that shadows the loop counter. Previously, epoch_number was initialized to 0 before the loop and incremented at the end of each iteration — producing the exact same sequence as range(EPOCHS). Removing it makes the code easier to follow.

Code Correctness

No concerns. I verified all four substitution sites:

Location Before After Equivalent?
trainingyt.py:286 — print statement epoch_number + 1 epoch + 1 Yes — both produce 1..5
trainingyt.py:290train_one_epoch() call epoch_number epoch Yes — both produce 0..4, used as epoch_index in the function
trainingyt.py:313add_scalars() step epoch_number + 1 epoch + 1 Yes — both produce 1..5
trainingyt.py:319 — model path epoch_number epoch Yes — both produce 0..4

The train_one_epoch function (trainingyt.py:226) receives the value as epoch_index and uses it to compute tb_x = epoch_index * len(training_loader) + i + 1 at line 255. The behavior is unchanged.

Structure & Formatting

No concerns. This is a modification to an existing file with no structural changes needed (no new tutorials, no index/toctree changes).

Build Compatibility

No concerns. The change is purely a variable renaming with identical runtime behavior. No new dependencies, data downloads, or build configuration changes.

Recommendation

Approve

Clean, correct simplification that removes unnecessary complexity from a beginner tutorial. The diff is minimal and behavior-preserving.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feedback about Training with PyTorch

2 participants