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.
These bindings use the PyTorch C++ API and are mostly automatically generated. The current GitHub tip corresponds to PyTorch v1.3.0.
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.
To build a first torch program, create a file example.ml
with the following content.
open Torchlet () = 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).
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
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.
Various pre-trained computer vision models are implemented in the vision library. The weight files can be downloaded at the following links:
ResNet-18 weights.
ResNet-34 weights.
ResNet-50 weights.
ResNet-101 weights.
ResNet-152 weights.
DenseNet-121 weights.
DenseNet-161 weights.
DenseNet-169 weights.
SqueezeNet 1.0 weights.
SqueezeNet 1.1 weights.
VGG-13 weights.
VGG-16 weights.
AlexNet weights.
Inception-v3 weights.
MobileNet-v2 weights.
EfficientNet b0 weights, b1 weights, b2 weights, b3 weights, b4 weights.
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
These alternative ways to install ocaml-torch could be useful to run with GPU acceleration enabled.
The libtorch library can be downloaded from the PyTorch website (1.3.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.gitcd ocaml-torch make all
Conda packages for PyTorch 1.1 can be used via the following command.
conda create -n torchsource activate torch conda install pytorch-cpu=1.3.0 -c pytorch# Or for the CUDA version# conda install pytorch=1.3.0 -c pytorchgit clone https://github.com/LaurentMazare/ocaml-torch.gitcd ocaml-torch make all
上一篇:DoodleMaster
下一篇:extension-script
还没有评论,说两句吧!
热门资源
Keras-ResNeXt
Keras ResNeXt Implementation of ResNeXt models...
seetafaceJNI
项目介绍 基于中科院seetaface2进行封装的JAVA...
spark-corenlp
This package wraps Stanford CoreNLP annotators ...
capsnet-with-caps...
CapsNet with capsule-wise convolution Project ...
inferno-boilerplate
This is a very basic boilerplate example for pe...
智能在线
400-630-6780
聆听.建议反馈
E-mail: support@tusaishared.com