{"id":13527045,"url":"https://github.com/nikhilk/node-tensorflow","last_synced_at":"2025-04-12T20:44:06.443Z","repository":{"id":56274863,"uuid":"45894047","full_name":"nikhilk/node-tensorflow","owner":"nikhilk","description":"Node.js + TensorFlow","archived":false,"fork":false,"pushed_at":"2020-10-02T13:42:57.000Z","size":91,"stargazers_count":586,"open_issues_count":12,"forks_count":58,"subscribers_count":45,"default_branch":"master","last_synced_at":"2025-04-04T00:44:50.995Z","etag":null,"topics":["deep-learning","javascript","machine-learning","node-tensorflow","nodejs","npm-package","tensorflow"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/nikhilk.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":"2015-11-10T07:04:49.000Z","updated_at":"2025-01-15T08:48:28.000Z","dependencies_parsed_at":"2022-08-15T15:50:36.399Z","dependency_job_id":null,"html_url":"https://github.com/nikhilk/node-tensorflow","commit_stats":{"total_commits":60,"total_committers":4,"mean_commits":15.0,"dds":0.1166666666666667,"last_synced_commit":"db0b0db423f0b8354d29819129c13ed3c8475971"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikhilk%2Fnode-tensorflow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikhilk%2Fnode-tensorflow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikhilk%2Fnode-tensorflow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikhilk%2Fnode-tensorflow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nikhilk","download_url":"https://codeload.github.com/nikhilk/node-tensorflow/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248631687,"owners_count":21136556,"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","javascript","machine-learning","node-tensorflow","nodejs","npm-package","tensorflow"],"created_at":"2024-08-01T06:01:39.835Z","updated_at":"2025-04-12T20:44:06.418Z","avatar_url":"https://github.com/nikhilk.png","language":"JavaScript","readme":"# TensorFlow + Node.js\n\n[TensorFlow](https://tensorflow.org) is Google's machine learning runtime. It\nis implemented as C++ runtime, along with Python framework to support building\na variety of models, especially neural networks for deep learning.\n\nIt is interesting to be able to use TensorFlow in a node.js application\nusing just JavaScript (or TypeScript if that's your preference). However,\nthe Python functionality is vast (several ops, estimator implementations etc.)\nand continually expanding. Instead, it would be more practical to consider\nbuilding Graphs and training models in Python, and then consuming those\nfor runtime use-cases (like prediction or inference) in a pure node.js and\nPython-free deployment. This is what this node module enables.\n\nThis module takes care of the building blocks and mechanics for working\nwith the TensorFlow C API, and instead provides an API around Tensors, Graphs,\nSessions and Models.\n\nThis is still in the works, and recently revamped to support TensorFlow 1.4+.\n\n## High Level Interface - Models\n\nThis is in plan. The idea here is to point to a saved model and be able to\nuse it for predictions. Instances-in, inferences-out.\n\nStay tuned for a future update.\n\n## Low Level Interface - Tensors, Graphs and Sessions\n\n### Trivial Example - Loading and Running Graphs\nLets assume we have a simple TensorFlow graph. For illustration purposes, a\ntrivial graph produced from this Python code, and saved as a GraphDef\nprotocol buffer file.\n\n```python\nimport tensorflow as tf\n\nwith tf.Graph().as_default() as graph:\n  c1 = tf.constant(1, name='c1')\n  c2 = tf.constant(41, name='c2')\n  result = tf.add(c1, c2, name='result')\n\n  tf.train.write_graph(graph, '.', 'trivial.graph.proto', as_text=False)\n```\n\nNow, in node.js, you can load this serialized graph definition, load a\nTensorFlow session, and then run specific operations to retrive tensors.\n\n```javascript\nconst tf = require('tensorflow');\n\n// Load the Graph and create a Session to be able to run the operations\n// defined in the graph.\nlet graph = tf.graph('trivial.graph.proto');\nlet session = graph.createSession();\n\n// Run to evaluate and retrieve the value of the 'result' op.\nlet result = session.run(/* inputs */ null,\n                         /* outputs */ 'result',\n                         /* targets */ null);\n\n// The result is a Tensor, which contains value, type and shape fields.\n// This Should print out '42'\nconsole.log(result.value);\n    \n// Cleanup\ngraph.delete();\n```\n\n### Feeding and Fetching Tensors with a Session\n\nThis example goes a bit further - in particular, the Graph contains\nvariables, and placeholders, requiring initialization as well as feeding values,\nwhen executing the graph. Additionally the Tensors are integer matrices.\n\n```python\nimport tensorflow as tf\n\nwith tf.Graph().as_default() as graph:\n  var1 = tf.placeholder(dtype=tf.int32, shape=[2,2], name='var1')\n  var2 = tf.placeholder(dtype=tf.int32, shape=[2,1], name='var2')\n  var3 = tf.Variable(initial_value=[[1],[1]], dtype=tf.int32)\n\n  tf.variables_initializer(tf.global_variables(), name='init')\n\n  with tf.name_scope('computation'):\n    tf.add(tf.matmul(var1, var2), var3, name='result')\n\n  tf.train.write_graph(graph, '.', 'graph.proto', as_text=False)\n```\n\nHere is the corresponding node.js snippet to work with the Graph defined above:\n\n```javascript\nconst tf = require('tensorflow');\n\nlet graph = tf.graph('graph.proto');\nlet session = graph.createSession();\n\n// Run the 'init' op to initialize variables defined in the graph.\nsession.run(null, null, 'init');\n\n// Generally you can use arrays directly. This samples demonstrates creating\n// Tensors to explicitly specify types to match the int32 types that the graph\n// expects.\nlet a = tf.tensor([[2,2],[4,4]], tf.Types.int32);\nlet b = tf.tensor([[3],[5]], tf.Types.int32);\n\n// You can fetch multiple outputs as well.\nlet outputs = session.run({ var1: a, var2: b }, ['var3', 'computation/result']);\nconsole.log(outputs.var3.value)\nconsole.log(outputs['computation/result'].value);\n    \ngraph.delete();\n```\n\n## Installation\n\nInstallation is pretty straight-forward. Installing this module automatically brings installs\nthe TensorFlow binary dependencies (by default, TensorFlow CPU v1.4.1).\n\n    npm install tensorflow\n\nOptionally, you can specify the build of TensorFlow binaries to install using environment\nvariables.\n\n    export TENSORFLOW_LIB_TYPE=gpu\n    export TENSORFLOW_LIB_VERSION=1.5.0\n    npm install tensorflow\n\nThe TensorFlow binaries automatically installed within the directory containing the node\nmodule. If you have a custom build of TensorFlow you would like to use instead, you can\nsuppress downloadinging the binaries at installation time.\n\n    export TENSORFLOW_LIB_PATH=path-to-custom-binaries\n    npm install tensorflow\n\nNote that the path you specify must be a directory that contains both `libtensorflow.so` and\n`libtensorflow_framework.so`.\n\n\n## TensorFlow Setup and Docs\nNote that to use the Python interface to build TensorFlow graphs and train models, you will\nalso need to install TensorFlow directly within your Python environment.\n\n    pip install tensorflow==1.4.1\n\nFor more information, check out the TensorFlow [install](https://www.tensorflow.org/install)\nand [API](https://www.tensorflow.org/api_docs/) documentation.\n\n\n## In the works, and more to come ...\nSome things on the plan to be tackled.\n\n* Support for high-level API (and saved models representing results of training)\n* Support for Windows\n\nPlease file issues for feature suggestions, bugs or questions.\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikhilk%2Fnode-tensorflow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnikhilk%2Fnode-tensorflow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikhilk%2Fnode-tensorflow/lists"}