What machine learning and optimization features does DiffSharp provide?
Updated
DiffSharp is a tensor library built for differentiable programming, and its machine learning and optimization features fall into four practical areas: common optimizers and model elements, differentiable probability distributions, GPU/CUDA tensor computation through a LibTorch backend, and interactive notebooks for Jupyter and Visual Studio Code. It is aimed at machine learning, probabilistic programming, and optimization work, and it is 100% open source under a BSD license. The features below are the ones the project documents on its home page; for exact API signatures you would check the API reference.
Optimizers and model elements
DiffSharp ships with "common optimizers, model elements," which means you don't have to hand-write the standard training loop plumbing. In practice this covers the pieces you'd expect when building and training a model: parameter containers, layers or model building blocks, and optimizer routines that update parameters from computed gradients.
The important mechanism here is that these elements sit on top of DiffSharp's own computation graph and differentiation engine, not on LibTorch's autograd. That design is what lets optimizers participate in differentiation themselves — for example, differentiating through an optimization step rather than only using the optimizer to train a model.
Differentiable probability distributions
DiffSharp includes "differentiable probability distributions," which is the feature that makes probabilistic programming viable in the library. A distribution whose sampling and log-density are differentiable can be composed with the rest of the tensor code and differentiated end to end.
A concrete case: you want to fit a model by maximizing a likelihood or minimizing a variational objective. With differentiable distributions you can write the log-density directly in your loss and let DiffSharp compute gradients through it, instead of deriving gradients by hand. This is also where DiffSharp's nested differentiation matters — you can differentiate a function that internally performs gradient-based optimization.
Automatic differentiation depth
The differentiation capability is the core of the optimization story. DiffSharp provides composable gradients, Hessians, Jacobians, directional derivatives, and matrix-free Hessian- and Jacobian-vector products over arbitrary user code. It supports nested forward and reverse differentiation to any level.
The project explicitly positions this as going beyond conventional tensor libraries such as PyTorch and TensorFlow, because those typically give you first-order reverse-mode gradients as the primary path. If your optimization method needs second-order information — a Hessian-vector product inside a solver, for instance — DiffSharp expresses it directly rather than through finite differences or manual derivation.
GPU and CUDA support via LibTorch
DiffSharp uses a LibTorch backend, running the same C++ and CUDA tensor implementations that power PyTorch, with LibTorch's ATen tensors underneath and DiffSharp's own computation graph on top. It supports CUDA and GPUs, and is tested on Linux, macOS, and Windows.
Two consequences worth knowing before you commit:
- Performance profile: raw tensor operations inherit LibTorch's optimized kernels, so heavy tensor math is not a weak point.
- Portability: the same code is expected to run across the three supported desktop platforms, which matters if your optimization job moves between a laptop and a GPU machine.
Interactive notebooks
All documentation pages on the site are interactive notebooks you can execute directly in the browser without installing anything locally. For local work, DiffSharp supports interactive notebooks in Jupyter and Visual Studio Code. This is the fastest way to test whether a particular optimizer or distribution behaves as you expect before wiring it into a larger project.
API familiarity and language
The API is deliberately modeled on the PyTorch Python API, using similar naming and idioms, so where elements share names the PyTorch documentation can generally serve as a guide. The implementation language is F#, chosen for robust functional programming; the project describes F# code as generally faster and more robust than equivalent Python while remaining succinct.
Choosing based on your situation
| If your priority is… | DiffSharp fits when… | Consider alternatives when… |
|---|---|---|
| Higher-order derivatives in optimization | You need Hessians, Jacobians, or nested differentiation over arbitrary code | First-order gradients are sufficient and you want the largest ecosystem |
| Probabilistic modeling | You need differentiable distributions composed into a loss | You need a mature probabilistic programming language with its own inference engine |
| GPU tensor performance | You want LibTorch/CUDA kernels with a PyTorch-like API | Your team is standardized on Python and PyTorch tooling |
| Language and robustness | You are comfortable with or willing to learn F# | Your team cannot adopt F# |
The main adoption cost is F#: the PyTorch-like naming lowers the API learning curve, but not the language one. If your optimization work genuinely needs nested or higher-order differentiation, that cost buys capability you won't easily replicate elsewhere. If it doesn't, a conventional tensor library will likely be the simpler path.