{"id":13713595,"url":"https://github.com/LaurentMazare/ocaml-torch","last_synced_at":"2025-05-07T00:31:49.826Z","repository":{"id":45894752,"uuid":"151335820","full_name":"LaurentMazare/ocaml-torch","owner":"LaurentMazare","description":"OCaml bindings for PyTorch","archived":false,"fork":false,"pushed_at":"2024-10-17T13:03:35.000Z","size":9301,"stargazers_count":425,"open_issues_count":10,"forks_count":37,"subscribers_count":13,"default_branch":"main","last_synced_at":"2025-04-08T10:19:25.502Z","etag":null,"topics":["artificial-intelligence","deep-learning","gpu","machine-learning","neural-network","ocaml","pytorch","tensor"],"latest_commit_sha":null,"homepage":"","language":"OCaml","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LaurentMazare.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-02T23:14:32.000Z","updated_at":"2025-03-19T19:10:43.000Z","dependencies_parsed_at":"2024-11-13T19:00:38.104Z","dependency_job_id":"2bfb3d6a-f0e3-4002-9b6a-e1a6d9c98e7e","html_url":"https://github.com/LaurentMazare/ocaml-torch","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaurentMazare%2Focaml-torch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaurentMazare%2Focaml-torch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaurentMazare%2Focaml-torch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaurentMazare%2Focaml-torch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LaurentMazare","download_url":"https://codeload.github.com/LaurentMazare/ocaml-torch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252791527,"owners_count":21804790,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["artificial-intelligence","deep-learning","gpu","machine-learning","neural-network","ocaml","pytorch","tensor"],"created_at":"2024-08-02T23:01:40.044Z","updated_at":"2025-05-07T00:31:44.761Z","avatar_url":"https://github.com/LaurentMazare.png","language":"OCaml","funding_links":[],"categories":["OCaml","Pytorch elsewhere ｜ Pytorch相关","Pytorch elsewhere","Machine Learning"],"sub_categories":["Other libraries｜其他库:","Other libraries:"],"readme":"\u003e **Warning**\n\u003e \n\u003e Development for this repo has moved to https://github.com/janestreet/torch .\n\n# ocaml-torch\n__ocaml-torch__ provides some ocaml bindings for the [PyTorch](https://pytorch.org) tensor library.\nThis brings to OCaml NumPy-like tensor computations with GPU acceleration and tape-based automatic\ndifferentiation.\n\n![Main workflow](https://github.com/LaurentMazare/ocaml-torch/workflows/Main%20workflow/badge.svg)\n\nThese bindings use the [PyTorch C++ API](https://pytorch.org/cppdocs/) and are\nmostly automatically generated. The current GitHub tip and the opam package v0.7\ncorresponds to PyTorch **v2.0.0**.\n\nOn Linux note that you will need the PyTorch version using the cxx11 abi\n[cpu version](https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.0.0%2Bcpu.zip),\n[cuda 11.7 version](https://download.pytorch.org/libtorch/cu117/libtorch-cxx11-abi-shared-with-deps-2.0.0%2Bcu117.zip).\n\n## Opam Installation\n\nThe [opam](https://opam.ocaml.org/) package can be installed using the following command.\nThis automatically installs the CPU version of libtorch.\n\n```bash\nopam install torch\n```\n\nYou can then compile some sample code, see some instructions below.\n__ocaml-torch__ can also be used in interactive mode via\n[utop](https://github.com/ocaml-community/utop) or\n[ocaml-jupyter](https://github.com/akabe/ocaml-jupyter).\n\nHere is a sample utop session.\n\n![utop](./images/utop.png)\n\n\n### Build a Simple Example\n\nTo build a first torch program, create a file `example.ml` with the\nfollowing content.\n\n```ocaml\nopen Torch\n\nlet () =\n  let tensor = Tensor.randn [ 4; 2 ] in\n  Tensor.print tensor\n```\n\nThen create a `dune` file with the following content:\n\n```ocaml\n(executables\n  (names example)\n  (libraries torch))\n```\n\nRun `dune exec example.exe` to compile the program and run it!\n\nAlternatively you can first compile the code via `dune build example.exe` then run the executable\n`_build/default/example.exe` (note that building the bytecode target `example.bc` may\nnot work on macos).\n\n## Tutorials and Examples\n\n* [MNIST tutorial](https://github.com/LaurentMazare/ocaml-torch/tree/master/examples/mnist).\n* [Finetuning a ResNet-18 model](https://github.com/LaurentMazare/ocaml-torch/blob/master/examples/pretrained/).\n* [Generative Adversarial Networks](https://github.com/LaurentMazare/ocaml-torch/blob/master/examples/gan).\n* [Running some Python model](https://github.com/LaurentMazare/ocaml-torch/tree/master/examples/jit).\n\nSome more advanced applications from external repos:\n* An [OCaml port of mini-dalle](https://github.com/ArulselvanMadhavan/mini_dalle) by Arulselvan Madhavan.\n* A clone of the [diffusers](https://github.com/huggingface/diffusers) library implementing\n  stable-diffusion 1.5: [diffusers-ocaml](https://github.com/ArulselvanMadhavan/diffusers-ocaml) by Arulselvan Madhavan.\n* Natural Language Processing models based on BERT can be found in the\n[ocaml-bert repo](https://github.com/LaurentMazare/ocaml-bert).\n\n\n## Sample Code\n\nBelow is an example of a linear model trained on the MNIST dataset ([full\ncode](https://github.com/LaurentMazare/ocaml-torch/blob/master/examples/mnist/linear.ml)).\n\n```ocaml\n  (* Create two tensors to store model weights. *)\n  let ws = Tensor.zeros [image_dim; label_count] ~requires_grad:true in\n  let bs = Tensor.zeros [label_count] ~requires_grad:true in\n\n  let model xs = Tensor.(mm xs ws + bs) in\n  for index = 1 to 100 do\n    (* Compute the cross-entropy loss. *)\n    let loss =\n      Tensor.cross_entropy_for_logits (model train_images) ~targets:train_labels\n    in\n\n    Tensor.backward loss;\n\n    (* Apply gradient descent, disable gradient tracking for these. *)\n    Tensor.(no_grad (fun () -\u003e\n        ws -= grad ws * f learning_rate;\n        bs -= grad bs * f learning_rate));\n\n    (* Compute the validation error. *)\n    let test_accuracy =\n      Tensor.(argmax ~dim:(-1) (model test_images) = test_labels)\n      |\u003e Tensor.to_kind ~kind:(T Float)\n      |\u003e Tensor.sum\n      |\u003e Tensor.float_value\n      |\u003e fun sum -\u003e sum /. test_samples\n    in\n    printf \"%d %f %.2f%%\\n%!\" index (Tensor.float_value loss) (100. *. test_accuracy);\n  done\n\n```\n\n* Some [ResNet examples on CIFAR-10](https://github.com/LaurentMazare/ocaml-torch/tree/master/examples/cifar).\n* A simplified version of\n  [char-rnn](https://github.com/LaurentMazare/ocaml-torch/blob/master/examples/char_rnn)\n  illustrating character level language modeling using Recurrent Neural Networks.\n* [Neural Style Transfer](https://github.com/LaurentMazare/ocaml-torch/blob/master/examples/neural_transfer)\n  applies the style of an image to the content of another image. This uses some deep Convolutional Neural Network.\n\n## Models and Weights\n\nVarious pre-trained computer vision models are implemented in the\n[vision library](https://github.com/LaurentMazare/ocaml-torch/tree/master/src/vision).\nThe weight files can be downloaded at the following links:\n\n* ResNet-18 [weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/resnet18.ot).\n* ResNet-34 [weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/resnet34.ot).\n* ResNet-50 [weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/resnet50.ot).\n* ResNet-101 [weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/resnet101.ot).\n* ResNet-152 [weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/resnet152.ot).\n* DenseNet-121 [weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/densenet121.ot).\n* DenseNet-161 [weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/densenet161.ot).\n* DenseNet-169 [weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/densenet169.ot).\n* SqueezeNet 1.0 [weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/squeezenet1_0.ot).\n* SqueezeNet 1.1 [weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/squeezenet1_1.ot).\n* VGG-13 [weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/vgg13.ot).\n* VGG-16 [weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/vgg16.ot).\n* AlexNet [weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/alexnet.ot).\n* Inception-v3 [weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/inception-v3.ot).\n* MobileNet-v2 [weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/mobilenet-v2.ot).\n* EfficientNet\n  [b0 weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/efficientnet-b0.ot),\n  [b1 weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/efficientnet-b1.ot),\n  [b2 weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/efficientnet-b2.ot),\n  [b3 weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/efficientnet-b3.ot),\n  [b4 weights](https://github.com/LaurentMazare/ocaml-torch/releases/download/v0.1-unstable/efficientnet-b4.ot).\n\nRunning the pre-trained models on some sample images can the easily be done via the following commands.\n```bash\ndune exec examples/pretrained/predict.exe path/to/resnet18.ot tiger.jpg\n```\n\n## Alternative Installation Option\n\nThis alternative way to install __ocaml-torch__ could be useful to run with GPU\nacceleration enabled.\n\nThe libtorch library can be downloaded from the [PyTorch\nwebsite](https://pytorch.org/resources) ([2.0.0 cpu\nversion](https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.0.0+cpu.zip)).\n\nDownload and extract the libtorch library then to build all the examples run:\n\n```bash\nexport LIBTORCH=/path/to/libtorch\ngit clone https://github.com/LaurentMazare/ocaml-torch.git\ncd ocaml-torch\nmake all\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLaurentMazare%2Focaml-torch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FLaurentMazare%2Focaml-torch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLaurentMazare%2Focaml-torch/lists"}