A WebGL accelerated JavaScript library for training and deploying ML models.
Installations
npm install @tensorflow/tfjs-layers
Score
66.7
Supply Chain
95.8
Quality
92.3
Maintenance
100
Vulnerability
71.1
License
Releases
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
18.13.0
NPM Version
8.19.3
Statistics
18,512 Stars
6,100 Commits
1,935 Forks
327 Watching
467 Branches
368 Contributors
Updated on 27 Nov 2024
Bundle Size
219.19 kB
Minified
53.24 kB
Minified + Gzipped
Languages
TypeScript (80.15%)
JavaScript (8.52%)
C++ (4.51%)
Python (3.77%)
Starlark (1.32%)
HTML (0.92%)
Shell (0.51%)
CSS (0.19%)
Objective-C (0.04%)
Java (0.03%)
Ruby (0.01%)
Batchfile (0.01%)
C (0.01%)
Dockerfile (0.01%)
Total Downloads
Cumulative downloads
Total Downloads
19,134,640
Last day
-3.5%
22,604
Compared to previous day
Last week
4.5%
125,980
Compared to previous week
Last month
12.9%
508,699
Compared to previous month
Last year
-19%
5,036,637
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
1
Dev Dependencies
2
TensorFlow.js
TensorFlow.js is an open-source hardware-accelerated JavaScript library for training and deploying machine learning models.
Develop ML in the Browser
Use flexible and intuitive APIs to build models from scratch using the low-level
JavaScript linear algebra library or the high-level layers API.
Develop ML in Node.js
Execute native TensorFlow with the same TensorFlow.js API under the Node.js
runtime.
Run Existing models
Use TensorFlow.js model converters to run pre-existing TensorFlow models right
in the browser.
Retrain Existing models
Retrain pre-existing ML models using sensor data connected to the browser or
other client-side data.
About this repo
This repository contains the logic and scripts that combine several packages.
APIs:
- TensorFlow.js Core, a flexible low-level API for neural networks and numerical computation.
- TensorFlow.js Layers, a high-level API which implements functionality similar to Keras.
- TensorFlow.js Data, a simple API to load and prepare data analogous to tf.data.
- TensorFlow.js Converter, tools to import a TensorFlow SavedModel to TensorFlow.js
- TensorFlow.js Vis, in-browser visualization for TensorFlow.js models
- TensorFlow.js AutoML, Set of APIs to load and run models produced by AutoML Edge.
Backends/Platforms:
- TensorFlow.js CPU Backend, pure-JS backend for Node.js and the browser.
- TensorFlow.js WebGL Backend, WebGL backend for the browser.
- TensorFlow.js WASM Backend, WebAssembly backend for the browser.
- TensorFlow.js WebGPU, WebGPU backend for the browser.
- TensorFlow.js Node, Node.js platform via TensorFlow C++ adapter.
- TensorFlow.js React Native, React Native platform via expo-gl adapter.
If you care about bundle size, you can import those packages individually.
If you are looking for Node.js support, check out the TensorFlow.js Node directory.
Examples
Check out our examples repository and our tutorials.
Gallery
Be sure to check out the gallery of all projects related to TensorFlow.js.
Pre-trained models
Be sure to also check out our models repository where we host pre-trained models on NPM.
Benchmarks
- Local benchmark tool. Use this webpage tool to collect the performance related metrics (speed, memory, etc) of TensorFlow.js models and kernels on your local device with CPU, WebGL or WASM backends. You can benchmark custom models by following this guide.
- Multi-device benchmark tool. Use this tool to collect the same performance related metrics on a collection of remote devices.
Getting started
There are two main ways to get TensorFlow.js in your JavaScript project: via script tags or by installing it from NPM and using a build tool like Parcel, WebPack, or Rollup.
via Script Tag
Add the following code to an HTML file:
1<html> 2 <head> 3 <!-- Load TensorFlow.js --> 4 <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"> </script> 5 6 7 <!-- Place your code in the script tag below. You can also use an external .js file --> 8 <script> 9 // Notice there is no 'import' statement. 'tf' is available on the index-page 10 // because of the script tag above. 11 12 // Define a model for linear regression. 13 const model = tf.sequential(); 14 model.add(tf.layers.dense({units: 1, inputShape: [1]})); 15 16 // Prepare the model for training: Specify the loss and the optimizer. 17 model.compile({loss: 'meanSquaredError', optimizer: 'sgd'}); 18 19 // Generate some synthetic data for training. 20 const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]); 21 const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]); 22 23 // Train the model using the data. 24 model.fit(xs, ys).then(() => { 25 // Use the model to do inference on a data point the model hasn't seen before: 26 // Open the browser devtools to see the output 27 model.predict(tf.tensor2d([5], [1, 1])).print(); 28 }); 29 </script> 30 </head> 31 32 <body> 33 </body> 34</html>
Open up that HTML file in your browser, and the code should run!
via NPM
Add TensorFlow.js to your project using yarn or npm. Note: Because
we use ES2017 syntax (such as import
), this workflow assumes you are using a modern browser or a bundler/transpiler
to convert your code to something older browsers understand. See our
examples
to see how we use Parcel to build
our code. However, you are free to use any build tool that you prefer.
1import * as tf from '@tensorflow/tfjs'; 2 3// Define a model for linear regression. 4const model = tf.sequential(); 5model.add(tf.layers.dense({units: 1, inputShape: [1]})); 6 7// Prepare the model for training: Specify the loss and the optimizer. 8model.compile({loss: 'meanSquaredError', optimizer: 'sgd'}); 9 10// Generate some synthetic data for training. 11const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]); 12const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]); 13 14// Train the model using the data. 15model.fit(xs, ys).then(() => { 16 // Use the model to do inference on a data point the model hasn't seen before: 17 model.predict(tf.tensor2d([5], [1, 1])).print(); 18});
See our tutorials, examples and documentation for more details.
Importing pre-trained models
We support porting pre-trained models from:
Various ops supported in different backends
Please refer below :
Find out more
TensorFlow.js is a part of the TensorFlow ecosystem. For more info:
- For help from the community, use the
tfjs
tag on the TensorFlow Forum. - TensorFlow.js Website
- Tutorials
- API reference
- TensorFlow.js Blog
Thanks, BrowserStack, for providing testing support.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
all changesets reviewed
Reason
GitHub workflow tokens follow principle of least privilege
Details
- Info: topLevel 'contents' permission set to 'read': .github/workflows/deploy-benchmark-preview.yml:9
- Info: topLevel 'contents' permission set to 'read': .github/workflows/deploy-benchmark-prod.yml:11
- Info: topLevel 'contents' permission set to 'read': .github/workflows/stale.yaml:26
- Info: no jobLevel write permissions found
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: Apache License 2.0: LICENSE:0
Reason
binaries present in source code
Details
- Warn: binary detected: tfjs-react-native/integration_rn59/android/gradle/wrapper/gradle-wrapper.jar:1
Reason
5 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 4
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/deploy-benchmark-preview.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/tensorflow/tfjs/deploy-benchmark-preview.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/deploy-benchmark-preview.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/tensorflow/tfjs/deploy-benchmark-preview.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/deploy-benchmark-prod.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/tensorflow/tfjs/deploy-benchmark-prod.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/deploy-benchmark-prod.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/tensorflow/tfjs/deploy-benchmark-prod.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/stale.yaml:33: update your workflow using https://app.stepsecurity.io/secureworkflow/tensorflow/tfjs/stale.yaml/master?enable=pin
- Warn: containerImage not pinned by hash: dockers/release-docker/Dockerfile:1: pin your Docker image by updating node:18.13.0-bullseye-slim to node:18.13.0-bullseye-slim@sha256:bc946484118735406562f17c57ddf5fded436e175b6a51f827aa6540ba1e13de
- Warn: containerImage not pinned by hash: dockers/wasm-docker/Dockerfile:2: pin your Docker image by updating emscripten/emsdk:2.0.14 to emscripten/emsdk:2.0.14@sha256:d409c163cc3761baaadc8371af47f8a1465c9d4215a6a75ad15ac6c5dd05022f
- Warn: pipCommand not pinned by hash: dockers/release-docker/Dockerfile:8-38
- Warn: pipCommand not pinned by hash: dockers/release-docker/Dockerfile:45
- Warn: pipCommand not pinned by hash: dockers/release-docker/Dockerfile:48
- Warn: npmCommand not pinned by hash: dockers/wasm-docker/Dockerfile:5
- Warn: pipCommand not pinned by hash: dockers/wasm-docker/Dockerfile:16
- Warn: npmCommand not pinned by hash: e2e/scripts/release-e2e.sh:76
- Warn: pipCommand not pinned by hash: e2e/scripts/setup-py-env.sh:53
- Warn: pipCommand not pinned by hash: e2e/scripts/setup-py-env.sh:55
- Warn: pipCommand not pinned by hash: e2e/scripts/setup-py-env.sh:59
- Warn: pipCommand not pinned by hash: tfjs-converter/python/build-pip-package.sh:128
- Warn: pipCommand not pinned by hash: tfjs-converter/python/build-pip-package.sh:190
- Warn: pipCommand not pinned by hash: tfjs-converter/python/build-pip-package.sh:251
- Warn: pipCommand not pinned by hash: tfjs-converter/python/run-python-tests.sh:31
- Warn: pipCommand not pinned by hash: tfjs-converter/python/run-python-tests.sh:54
- Warn: pipCommand not pinned by hash: tfjs-inference/scripts/run_python.sh:20
- Warn: pipCommand not pinned by hash: tfjs-inference/scripts/run_python.sh:28
- Info: 0 out of 3 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 third-party GitHubAction dependencies pinned
- Info: 0 out of 2 containerImage dependencies pinned
- Info: 0 out of 14 pipCommand dependencies pinned
- Info: 0 out of 2 npmCommand dependencies pinned
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 30 are checked with a SAST tool
Reason
110 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-7v5v-9h63-cj86
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-36jr-mh4h-2g58
- Warn: Project is vulnerable to: GHSA-r7qp-cfhv-p84w
- Warn: Project is vulnerable to: GHSA-q9mw-68c2-j6m5
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-hhhv-q57g-882q
- Warn: Project is vulnerable to: GHSA-8cf7-32gw-wr33
- Warn: Project is vulnerable to: GHSA-hjrf-2m68-5959
- Warn: Project is vulnerable to: GHSA-qwph-4952-7xr6
- Warn: Project is vulnerable to: GHSA-h755-8qp9-cq85
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-25hc-qcg6-38wj
- Warn: Project is vulnerable to: GHSA-cqmj-92xf-r6r9
- Warn: Project is vulnerable to: GHSA-mxhp-79qh-mcx6
- Warn: Project is vulnerable to: GHSA-4vq7-882g-wcg4
- Warn: Project is vulnerable to: GHSA-w5m3-xh75-mp55
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-7hpj-7hhx-2fgx
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-hxcc-f52p-wc94
- Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-c7qv-q95q-8v27
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-wr3j-pwj9-hqq6
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w7rc-rwvf-8q5r
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-cx63-2mw6-8hw5
- Warn: Project is vulnerable to: GHSA-248v-346w-9cwc
- Warn: Project is vulnerable to: PYSEC-2023-135 / GHSA-xqr8-7jwr-rhp7
- Warn: Project is vulnerable to: GHSA-496j-2rq6-j6cc
- Warn: Project is vulnerable to: GHSA-6628-q6j9-w8vg
- Warn: Project is vulnerable to: GHSA-9hxf-ppjv-w6rq
- Warn: Project is vulnerable to: GHSA-cfgp-2977-2fmm
- Warn: Project is vulnerable to: PYSEC-2024-60 / GHSA-jjg7-2v4v-x38h
- Warn: Project is vulnerable to: PYSEC-2023-117 / GHSA-mrwq-x4v8-fh7p
- Warn: Project is vulnerable to: GHSA-9wx4-h78v-vm56
- Warn: Project is vulnerable to: PYSEC-2023-74 / GHSA-j8r2-6x86-q33q
- Warn: Project is vulnerable to: PYSEC-2023-102
- Warn: Project is vulnerable to: GHSA-34jh-p97f-mpxf
- Warn: Project is vulnerable to: PYSEC-2023-212 / GHSA-g4mx-q9vg-27p4
- Warn: Project is vulnerable to: PYSEC-2023-192 / GHSA-v845-jxx5-vc9f
- Warn: Project is vulnerable to: GHSA-2g68-c3qc-8985
- Warn: Project is vulnerable to: GHSA-f9vj-2wh5-fj8j
- Warn: Project is vulnerable to: PYSEC-2023-221 / GHSA-hrfv-mqp8-q5rw
- Warn: Project is vulnerable to: PYSEC-2023-57 / GHSA-px8h-6qxv-m22q
- Warn: Project is vulnerable to: GHSA-q34m-jh98-gwm2
- Warn: Project is vulnerable to: PYSEC-2023-58 / GHSA-xg9f-g7g7-2323
- Warn: Project is vulnerable to: GHSA-jfmj-5v4g-7637
- Warn: Project is vulnerable to: GHSA-22r3-9w55-cj54
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-phwq-j96m-2c2q
- Warn: Project is vulnerable to: GHSA-ghr5-ch3p-vcr6
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-pc5p-h8pf-mvwp
- Warn: Project is vulnerable to: GHSA-w7q9-p3jq-fmhm
- Warn: Project is vulnerable to: GHSA-xvf7-4v9q-58w6
- Warn: Project is vulnerable to: GHSA-4xcv-9jjx-gfj3
- Warn: Project is vulnerable to: GHSA-7wpw-2hjm-89gp
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
- Warn: Project is vulnerable to: GHSA-rxrc-rgv4-jpvx
- Warn: Project is vulnerable to: GHSA-7f53-fmmv-mfjv
- Warn: Project is vulnerable to: GHSA-g4rg-993r-mgx7
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
- Warn: Project is vulnerable to: GHSA-pfrx-2q88-qq97
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-crh6-fp67-6883
- Warn: Project is vulnerable to: GHSA-p3vf-v8qc-cwcr
- Warn: Project is vulnerable to: GHSA-gx9m-whjm-85jf
- Warn: Project is vulnerable to: GHSA-mmhx-hmjr-r674
- Warn: Project is vulnerable to: GHSA-4gmj-3p3h-gm8h
- Warn: Project is vulnerable to: GHSA-74fj-2j2h-c42q
- Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c
- Warn: Project is vulnerable to: GHSA-x3m3-4wpv-5vgc
Score
5.1
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to @tensorflow/tfjs-layers
@tensorflow/tfjs-core
Hardware-accelerated JavaScript library for machine intelligence
@tensorflow/tfjs
An open-source machine learning framework.
@tensorflow/tfjs-backend-cpu
Vanilla JavaScript backend for TensorFlow.js
@tensorflow/tfjs-backend-webgl
GPU accelerated WebGL backend for TensorFlow.js