{"id":13713604,"url":"https://github.com/LaurentMazare/tensorflow-ocaml","last_synced_at":"2025-05-07T00:31:47.609Z","repository":{"id":144339308,"uuid":"54334585","full_name":"LaurentMazare/tensorflow-ocaml","owner":"LaurentMazare","description":"OCaml bindings for TensorFlow","archived":false,"fork":false,"pushed_at":"2019-07-06T20:37:30.000Z","size":21525,"stargazers_count":284,"open_issues_count":4,"forks_count":25,"subscribers_count":24,"default_branch":"master","last_synced_at":"2025-04-09T15:05:35.148Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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":"2016-03-20T18:53:25.000Z","updated_at":"2025-02-04T07:19:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"b21071ff-9f80-4097-9258-c134f3b2e05e","html_url":"https://github.com/LaurentMazare/tensorflow-ocaml","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaurentMazare%2Ftensorflow-ocaml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaurentMazare%2Ftensorflow-ocaml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaurentMazare%2Ftensorflow-ocaml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaurentMazare%2Ftensorflow-ocaml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LaurentMazare","download_url":"https://codeload.github.com/LaurentMazare/tensorflow-ocaml/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252791530,"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":[],"created_at":"2024-08-02T23:01:40.276Z","updated_at":"2025-05-07T00:31:44.842Z","avatar_url":"https://github.com/LaurentMazare.png","language":"OCaml","funding_links":[],"categories":["OCaml","Questions"],"sub_categories":["General-Purpose Machine Learning"],"readme":"The __tensorflow-ocaml__ project provides some [OCaml](http://ocaml.org) bindings for [TensorFlow](http://tensorflow.org).\n\nExperimental ocaml bindings for [PyTorch](https://pytorch.org)\ncan be found in the [ocaml-torch repo](https://github.com/LaurentMazare/ocaml-torch).\n\n## Installation\n\nUse [opam](https://opam.ocaml.org/) to install the __tensorflow-ocaml__ package.\nStarting from version 0.0.11 this will automatically install the TensorFlow library.\n\n```bash\nopam install tensorflow\n```\n\n### Build a simple example or run utop\n\nTo build your first TensorFlow program, create a new directory and cd into it.\nThen create a `forty_two.ml` file with the following content:\n\n```ocaml\nopen Tensorflow\n\nlet () =\n  let forty_two = Ops.(f 40. + f 2.) in\n  let v = Session.run (Session.Output.scalar_float forty_two) in\n  Printf.printf \"%f\\n%!\" v\n```\n\nThen create a `dune` file with the following content:\n\n```ocaml\n(executables\n  (names forty_two)\n  (libraries tensorflow))\n```\n\nRun `dune build forty_two.exe` to compile the program and\n`_build/default/forty_two.exe` to run it!\n\nYou can also use Tensorflow via utop.\n\n![utop](./bin/utop.png)\n\n\n### Optional step for GPU support\n\nThe TensorFlow library installed via opam does not support GPU acceleration.\nIn order to use your GPU you will have to install TensorFlow 1.14, either\nby building it from source or by using prebuilt binaries. Then \nthe library should be installed system-wide or you could set the\n`LIBTENSORFLOW` environment variable.\n\n```bash\n    export LIBTENSORFLOW={path_to_folder_with_libtensorflow.so}\n``` \n\nPossible ways to get the TensorFlow library:\n\n* __Use prebuilt binaries from Google__. The releases are available for download in URLs of the form: `https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-TYPE-OS-ARCH-VERSION.tar.gz`. For example:\n    * CPU-only, Linux, x86_64.\n    [[1.0.0]](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-1.0.0.tar.gz)\n    [[1.14.0]](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-1.14.0.tar.gz)\n    * GPU-enabled, Linux, x86_64.\n    [[1.0.0]](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-linux-x86_64-1.0.0.tar.gz)\n    [[1.14.0]](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-linux-x86_64-1.14.0.tar.gz)\n    * CPU-only, OS X, x86_64.\n    [[1.0.0]](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-darwin-x86_64-1.0.0.tar.gz)\n    [[1.14.0]](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-darwin-x86_64-1.14.0.tar.gz)\n    * GPU-enabled, OS X, x86_64.\n    [[1.0.0]](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-darwin-x86_64-1.0.0.tar.gz)\n    [[1.14.0]](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-darwin-x86_64-1.14.0.tar.gz)\n\n* __Build the library from source__. Perform the following steps:\n    1. Install the [Bazel build system](http://bazel.io/docs/install.html).\n    1. Clone the TensorFlow repo:\n\n        `git clone --recurse-submodules -b r1.14 https://github.com/tensorflow/tensorflow`\n    1. Configure the build (you will be asked if you want to enable CUDA support):\n    \n        ```\n        cd tensorflow/\n        ./configure\n        ```\n    1. Compile the library:\n\n       `bazel build -c opt tensorflow:libtensorflow.so`\n       \n       The binary should appear under `bazel-bin/tensorflow/libtensorflow.so`.\n\n## Examples\n\n__Tensorflow-ocaml__ includes two different APIs to write graphs.\n\n### Using the Graph API\n\nThe graph API is very close to the original TensorFlow API.\n\n* Some MNIST based tutorials are available in the [examples directory](https://github.com/LaurentMazare/tensorflow-ocaml/tree/master/examples/mnist).\n  A simple Convolutional Neural Network can be defined as follows:\n  ```ocaml\n  let ys_ =\n    O.Placeholder.to_node xs\n    |\u003e Layer.reshape ~shape:[ -1; 28; 28; 1 ]\n    |\u003e Layer.conv2d ~ksize:(5, 5) ~strides:(1, 1) ~output_dim:32\n    |\u003e Layer.max_pool ~ksize:(2, 2) ~strides:(2, 2)\n    |\u003e Layer.conv2d ~ksize:(5, 5) ~strides:(1, 1) ~output_dim:64\n    |\u003e Layer.max_pool ~ksize:(2, 2) ~strides:(2, 2)\n    |\u003e Layer.flatten\n    |\u003e Layer.linear ~output_dim:1024 ~activation:Relu\n    |\u003e O.dropout ~keep_prob:(O.Placeholder.to_node keep_prob)\n    |\u003e Layer.linear ~output_dim:10 ~activation:Softmax\n  in\n  ```\n\n* `examples/load/load.ml` contains a simple example where the TensorFlow graph is loaded from a file (this graph has been generated by `examples/load.py`),\n* `examples/basics` contains some curve fitting examples. You will need gnuplot to be installed via opam to run the gnuplot versions.\n\n### Using the FNN API\n\nThe FNN API is a layer based API to easily build neural-networks. A linear classifier could be defined and trained in a couple lines:\n\n```ocaml\n  let input, input_id = Fnn.input ~shape:(D1 image_dim) in\n  let model =\n    Fnn.dense label_count input\n    |\u003e Fnn.softmax\n    |\u003e Fnn.Model.create Float\n  in\n  Fnn.Model.fit model\n    ~loss:(Fnn.Loss.cross_entropy `mean)\n    ~optimizer:(Fnn.Optimizer.gradient_descent ~learning_rate:8.)\n    ~epochs\n    ~input_id\n    ~xs:train_images\n    ~ys:train_labels;\n```\nA complete VGG-19 model can be defined as follows:\n\n```ocaml\nlet vgg19 () =\n  let block iter ~block_idx ~out_channels x =\n    List.init iter ~f:Fn.id\n    |\u003e List.fold ~init:x ~f:(fun acc idx -\u003e\n      Fnn.conv2d () acc\n        ~name:(sprintf \"conv%d_%d\" block_idx (idx+1))\n        ~w_init:(`normal 0.1) ~filter:(3, 3) ~strides:(1, 1) ~padding:`same ~out_channels\n      |\u003e Fnn.relu)\n    |\u003e Fnn.max_pool ~filter:(2, 2) ~strides:(2, 2) ~padding:`same\n  in\n  let input, input_id = Fnn.input ~shape:(D3 (img_size, img_size, 3)) in\n  let model =\n    Fnn.reshape input ~shape:(D3 (img_size, img_size, 3))\n    |\u003e block 2 ~block_idx:1 ~out_channels:64\n    |\u003e block 2 ~block_idx:2 ~out_channels:128\n    |\u003e block 4 ~block_idx:3 ~out_channels:256\n    |\u003e block 4 ~block_idx:4 ~out_channels:512\n    |\u003e block 4 ~block_idx:5 ~out_channels:512\n    |\u003e Fnn.flatten\n    |\u003e Fnn.dense ~name:\"fc6\" ~w_init:(`normal 0.1) 4096\n    |\u003e Fnn.relu\n    |\u003e Fnn.dense ~name:\"fc7\" ~w_init:(`normal 0.1) 4096\n    |\u003e Fnn.relu\n    |\u003e Fnn.dense ~name:\"fc8\" ~w_init:(`normal 0.1) 1000\n    |\u003e Fnn.softmax\n    |\u003e Fnn.Model.create Float\n  in\n  input_id, model\n```\nThis model is used in the [following example](https://github.com/LaurentMazare/tensorflow-ocaml/blob/master/examples/neural-style/vgg19.ml) to classify an input image. In order to use it you will have to download the [pre-trained weights](https://github.com/LaurentMazare/tensorflow-ocaml/releases/download/0.0.7/vgg19.cpkt).\n\nThere are also some MNIST based [examples](https://github.com/LaurentMazare/tensorflow-ocaml/tree/master/examples/fnn).\n\n### Other Examples\n\nThe examples directory contains various models among which:\n\n* A simplified version of\n  [char-rnn](https://github.com/LaurentMazare/tensorflow-ocaml/blob/master/examples/char_rnn)\n  illustrating character level language modeling using Recurrent Neural Networks.\n* [Neural Style Transfer](https://github.com/LaurentMazare/tensorflow-ocaml/blob/master/examples/neural-style)\n  applies the style of an image to the content of another image. This uses some deep Convolutional Neural Network.\n* Some variants of [Generative Adversarial Networks](https://github.com/LaurentMazare/tensorflow-ocaml/blob/master/examples/gan).\n  These are used to generate MNIST like images.\n\n## Dependencies\n\n* [dune](https://github.com/ocaml/dune) is used as a build system.\n* [ocaml-ctypes](https://github.com/ocamllabs/ocaml-ctypes) is used for the C bindings.\n* [Base](https://github.com/janestreet/base) is only necessary when generating the TensorFlow graph from OCaml, the wrapper itself does not need it.\n* The code in the piqi directory comes from the [Piqi project](http://piqi.org). There is no need to install piqi though.\n* [Cmdliner](https://github.com/dbuenzli/cmdliner) is used for command line interfaces.\n* [Gnuplot-ocaml](https://bitbucket.org/ogu/gnuplot-ocaml) is an optional dependency used by a couple examples.\n* [npy-ocaml](https://github.com/LaurentMazare/npy-ocaml) is used to read/write from npy/npz files.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLaurentMazare%2Ftensorflow-ocaml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FLaurentMazare%2Ftensorflow-ocaml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLaurentMazare%2Ftensorflow-ocaml/lists"}