Git Product home page Git Product logo

torch's Introduction

ocaml-torch

ocaml-torch provides OCaml bindings for the PyTorch tensor library. This brings to OCaml NumPy-like tensor computations with GPU acceleration and tape-based automatic differentiation.

These bindings use the PyTorch C++ API and are mostly automatically generated. The current GitHub tip corresponds to PyTorch v1.13.0.

Installation with Libtorch

Torch depends on libtorch, so when you opam install this package, it will try linking to libtorch depending on your environment variables. The code for discovering libtorch is in src/config/discover.ml. In order to change how torch binds to libtorch, you must uninstall and reinstall libtorch.

To install with any of these methods, after configuring your environment, you may either

  • opam install torch, or
  • build from source:
git clone https://github.com/LaurentMazare/ocaml-torch.git
cd ocaml-torch
make all

On Linux note that you will need the libtorch PyTorch version using the appropriate cxx11 abi depending on your g++ version.

Option 1: OPAM switch (CPU only)

The opam libtorch package will be automatically installed as a dependency and requires no special environment variables. However, it is CPU-only, so will not support GPUs.

Option 2: Conda

If you've installed libtorch via Conda, ensure that you are in the Conda environment with the CONDA_PREFIX set before installing.

Option 3: System Libraries

If you have libtorch installed as a system library (e.g. RPM), run export LIBTORCH_USE_SYSTEM=1 before installing.

Option 4: Custom Libtorch Location

If you have downloaded libtorch somewhere, run export LIBTORCH=/path/to/libtorch/ before installing.

Examples

Utop

ocaml-torch can be used in interactive mode via utop or ocaml-jupyter.

Here is a sample utop session:

utop

Simple Script

To build a simple torch program, create a file example.ml:

open Torch

let () =
  let tensor = Tensor.randn [ 4; 2 ] in
  Tensor.print tensor

Then create a dune file with the following content:

(executables
  (names example)
  (libraries torch))

Run dune exec example.exe to compile the program and run it!

Alternatively you can first compile the code via dune build example.exe then run the executable _build/default/example.exe (note that building the bytecode target example.bc may not work on macos).

Demos

Some more advanced applications from external repos:

Models and Weights

Various pre-trained computer vision models are implemented in the vision library. The weight files can be downloaded at the following links:

Running the pre-trained models on sample images can the easily be done via:

dune exec examples/pretrained/predict.exe path/to/resnet18.ot images/tiger.jpg

Acknowledgements

Many thanks to @LaurentMazare for the original work of ocaml-torch.

torch's People

Contributors

public-release avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.