dotnet/TorchSharp
A .NET library that provides access to the library that powers PyTorch.
What it solves
TorchSharp brings the functionality of PyTorch to the .NET ecosystem. It solves the problem that .NET developers previously had to use Python to work with PyTorch, or use other frameworks. It provides access to the underlying LibTorch library, allowing .NET developers to build and train neural networks, do tensor math, and more, in C# or F#.
How it works
TorchSharp is a wrapper library: it wraps the native LibTorch binaries and exposes them as .NET APIs. The design mirrors PyTorch's API as closely as possible, so that Python-based examples can be easily translated. It relies on method overloading to support multiple input types, and uses .NET enumerations instead of strings where PyTorch uses enums. The core focus is on tensors. The library includes factory methods for each module class (e.g., Linear, ReLU, Dropout) so that C# code looks similar to PyTorch. Memory management is handled through disposable objects and careful resource tracking.
Who it’s for
The library is for .NET developers who want to use the power of PyTorch without leaving the .NET world. It is particularly useful for researchers, data scientists, and machine learning engineers working in C# or F#. It is also used by other libraries, such as DiffSharp, which builds on it for automatic differentiation.
Highlights
- Provides .NET bindings for LibTorch (the engine behind PyTorch) with a focus on tensors.
- Designed to feel like PyTorch, so Python examples can be adapted easily.
- Distributed via NuGet with bundled packages for CPU and CUDA (Windows/Linux).
- Works on Linux, Windows, and macOS (Apple Silicon).
- Includes high-level modules (Linear, ReLU, Dropout) and an optimizer (Adam) for building and training networks.
- Supports both CPU and GPU (CUDA) execution.
Related
- Project
- Project
- Project
- Project