All Projects → LaurentMazare → Ocaml Torch

LaurentMazare / Ocaml Torch

Licence: apache-2.0
OCaml bindings for PyTorch

Programming Languages

ocaml
1615 projects

Projects that are alternatives of or similar to Ocaml Torch

Norse
Deep learning with spiking neural networks (SNNs) in PyTorch.
Stars: ✭ 211 (-31.49%)
Mutual labels:  gpu, tensor
Nd4j
Fast, Scientific and Numerical Computing for the JVM (NDArrays)
Stars: ✭ 1,742 (+465.58%)
Mutual labels:  artificial-intelligence, gpu
Caer
High-performance Vision library in Python. Scale your research, not boilerplate.
Stars: ✭ 452 (+46.75%)
Mutual labels:  artificial-intelligence, gpu
Tensorflow Gpu Macosx
Unoffcial NVIDIA CUDA GPU support version of Google Tensorflow for MAC OSX
Stars: ✭ 103 (-66.56%)
Mutual labels:  gpu, tensor
Deep Learning In Cloud
List of Deep Learning Cloud Providers
Stars: ✭ 298 (-3.25%)
Mutual labels:  artificial-intelligence, gpu
Pytorch
Tensors and Dynamic neural networks in Python with strong GPU acceleration
Stars: ✭ 52,811 (+17046.43%)
Mutual labels:  gpu, tensor
Blazingsql
BlazingSQL is a lightweight, GPU accelerated, SQL engine for Python. Built on RAPIDS cuDF.
Stars: ✭ 1,652 (+436.36%)
Mutual labels:  artificial-intelligence, gpu
Drlkit
A High Level Python Deep Reinforcement Learning library. Great for beginners, prototyping and quickly comparing algorithms
Stars: ✭ 29 (-90.58%)
Mutual labels:  gpu, tensor
Komputation
Komputation is a neural network framework for the Java Virtual Machine written in Kotlin and CUDA C.
Stars: ✭ 295 (-4.22%)
Mutual labels:  artificial-intelligence, gpu
Deeplearning4j
Suite of tools for deploying and training deep learning models using the JVM. Highlights include model import for keras, tensorflow, and onnx/pytorch, a modular and tiny c++ library for running math code and a java based math library on top of the core c++ library. Also includes samediff: a pytorch/tensorflow like library for running deep learni…
Stars: ✭ 12,277 (+3886.04%)
Mutual labels:  artificial-intelligence, gpu
Deepnet
Deep.Net machine learning framework for F#
Stars: ✭ 99 (-67.86%)
Mutual labels:  gpu, tensor
Atlas
An Open Source, Self-Hosted Platform For Applied Deep Learning Development
Stars: ✭ 259 (-15.91%)
Mutual labels:  artificial-intelligence, gpu
Hyperlearn
50% faster, 50% less RAM Machine Learning. Numba rewritten Sklearn. SVD, NNMF, PCA, LinearReg, RidgeReg, Randomized, Truncated SVD/PCA, CSR Matrices all 50+% faster
Stars: ✭ 1,204 (+290.91%)
Mutual labels:  gpu, tensor
Compute.scala
Scientific computing with N-dimensional arrays
Stars: ✭ 191 (-37.99%)
Mutual labels:  gpu, tensor
Nx
Multi-dimensional arrays (tensors) and numerical definitions for Elixir
Stars: ✭ 1,133 (+267.86%)
Mutual labels:  gpu, tensor
Imageai
A python library built to empower developers to build applications and systems with self-contained Computer Vision capabilities
Stars: ✭ 6,734 (+2086.36%)
Mutual labels:  artificial-intelligence, gpu
Cupy
NumPy & SciPy for GPU
Stars: ✭ 5,625 (+1726.3%)
Mutual labels:  gpu, tensor
Tvm
Open deep learning compiler stack for cpu, gpu and specialized accelerators
Stars: ✭ 7,494 (+2333.12%)
Mutual labels:  gpu, tensor
Floyd Cli
Command line tool for FloydHub - the fastest way to build, train, and deploy deep learning models
Stars: ✭ 147 (-52.27%)
Mutual labels:  artificial-intelligence, gpu
Pai
Resource scheduling and cluster management for AI
Stars: ✭ 2,223 (+621.75%)
Mutual labels:  artificial-intelligence, gpu

ocaml-torch

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

Main workflow

These bindings use the PyTorch C++ API and are mostly automatically generated. The current GitHub tip and the opam package v0.7 corresponds to PyTorch v1.8.0.

On Linux note that you will need the PyTorch version using the cxx11 abi cpu version, cuda 10.2 version.

Opam Installation

The opam package can be installed using the following command. This automatically installs the CPU version of libtorch.

opam install torch

You can then compile some sample code, see some instructions below. ocaml-torch can also be used in interactive mode via utop or ocaml-jupyter.

Here is a sample utop session.

utop

Build a Simple Example

To build a first torch program, create a file example.ml with the following content.

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).

Tutorials

Examples

Below is an example of a linear model trained on the MNIST dataset (full code).

  (* Create two tensors to store model weights. *)
  let ws = Tensor.zeros [image_dim; label_count] ~requires_grad:true in
  let bs = Tensor.zeros [label_count] ~requires_grad:true in

  let model xs = Tensor.(mm xs ws + bs) in
  for index = 1 to 100 do
    (* Compute the cross-entropy loss. *)
    let loss =
      Tensor.cross_entropy_for_logits (model train_images) ~targets:train_labels
    in

    Tensor.backward loss;

    (* Apply gradient descent, disable gradient tracking for these. *)
    Tensor.(no_grad (fun () ->
        ws -= grad ws * f learning_rate;
        bs -= grad bs * f learning_rate));

    (* Compute the validation error. *)
    let test_accuracy =
      Tensor.(argmax (model test_images) = test_labels)
      |> Tensor.to_kind ~kind:(T Float)
      |> Tensor.sum
      |> Tensor.float_value
      |> fun sum -> sum /. test_samples
    in
    printf "%d %f %.2f%%\n%!" index (Tensor.float_value loss) (100. *. test_accuracy);
  done

  • Some ResNet examples on CIFAR-10.
  • A simplified version of char-rnn illustrating character level language modeling using Recurrent Neural Networks.
  • Neural Style Transfer applies the style of an image to the content of another image. This uses some deep Convolutional Neural Network.

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 some sample images can the easily be done via the following commands.

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

Natural Language Processing models based on BERT can be found in the ocaml-torch repo.

Alternative Installation Option

This alternative way to install ocaml-torch could be useful to run with GPU acceleration enabled.

The libtorch library can be downloaded from the PyTorch website (1.8.0 cpu version).

Download and extract the libtorch library then to build all the examples run:

export LIBTORCH=/path/to/libtorch
git clone https://github.com/LaurentMazare/ocaml-torch.git
cd ocaml-torch
make all
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].