Examples using libtorch, the C++ frontend of PyTorch (v2.5.1).
| Example | Description |
|---|---|
hello_world.cpp |
Tensor creation, reshaping, and transpose operations |
simple_optimization_example.cpp |
Gradient descent — manual and with torch::optim::SGD |
function_approx.cpp |
Feedforward network that learns y = cos(x) |
time_serie_prediction.cpp |
LSTM-based time series forward pass |
lstm_example.cpp |
Bidirectional LSTM trained on a simple sequence |
dataset_example.cpp |
Custom dataset with CSV parsing and one-hot encoding |
- CMake ≥ 3.14
- C++17 compiler
libtorch is downloaded automatically during the CMake configure step if not found locally.
Download libtorch automatically:
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build .Use an existing local libtorch:
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch && cmake --build .