{"id":13801734,"url":"https://github.com/eaplatanios/tensorflow_scala","last_synced_at":"2025-08-18T10:24:38.734Z","repository":{"id":37587394,"uuid":"86932157","full_name":"eaplatanios/tensorflow_scala","owner":"eaplatanios","description":"TensorFlow API for the Scala Programming Language","archived":false,"fork":false,"pushed_at":"2022-06-22T21:53:34.000Z","size":46935,"stargazers_count":939,"open_issues_count":29,"forks_count":95,"subscribers_count":65,"default_branch":"master","last_synced_at":"2024-11-15T07:33:10.574Z","etag":null,"topics":["deep-learning","machine-learning","scala","tensorflow"],"latest_commit_sha":null,"homepage":"http://platanios.org/tensorflow_scala/","language":"Scala","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/eaplatanios.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}},"created_at":"2017-04-01T18:00:29.000Z","updated_at":"2024-10-28T21:40:28.000Z","dependencies_parsed_at":"2022-07-12T16:32:20.017Z","dependency_job_id":null,"html_url":"https://github.com/eaplatanios/tensorflow_scala","commit_stats":null,"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eaplatanios%2Ftensorflow_scala","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eaplatanios%2Ftensorflow_scala/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eaplatanios%2Ftensorflow_scala/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eaplatanios%2Ftensorflow_scala/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eaplatanios","download_url":"https://codeload.github.com/eaplatanios/tensorflow_scala/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253932952,"owners_count":21986484,"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":["deep-learning","machine-learning","scala","tensorflow"],"created_at":"2024-08-04T00:01:26.557Z","updated_at":"2025-05-13T11:31:44.647Z","avatar_url":"https://github.com/eaplatanios.png","language":"Scala","funding_links":[],"categories":["Table of Contents","人工智能","Scala","Swift Tools and Frameworks","C/C++ Tools and Frameworks","Science and Data Analysis"],"sub_categories":["Science and Data Analysis","机器学习","General-Purpose Machine Learning"],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/eaplatanios/tensorflow_scala/master/docs/images/logo.svg?sanitize=true\" width=\"350px\" height=\"242px\"\u003e\u003cbr\u003e\n\u003c/div\u003e\n\n-----------------\n\n[![CircleCI](https://img.shields.io/circleci/project/github/eaplatanios/tensorflow_scala.svg?style=flat-square)](https://circleci.com/gh/eaplatanios/tensorflow_scala/tree/master)\n[![Codacy Badge](https://img.shields.io/codacy/grade/7fae7fba84df4831a80bc20c3bd021df.svg?style=flat-square)](https://www.codacy.com/app/eaplatanios/tensorflow_scala?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=eaplatanios/tensorflow_scala\u0026amp;utm_campaign=Badge_Grade)\n![License](https://img.shields.io/github/license/eaplatanios/tensorflow_scala.svg?style=flat-square)\n[![API Docs](https://img.shields.io/badge/docs-api-lightgrey.svg?longCache=true\u0026style=flat-square\u0026logo=read-the-docs\u0026logoColor=white)](http://platanios.org/tensorflow_scala/api/api)\n[![JNI Docs](https://img.shields.io/badge/docs-jni-lightgrey.svg?longCache=true\u0026style=flat-square\u0026logo=read-the-docs\u0026logoColor=white)](http://platanios.org/tensorflow_scala/api/jni)\n[![Data Docs](https://img.shields.io/badge/docs-data-lightgrey.svg?longCache=true\u0026style=flat-square\u0026logo=read-the-docs\u0026logoColor=white)](http://platanios.org/tensorflow_scala/api/data)\n[![Examples Docs](https://img.shields.io/badge/docs-examples-lightgrey.svg?longCache=true\u0026style=flat-square\u0026logo=read-the-docs\u0026logoColor=white)](http://platanios.org/tensorflow_scala/api/examples)\n\nThis library is a Scala API for [https://www.tensorflow.org](https://www.tensorflow.org). It attempts to provide most of\nthe functionality provided by the official Python API, while at the same type being strongly-typed and adding some new\nfeatures. It is a work in progress and a project I started working on for my personal research purposes. Much of the API\nshould be relatively stable by now, but things are still likely to change.\n\n[![Chat Room](https://img.shields.io/badge/chat-gitter-ed1965.svg?longCache=true\u0026style=flat-square\u0026logo=gitter)](https://gitter.im/eaplatanios/tensorflow_scala?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\nPlease refer to the main website for documentation and tutorials. Here\nare a few useful links:\n\n  - [Installation](https://eaplatanios.github.io/tensorflow_scala/installation.html)\n  - [Getting Started Guide](https://eaplatanios.github.io/tensorflow_scala/getting_started.html)\n  - [Library Architecture](https://eaplatanios.github.io/tensorflow_scala/architecture.html)\n  - [Contributing](https://eaplatanios.github.io/tensorflow_scala/contributing.html)\n\n## Citation\n\nIt would be greatly appreciated if you could cite this project using the following BibTex entry, if you end up using it\nin your work:\n\n```bibtex\n@misc{Platanios:2018:tensorflow-scala,\n  title        = {{TensorFlow Scala}},\n  author       = {Platanios, Emmanouil Antonios},\n  howpublished = {\\url{https://github.com/eaplatanios/tensorflow_scala}},\n  year         = {2018}\n}\n```\n\n## Main Features\n\n  - Easy manipulation of tensors and computations involving tensors (similar to NumPy in Python):\n\n    ```scala\n    val t1 = Tensor(1.2, 4.5)\n    val t2 = Tensor(-0.2, 1.1)\n    t1 + t2 == Tensor(1.0, 5.6)\n    ```\n\n  - Low-level graph construction API, similar to that of the Python API, but strongly typed wherever possible:\n\n    ```scala\n    val inputs      = tf.placeholder[Float](Shape(-1, 10))\n    val outputs     = tf.placeholder[Float](Shape(-1, 10))\n    val predictions = tf.nameScope(\"Linear\") {\n      val weights = tf.variable[Float](\"weights\", Shape(10, 1), tf.ZerosInitializer)\n      tf.matmul(inputs, weights)\n    }\n    val loss        = tf.sum(tf.square(predictions - outputs))\n    val optimizer   = tf.train.AdaGrad(1.0f)\n    val trainOp     = optimizer.minimize(loss)\n    ```\n\n  - Numpy-like indexing/slicing for tensors. For example:\n\n    ```scala\n    tensor(2 :: 5, ---, 1) // is equivalent to numpy's 'tensor[2:5, ..., 1]'\n    ```\n\n  - High-level API for creating, training, and using neural networks. For example, the following code shows how simple it\n    is to train a multi-layer perceptron for MNIST using TensorFlow for Scala. Here we omit a lot of very powerful\n    features such as summary and checkpoint savers, for simplicity, but these are also very simple to use.\n\n    ```scala\n    // Load and batch data using pre-fetching.\n    val dataset = MNISTLoader.load(Paths.get(\"/tmp\"))\n    val trainImages = tf.data.datasetFromTensorSlices(dataset.trainImages.toFloat)\n    val trainLabels = tf.data.datasetFromTensorSlices(dataset.trainLabels.toLong)\n    val trainData =\n      trainImages.zip(trainLabels)\n          .repeat()\n          .shuffle(10000)\n          .batch(256)\n          .prefetch(10)\n\n    // Create the MLP model.\n    val input = Input(FLOAT32, Shape(-1, 28, 28))\n    val trainInput = Input(INT64, Shape(-1))\n    val layer = Flatten[Float](\"Input/Flatten\") \u003e\u003e\n        Linear[Float](\"Layer_0\", 128) \u003e\u003e ReLU[Float](\"Layer_0/Activation\", 0.1f) \u003e\u003e\n        Linear[Float](\"Layer_1\", 64) \u003e\u003e ReLU[Float](\"Layer_1/Activation\", 0.1f) \u003e\u003e\n        Linear[Float](\"Layer_2\", 32) \u003e\u003e ReLU[Float](\"Layer_2/Activation\", 0.1f) \u003e\u003e\n        Linear[Float](\"OutputLayer\", 10)\n    val loss = SparseSoftmaxCrossEntropy[Float, Long, Float](\"Loss\") \u003e\u003e\n        Mean(\"Loss/Mean\")\n    val optimizer = tf.train.GradientDescent(1e-6f)\n    val model = Model.simpleSupervised(input, trainInput, layer, loss, optimizer)\n\n    // Create an estimator and train the model.\n    val estimator = InMemoryEstimator(model)\n    estimator.train(() =\u003e trainData, StopCriteria(maxSteps = Some(1000000)))\n    ```\n\n    And by changing a few lines to the following code, you can get checkpoint capability, summaries, and seamless\n    integration with TensorBoard:\n\n    ```scala\n    val loss = SparseSoftmaxCrossEntropy[Float, Long, Float](\"Loss\") \u003e\u003e\n        Mean(\"Loss/Mean\") \u003e\u003e\n        ScalarSummary(name = \"Loss\", tag = \"Loss\")\n    val summariesDir = Paths.get(\"/tmp/summaries\")\n    val estimator = InMemoryEstimator(\n      modelFunction = model,\n      configurationBase = Configuration(Some(summariesDir)),\n      trainHooks = Set(\n        SummarySaver(summariesDir, StepHookTrigger(100)),\n        CheckpointSaver(summariesDir, StepHookTrigger(1000))),\n      tensorBoardConfig = TensorBoardConfig(summariesDir))\n    estimator.train(() =\u003e trainData, StopCriteria(maxSteps = Some(100000)))\n    ```\n\n    If you now browse to `https://127.0.0.1:6006` while training, you can see the training progress:\n\n    \u003cimg src=\"https://platanios.org/tensorflow_scala/assets/images/tensorboard_mnist_example_plot.png\" alt=\"tensorboard_mnist_example_plot\" width=\"600px\"\u003e\n\n  - Efficient interaction with the native library that avoids unnecessary copying of data. All tensors are created and\n    managed by the native TensorFlow library. When they are passed to the Scala API (e.g., fetched from a TensorFlow\n    session), we use a combination of weak references and a disposing thread running in the background. Please refer to\n    `tensorflow/src/main/scala/org/platanios/tensorflow/api/utilities/Disposer.scala`, for the implementation.\n\n## Compiling from Source\n\nNote that in order to compile TensorFlow Scala on your\nmachine you will need to first install the TensorFlow\nPython API. You also need to make sure that you have a\n`python3` alias for your python binary. This is used by\nCMake to find the TensorFlow header files in your\ninstallation.\n\n## Tutorials\n\n- [Object Detection using Pre-Trained Models](https://brunk.io/deep-learning-in-scala-part-3-object-detection.html)\n\n## Funding\n\nFunding for the development of this library has been generously provided by the following sponsors:\n\n|\u003cimg src=\"https://platanios.org/tensorflow_scala/assets/images/cmu_logo.svg\" alt=\"cmu_logo\" width=\"200px\" height=\"150px\"\u003e|\u003cimg src=\"https://platanios.org/tensorflow_scala/assets/images/nsf_logo.svg\" alt=\"nsf_logo\" width=\"150px\" height=\"150px\"\u003e|\u003cimg src=\"https://platanios.org/tensorflow_scala/assets/images/afosr_logo.gif\" alt=\"afosr_logo\" width=\"150px\" height=\"150px\"\u003e|\n|:---------------------------------------:|:---------------------------------:|:-----------------------------------------------:|\n| **CMU Presidential Fellowship**         | **National Science Foundation**   | **Air Force Office of Scientific Research**     | \n| awarded to Emmanouil Antonios Platanios | Grant #: IIS1250956               | Grant #: FA95501710218                          |\n\nTensorFlow, the TensorFlow logo, and any related marks are trademarks of Google Inc.\n\n\u003c!---\n\n## Some TODOs\n\n  - [ ] Figure out what the proper to way to handle Int vs Long shapes is, so that we can use Long shapes without hurting GPU performance.\n  - [ ] Make the optimizers typed (with respect to their state, at least).\n  - [ ] Make the gradients function retain types (we need a type trait for that).\n  - [ ] Dispose dataset iterators automatically.\n  - [ ] Fixed all `[TYPE] !!!` code TODOs.\n\n  - [ ] Session execution context (I'm not sure if that's good to have)\n  - [ ] Session reset functionality\n  - [ ] Variables slicing\n  - [ ] Slice assignment\n  - [ ] Support for `CriticalSection`.\n  - [ ] tfdbg / debugging support\n  - [ ] tfprof / op statistics collection\n\n  - Switch to using JUnit for all tests.\n  - Add convenience implicit conversions for shapes (e.g., from tuples or sequences of integers).\n  - Create a \"Scope\" class and companion object.\n  - Variables API:\n    - Clean up the implementation of variable scopes and stores and integrate it with \"Scope\".\n    - Make 'PartitionedVariable' extend 'Variable'.\n    - After that change, all 'getPartitionedVariable' methods can be integrated with the 'getVariable' methods, which will\n      simplify the variables API.\n  - Switch to using \"Seq\" instead of \"Array\" wherever possible.\n  - Op creation:\n    - Reset default graph\n    - Register op statistics\n  - Fix Travis CI support (somehow load the native library)\n\n- Website margins are a little large relative to the content in mobile\n- Make the code blocks scroll rather than wrap\n\nTo publish a signed snapshot version of the package that is \ncross-compiled, we use the following commands from within\nan SBT shell:\n\n```sbt\nset nativeCrossCompilationEnabled in jni := true\npublishSigned\n```\n\nYou can also test cross-compilation using the following\ncommand:\n\n```bash\nsbt jni/cross:nativeCrossCompile\n```\n\nCUDA Compute Capabilities: 3.5,7.0,7.5,8.0,8.6\n\nCompile the TensorFlow dynamic libraries from source using:\n\n```bash\nbazel build --config=opt --cxxopt=-D_GLIBCXX_USE_CXX11_ABI=0 //tensorflow:libtensorflow.so\n```\n\nOn Ubuntu 18.04 you may get some linking errors, in which case you should use:\n\n```bash\nbazel build --config=opt --cxxopt=-D_GLIBCXX_USE_CXX11_ABI=0 --noincompatible_do_not_split_linking_cmdline //tensorflow:libtensorflow.so\n```\n\nOn Windows you may get some CUDA-related errors, in which case you should use:\n\n```cmd\nbazel build --config=opt --cxxopt=-D_GLIBCXX_USE_CXX11_ABI=0 --define=no_tensorflow_py_deps=true --copt=-DTHRUST_IGNORE_CUB_VERSION_CHECK --copt=-nvcc_options=disable-warnings //tensorflow:tensorflow.lib //tensorflow:tensorflow_framework.lib\n```\n\nFor Mac we also need to deal with this currently:\n\n```bash\ninstall_name_tool -id @rpath/libtensorflow.2.dylib libtensorflow.2.4.0.dylib\ninstall_name_tool -change @rpath/libtensorflow.so.2 @rpath/libtensorflow.2.dylib libtensorflow_framework.2.4.0.dylib\n```\n\nTo publish the documentation website we use the following commands:\n\n```bash\nsbt docs/previewSite     # To preview the website\nsbt docs/ghpagesPushSite # To publish the website\n```\n\nTo prepare the precompiled TensorFlow binary packages, use the following commands:\n\n```bash\nmkdir lib\ncp -av /usr/local/lib/libtensorflow* lib/\ntar -zcvf libtensorflow-2.2.0-cpu-darwin-x86_64.tar.gz lib\ntar -ztvf libtensorflow-2.2.0-cpu-darwin-x86_64.tar.gz\n```\n\n--\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feaplatanios%2Ftensorflow_scala","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feaplatanios%2Ftensorflow_scala","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feaplatanios%2Ftensorflow_scala/lists"}