A simple repo for beginner to train on MNIST with libtorch(cpu version)
- Download libtorch from https://pytorch.org/get-started/locally/ or
wget
the latest nightly build. unzip
your libtorch and set the environment variableYOUR_LIBTORCH_FOLDER
to the path of the unzipped folder.
For VSCode
You should set the includePath
for VSCode to search the include headers.
YOUR_LIBTORCH_FOLDER="" # fill it
LIBTORCH_INCLUDE="${YOUR_LIBTORCH_FOLDER}/include"
DEEPER_LIBTORCH="${LIBTORCH_INCLUDE}/torch/csrc/api/include"
Create .vscode/c_cpp_properties.json and set includePath. You can take the following as an example
{
"configurations": [
{
"name": "libtorch",
"includePath": [
"./libtorch/include",
"./libtorch/include/torch/csrc/api/include/"
]
}
],
"version": 4
}
There are three relative files.
- main.cpp: show simple standard training pipeline here
- net.hpp: the file where the real model hides
- utils.hpp: some utils
For simplicity, I don't divide the headers into .h in the include and .cpp in the src and compile they into .a or .so files.
You can build and run your models like:
mkdir build
cd build
cmake ..
make
cd ..
./build/mnist # It depends on what you specify your data path in your code
Without specific seed, cmd:./build/mnist 64 1e-3 128 150
could achive a test accuracy of 0.8726 with a model size of 216kB.