pytorch / pytorch/pytorch.github.io

Incorrect code example in beginner tutorial (Build the Neural Network)

Open
#730 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
285
Forks
317
PR merge metrics
No merged PRs in 30d

Description

📚 Documentation

Hey, I was following along the beginner tutorial and attempted to run the following code sample here:
https://pytorch.org/tutorials/beginner/basics/buildmodel_tutorial.html

X = torch.rand(1, 28, 28, device=device)
logits = model(X)
pred_probab = nn.Softmax(dim=1)(logits)
y_pred = pred_probab.argmax(1)
print(f"Predicted class: {y_pred}")

Running this example gave me the following RuntimeError

RuntimeError: The size of tensor a (28) must match the size of tensor b (784) at non-singleton dimension 2

After looking up the torch.rand docs https://pytorch.org/docs/stable/generated/torch.rand.html I attempted to change the parameters to something that seemed more correct considering the input tensor to the example NeuralNetwork class.

X = torch.rand(1, 28*28, device=device)

This seems to let the code example execute as expected (by the docs).

I will note however I am a TOTAL beginner here so if this isn't the right solution then please let me know!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the code sample in the linked Build the Neural Network beginner tutorial and reproduce the reported RuntimeError. Compare the sample input shape with the NeuralNetwork example and update the tutorial so the example runs successfully without the tensor-size error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.