{"id":20123219,"url":"https://github.com/laurentmazare/ocaml-tensorflow-eager","last_synced_at":"2025-05-06T16:33:48.648Z","repository":{"id":146030023,"uuid":"105582785","full_name":"LaurentMazare/ocaml-tensorflow-eager","owner":"LaurentMazare","description":"OCaml bindings for TensorFlow Eager mode","archived":false,"fork":false,"pushed_at":"2018-10-05T23:20:44.000Z","size":975,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-09T14:40:45.320Z","etag":null,"topics":["bindings","eager","ocaml","tensorflow"],"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":"2017-10-02T20:41:22.000Z","updated_at":"2024-09-18T16:42:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"71bdf0c2-417b-4d90-aee2-0224b3eebea8","html_url":"https://github.com/LaurentMazare/ocaml-tensorflow-eager","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaurentMazare%2Focaml-tensorflow-eager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaurentMazare%2Focaml-tensorflow-eager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaurentMazare%2Focaml-tensorflow-eager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaurentMazare%2Focaml-tensorflow-eager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LaurentMazare","download_url":"https://codeload.github.com/LaurentMazare/ocaml-tensorflow-eager/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252721118,"owners_count":21793756,"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":["bindings","eager","ocaml","tensorflow"],"created_at":"2024-11-13T19:43:57.797Z","updated_at":"2025-05-06T16:33:48.635Z","avatar_url":"https://github.com/LaurentMazare.png","language":"OCaml","funding_links":[],"categories":[],"sub_categories":[],"readme":"Experimental [OCaml](http://ocaml.org) bindings for [TensorFlow](http://tensorflow.org) [Eager execution](https://github.com/tensorflow/tensorflow/tree/r1.5/tensorflow/contrib/eager).\n\nThese bindings are pretty much out of date. Some bindings for [PyTorch](https://pytorch.org)\ncan be found in the [ocaml-torch repo](https://github.com/LaurentMazare/ocaml-torch).\n\n\nWhen using TensorFlow Eager execution, operations are executed immediately in the\nsame way as [PyTorch](http://pytorch.org/). The computation graph is dynamic.\n\n## Examples\n\n### Hello World!\nA very simple example performing an addition using TensorFlow can be seen below:\n\n```ocaml\nmodule O = Tf_ops.Ops\n\nlet () =\n  let twenty_one = O.f32 21. in\n  let forty_two = O.(twenty_one + twenty_one) in\n  O.print forty_two\n```\n\n### Linear Model for Mnist\n\nIn this example, we show how to build a linear classifier for the MNIST\ndataset. This requires you to download the [MNIST data files](http://yann.lecun.com/exdb/mnist/).\n\nGradients are computed using `Gradients.compute`. This function returns the\ngradients with respect to *watched* tensors. In order to watch a variable use\n`Var.read_and_watch` as in the example below.\n\nOnce the gradients have been computed, `Gradients.apply_sgd_step` is used\nto update the variables via a gradient descent step.\n\n```ocaml\nopen Base\nopen Tf_ops\nmodule O = Ops\n\n(* This should reach ~92% accuracy. *)\nlet image_dim = Helper.image_dim\nlet label_count = Helper.label_count\nlet training_steps = 500\nlet batch_size = 512\n\nlet () =\n  let mnist_dataset = Helper.read_files \"data\" in\n  let test_images = Helper.test_images mnist_dataset in\n  let test_labels = Helper.test_labels mnist_dataset in\n\n  (* Create the variables for the linear model. *)\n  let w = Var.f32 [ image_dim; label_count ] 0. in\n  let b = Var.f32 [ label_count ] 0. in\n\n  (* Build the model. [read_and_watch] returns the current value of a variable\n     and ensures that gradients wrt this variable will be computed. *)\n  let model xs =\n    let w_read = Var.read_and_watch w in\n    let b_read = Var.read_and_watch b in\n    O.(xs *^ w_read + b_read) |\u003e O.softmax\n  in\n\n  for step_index = 1 to training_steps do\n    (*Every so often, print the accuracy on the test dataset. *)\n    if step_index % 50 = 0 then begin\n      let accuracy =\n        O.(equal (arg_max (model test_images)) (arg_max test_labels))\n        |\u003e O.cast ~type_dstT:Float\n        |\u003e O.reduce_mean\n        |\u003e O.to_float\n      in\n      Stdio.printf \"step index %d, accuracy %.2f%%\\n%!\" step_index (100. *. accuracy)\n    end;\n\n    (* Get a training batch, apply the model and compute the loss. *)\n    let train_images, train_labels =\n      Helper.train_batch mnist_dataset ~batch_size ~batch_idx:step_index\n    in\n    let ys = model train_images in\n    let cross_entropy = O.cross_entropy ~ys:train_labels ~y_hats:ys `mean in\n\n    (* Compute the loss gradients and apply gradient descent to minimize it. *)\n    let gradients = Tf_ops.Gradients.compute cross_entropy in\n    Tf_ops.Gradients.apply_sgd_step gradients ~learning_rate:8.\n  done\n```\n\n## Installation\n\nIn order to build this on linux, download the [TensorFlow 1.7.0 binaries](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-1.7.0.tar.gz). If this is unpacked at `TFPATH` compiling can be done via:\n```bash\nLD_LIBRARY_PATH=$TFPATH/lib:$LD_LIBRARY_PATH LIBRARY_PATH=$TFPATH/lib:$LIBRARY_PATH make all\n```\n\nFor the [VGG-19 example](https://github.com/LaurentMazare/ocaml-tf/tree/master/examples/vgg19.ml), the weights are available [here](http://download.tensorflow.org/models/vgg_19_2016_08_28.tar.gz).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaurentmazare%2Focaml-tensorflow-eager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaurentmazare%2Focaml-tensorflow-eager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaurentmazare%2Focaml-tensorflow-eager/lists"}