{"id":13471226,"url":"https://github.com/prabhuomkar/pytorch-cpp","last_synced_at":"2025-10-04T19:09:49.735Z","repository":{"id":42136393,"uuid":"219796516","full_name":"prabhuomkar/pytorch-cpp","owner":"prabhuomkar","description":"C++ Implementation of PyTorch Tutorials for Everyone","archived":false,"fork":false,"pushed_at":"2025-08-25T05:12:15.000Z","size":518,"stargazers_count":2075,"open_issues_count":3,"forks_count":273,"subscribers_count":53,"default_branch":"master","last_synced_at":"2025-08-25T07:53:50.595Z","etag":null,"topics":["artificial-intelligence","autograd","colab","convolutional-neural-network","cplusplus","datasets","generative-adversarial-network","interactive-tutorials","language-model","libtorch","machine-learning","neural-network","pytorch","recurrent-neural-network","scriptmodule-files","tensors","torch","tutorial"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prabhuomkar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["prabhuomkar","mfl28"]}},"created_at":"2019-11-05T16:52:01.000Z","updated_at":"2025-08-24T14:39:22.000Z","dependencies_parsed_at":"2023-02-01T21:00:38.495Z","dependency_job_id":"a5649fec-3652-4775-9c7f-a73ae7976eec","html_url":"https://github.com/prabhuomkar/pytorch-cpp","commit_stats":{"total_commits":104,"total_committers":6,"mean_commits":"17.333333333333332","dds":0.5,"last_synced_commit":"511775f16e16ee1a76c5e64b6df92499af8495ea"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/prabhuomkar/pytorch-cpp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prabhuomkar%2Fpytorch-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prabhuomkar%2Fpytorch-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prabhuomkar%2Fpytorch-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prabhuomkar%2Fpytorch-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prabhuomkar","download_url":"https://codeload.github.com/prabhuomkar/pytorch-cpp/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prabhuomkar%2Fpytorch-cpp/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278359763,"owners_count":25974173,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-04T02:00:05.491Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["artificial-intelligence","autograd","colab","convolutional-neural-network","cplusplus","datasets","generative-adversarial-network","interactive-tutorials","language-model","libtorch","machine-learning","neural-network","pytorch","recurrent-neural-network","scriptmodule-files","tensors","torch","tutorial"],"created_at":"2024-07-31T16:00:41.857Z","updated_at":"2025-10-04T19:09:49.711Z","avatar_url":"https://github.com/prabhuomkar.png","language":"C++","readme":"\u003ch1 align=\"center\"\u003e\n    \u003cimg src=\"images/pytorch_cpp.jpg\" width=\"50%\"\u003e\n\u003c/h1\u003e\n\u003cp align=\"center\"\u003e\n    C++ Implementation of PyTorch Tutorials for Everyone\n    \u003cbr /\u003e\n\u003cimg src=\"https://img.shields.io/github/license/prabhuomkar/pytorch-cpp\"\u003e\n\u003cimg src=\"https://img.shields.io/badge/libtorch-2.8.0-ee4c2c\"\u003e\n\u003cimg src=\"https://img.shields.io/badge/cmake-3.28.6-064f8d\"\u003e\n\u003c/p\u003e\n\n\n| OS (Compiler)\\\\LibTorch |                                                  2.8.0                                                |\n| :--------------------- | :--------------------------------------------------------------------------------------------------- |\n|    macOS (clang 15, 16)    | [![Status](https://github.com/prabhuomkar/pytorch-cpp/actions/workflows/build_macos.yml/badge.svg?branch=master)](https://github.com/prabhuomkar/pytorch-cpp/actions?query=workflow%3Aci-build-macos) |\n|      Linux (gcc 13, 14)      | [![Status](https://github.com/prabhuomkar/pytorch-cpp/actions/workflows/build_ubuntu.yml/badge.svg?branch=master)](https://github.com/prabhuomkar/pytorch-cpp/actions?query=workflow%3Aci-build-ubuntu) |\n|    Windows (msvc 2022, 2025)  | [![Status](https://github.com/prabhuomkar/pytorch-cpp/actions/workflows/build_windows.yml/badge.svg?branch=master)](https://github.com/prabhuomkar/pytorch-cpp/actions?query=workflow%3Aci-build-windows) |\n\n## Table of Contents\n\nThis repository provides tutorial code in C++ for deep learning researchers to learn PyTorch _(i.e. Section 1 to 3)_  \n**Python Tutorial**: [https://github.com/yunjey/pytorch-tutorial](https://github.com/yunjey/pytorch-tutorial)\n\n### 1. Basics\n* [PyTorch Basics](tutorials/basics/pytorch_basics/main.cpp)\n* [Linear Regression](tutorials/basics/linear_regression/main.cpp)\n* [Logistic Regression](tutorials/basics/logistic_regression/main.cpp)\n* [Feedforward Neural Network](tutorials/basics/feedforward_neural_network/src/main.cpp)\n\n### 2. Intermediate\n* [Convolutional Neural Network](tutorials/intermediate/convolutional_neural_network/src/main.cpp)\n* [Deep Residual Network](tutorials/intermediate/deep_residual_network/src/main.cpp)\n* [Recurrent Neural Network](tutorials/intermediate/recurrent_neural_network/src/main.cpp)\n* [Bidirectional Recurrent Neural Network](tutorials/intermediate/bidirectional_recurrent_neural_network/src/main.cpp)\n* [Language Model (RNN-LM)](tutorials/intermediate/language_model/src/main.cpp)\n\n### 3. Advanced\n* [Generative Adversarial Networks](tutorials/advanced/generative_adversarial_network/main.cpp)\n* [Variational Auto-Encoder](tutorials/advanced/variational_autoencoder/src/main.cpp)\n* [Neural Style Transfer](tutorials/advanced/neural_style_transfer/src/main.cpp)\n* [Image Captioning (CNN-AttentionRNN)](tutorials/advanced/image_captioning/src/main.cpp)\n\n### 4. Interactive Tutorials\n* [Tensor Slicing](notebooks/tensor_slicing.ipynb)\n\n### 5. Other Popular Tutorials\n* [Deep Learning with PyTorch: A 60 Minute Blitz](tutorials/popular/blitz)\n\n# Getting Started\n\n## Requirements\n\n1. [C++-17](http://www.cplusplus.com/doc/tutorial/introduction/) compatible compiler\n2. [CMake](https://cmake.org/download/) (minimum version 3.28.6)\n3. [LibTorch version \u003e= 1.12.0 and \u003c= 2.8.0](https://pytorch.org/cppdocs/installing.html)\n4. [Conda](https://docs.conda.io/projects/conda/en/latest/user-guide/install/download.html)\n\n\n## For Interactive Tutorials\n\n**Note**: Interactive Tutorials are currently running on **LibTorch Nightly Version**.  \nSo there are some tutorials which can break when working with _nightly version_.\n\n```bash\nconda create --name pytorch-cpp\nconda activate pytorch-cpp\nconda install xeus-cling notebook -c conda-forge\n```\n## Clone, build and run tutorials\n### In Google Colab\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/prabhuomkar/pytorch-cpp/blob/master/notebooks/pytorch_cpp_colab_notebook.ipynb)\n\n### On Local Machine\n\n```bash\ngit clone https://github.com/prabhuomkar/pytorch-cpp.git\ncd pytorch-cpp\n```\n\n#### Generate build system\n\n```bash\ncmake -B build #\u003coptions\u003e\n```\n\u003e **_Note for Windows users:_**\u003cbr\u003e \n\u003e Libtorch only supports 64bit Windows and an x64 generator needs to be specified. For Visual Studio this can be done by appending `-A x64` to the above command.\n\nSome useful options:\n\n| Option       | Default           | Description  |\n| :------------- |:------------|-----:|\n| `-D CUDA_V=(11.8\\|12.4\\|12.6\\|12.8\\|12.9\\|none)`     | `none` | Download LibTorch for a CUDA version (`none` = download CPU version). |\n| `-D LIBTORCH_DOWNLOAD_BUILD_TYPE=(Release\\|Debug)` | `Release` | Determines which libtorch build type version to download (only relevant on **Windows**).|\n| `-D DOWNLOAD_DATASETS=(OFF\\|ON)`     | `ON`      |   Download required datasets during build (only if they do not already exist in `pytorch-cpp/data`). |\n|`-D CREATE_SCRIPTMODULES=(OFF\\|ON)` | `OFF` | Create all required scriptmodule files for prelearned models / weights during build. Requires installed  python3 with  pytorch and torchvision. |\n| `-D CMAKE_PREFIX_PATH=path/to/libtorch/share/cmake/Torch` |   `\u003cempty\u003e`    |    Skip the downloading of LibTorch and use your own local version (see [Requirements](#requirements)) instead. |\n| `-D CMAKE_BUILD_TYPE=(Release\\|Debug\\|...)` | `\u003cempty\u003e` | Determines the CMake build-type for single-configuration generators (see [CMake docs](https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html)).|\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cb\u003eExample Linux\u003c/b\u003e\u003c/summary\u003e\n\n##### Aim\n* Use existing Python, PyTorch (see [Requirements](#requirements)) and torchvision installation.\n* Download all datasets and create all necessary scriptmodule files.\n\n##### Command\n```bash\ncmake -B build \\\n-D CMAKE_BUILD_TYPE=Release \\\n-D CMAKE_PREFIX_PATH=/path/to/libtorch/share/cmake/Torch \\\n-D CREATE_SCRIPTMODULES=ON \n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cb\u003eExample Windows\u003c/b\u003e\u003c/summary\u003e\n\n##### Aim\n* Automatically download LibTorch for CUDA 11.8 (Release version) and all necessary datasets.\n* Do not create scriptmodule files.\n\n##### Command\n```bash\ncmake -B build \\\n-A x64 \\\n-D CUDA_V=11.8\n```\n\u003c/details\u003e\n\n#### Build\n\u003e**_Note for Windows (Visual Studio) users:_** \u003cbr\u003e\n\u003eThe CMake script downloads the *Release* version of LibTorch, so `--config Release` has to be appended to the build command.\n\n**How dataset download and scriptmodule creation work:**\n* If `DOWNLOAD_DATASETS` is `ON`, the datasets required by the tutorials you choose to build will be downloaded to `pytorch-cpp/data` (if they do not already exist there).\n* If `CREATE_SCRIPTMODULES` is `ON`, the scriptmodule files for the prelearned models / weights required by the tutorials you choose to build will be created in the `model` folder of the respective tutorial's source folder (if they do not already exist).\n#### All tutorials\nTo build all tutorials use\n```bash\ncmake --build build\n```\n\n#### All tutorials in a category\nYou can choose to only build tutorials in one of the categories `basics`, `intermediate`, `advanced` or `popular`. For example, if you are only interested in the `basics` tutorials:\n```bash\ncmake --build build --target basics\n# In general: cmake --build build --target {category}\n```\n\n#### Single tutorial\nYou can also choose to only build a single tutorial. For example to build the language model tutorial only: \n```bash\ncmake --build build --target language-model\n# In general: cmake --build build --target {tutorial-name}\n```\n\u003e**_Note_**:  \n\u003e The target argument is the tutorial's foldername with all underscores replaced by hyphens.\n\n\u003e**_Tip for users of CMake version \u003e= 3.15_**:  \n\u003e You can specify several targets separated by spaces, for example:  \n\u003e ```bash \n\u003e cmake --build build --target language-model image-captioning\n\u003e ```\n\n#### Run Tutorials\n1. (**IMPORTANT!**) First change into the tutorial's directory within `build/tutorials`. For example, assuming you are in the `pytorch-cpp` directory and want to change to the pytorch basics tutorial folder:\n     ```bash\n     cd build/tutorials/basics/pytorch_basics\n     # In general: cd build/tutorials/{basics|intermediate|advanced|popular/blitz}/{tutorial_name}\n     ```\n2. Run the executable. Note that the executable's name is the tutorial's foldername with all underscores replaced with hyphens (e.g. for tutorial folder: `pytorch_basics` -\u003e executable name: `pytorch-basics` (or `pytorch-basics.exe` on Windows)). For example, to run the pytorch basics tutorial:\u003cbr\u003e\u003cbr\u003e\n     **Linux/Mac**\n     ```bash\n     ./pytorch-basics\n     # In general: ./{tutorial-name}\n     ```\n     **Windows**\n     ```powershell\n     .\\pytorch-basics.exe\n     # In general: .\\{tutorial-name}.exe\n     ```\n\n### Using Docker\n\nFind the latest and previous version images on [Docker Hub](https://hub.docker.com/repository/docker/prabhuomkar/pytorch-cpp).\n\nYou can build and run the tutorials (on CPU) in a Docker container using the provided `Dockerfile` and `docker-compose.yml` files:  \n1. From the root directory of the cloned repo build the image:\n    ```bash\n    docker-compose build --build-arg USER_ID=$(id -u) --build-arg GROUP_ID=$(id -g)\n    ```\n    \u003e **_Note_**:  \n    \u003e When you run the Docker container, the host repo directory is mounted as a volume in the Docker container in order to cache build and downloaded dependency files so that it is not necessary to rebuild or redownload everything when a container is restarted. In order to have correct file permissions it is necessary to provide your user and group ids as build arguments when building the image on Linux.\n2. Now start the container and build the tutorials using:\n    ```bash\n    docker-compose run --rm pytorch-cpp\n    ```\n    This fetches all necessary dependencies and builds all tutorials.\n    After the build is done, by default the container starts `bash` in interactive mode in the `build/tutorials` folder.  \n    As with the local build, you can choose to only build tutorials of a category (`basics`, `intermediate`, `advanced`, `popular`):\n    ```bash\n    docker-compose run --rm pytorch-cpp {category}\n    ```\n    In this case the container is started in the chosen category's base build directory.  \n    Alternatively, you can also directly run a tutorial by instead invoking the run command with a tutorial name as additional argument, for example:\n    ```bash\n    docker-compose run --rm pytorch-cpp pytorch-basics\n    # In general: docker-compose run --rm pytorch-cpp {tutorial-name} \n    ```\n    This will - if necessary - build the pytorch-basics tutorial and then start the executable in a container.\n\n## License\nThis repository is licensed under MIT as given in [LICENSE](LICENSE).\n","funding_links":["https://github.com/sponsors/prabhuomkar","https://github.com/sponsors/mfl28"],"categories":["C++","Tutorials \u0026 books \u0026 examples｜教程 \u0026 书籍 \u0026 示例","Tutorials, books, \u0026 examples"],"sub_categories":["Other libraries｜其他库:","Other libraries:"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprabhuomkar%2Fpytorch-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprabhuomkar%2Fpytorch-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprabhuomkar%2Fpytorch-cpp/lists"}