Repository navigation
fix(mnist): restore MNIST example training - #279
Open
zhangkaiyuan007 wants to merge 2 commits into
Open
zhangkaiyuan007 wants to merge 2 commits into
zhangkaiyuan007 wants to merge 2 commits into
Conversation
Since 0d2bfc7, Module::Parameters() walks submodules through NamedModules(), which calls shared_from_this(). The MNIST example built the model as a stack object, so the first Parameters() call threw std::bad_weak_ptr and the example aborted before training.
MNISTDataset computed image_size_in_bytes_ from the IDX element type (uint8), but the constructor converts the images to float32 before they are indexed. operator[] therefore created each image view at a quarter of its real byte offset, pairing labels with the wrong pixels: training loss stayed at ln(10) and test accuracy at chance (~10%).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The MNIST example on
masterdoes not train. It aborts on the firstParameters()call withstd::bad_weak_ptr, and once that is fixed, images are paired with the wrong labels, so training loss stays at ln(10) ≈ 2.30 and test accuracy at chance. This PR fixes both issues in the example only; no framework code changes.Motivation
Module::Parameters()walks submodules throughNamedModules(), which callsshared_from_this(). The example builds the model as a stack object (auto network = MNIST();), so the call throwsstd::bad_weak_ptr.MNISTDatasetcomputesimage_size_in_bytes_from the IDX element type (uint8, 1 byte), but the constructor converts the images to float32 beforeoperator[]slices them. Each image view therefore starts at a quarter of its real byte offset and no longer matches its label.Both issues were also noted in the closed training-camp PR #228, which bundled the fixes with unrelated feature work, so they never reached
master.Changes
example/mnist/main.cc: construct the model withstd::make_shared<MNIST>()and call it through the pointer.example/mnist/dataset.cc: computeimage_size_in_bytes_withsizeof(float), matching the float32 tensor thatoperator[]slices.Validation
Linux (glibc 2.44), GCC 16.2.1, CMake 4.5.1, CPU Release build (
USE_CUDA=OFF,USE_NCCL=OFF,BUILD_TEST=ON; CMake 4 additionally needs-DCMAKE_POLICY_VERSION_MINIMUM=3.5for the bundled gflags, unrelated to this PR), data from./scripts/assets/prepare-infinitrain-assets.sh mnist, run as./build/mnist --device cpu --dataset data/mnist --num_epoch 3 --lr <lr>. The DataLoader does not shuffle, so runs are reproducible.masterstd::bad_weak_ptrstd::bad_weak_ptrctest(CPU) on this branch: 310/310 passed, same asmaster(the tests do not cover the example).clang-format16.0.6--dry-run --Werroron the changed files: clean.This is a correctness fix for the example; no performance change is claimed.