Registry / ai-ml / tensorflowjs

tensorflowjs

JSON →
library4.22.0pypypi✓ verified 28d ago

The `tensorflowjs` Python package provides utilities to convert trained Keras or TensorFlow SavedModel models into a format consumable by TensorFlow.js, enabling them to run directly in a web browser or Node.js environment. It facilitates the deployment of machine learning models on the web. The current version is 4.22.0, with releases typically aligned with major TensorFlow versions.

pip install tensorflowjs
INSTALL
IMPORT
SIG · TENSORFLOWJS
T
tensorflowjs
ai-mlpythonv4.22.0
Install
44.2s avg
Import
—
Disk
2697MB
Pass rate
3/ 10
Env Coverage3 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v4.22.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✕ timeout
py 3.11
✕ build_error
✓ 50.2s
py 3.12
✕ build_error
✓ 51.4s
py 3.13
✕ build_error
✓ 31s
py 3.9
✕ build_error
✕ timeout
2697MB installed
● package 2697MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

save_keras_model
✓ from tensorflowjs.converters import save_keras_model
convert_tf_saved_model
✓ from tensorflowjs.converters import convert_tf_saved_model
Used for converting TensorFlow SavedModel directories.

This quickstart demonstrates how to create a simple Keras model, train it, and then use `tensorflowjs.converters.save_keras_model` to convert it into the TensorFlow.js Layers format, ready for web deployment. The output is a directory containing `model.json` and weight files.

import tensorflow as tf import tensorflowjs as tfjs import os import shutil # Create a simple Keras model model = tf.keras.Sequential([ tf.keras.layers.Dense(units=1, input_shape=[1]) ]) model.compile(optimizer='sgd', loss='mean_squared_error') # Train the model (dummy data) hs = model.fit([1, 2, 3, 4], [0, -1, -2, -3], epochs=1) # Define output directory output_dir = 'my_tfjs_model' if os.path.exists(output_dir): shutil.rmtree(output_dir) # Convert the Keras model to TensorFlow.js format tfjs.converters.save_keras_model(model, output_dir) print(f"Model converted and saved to: {output_dir}/") print("You can now serve this model with `tensorflowjs_converter --input_format=tfjs_layers_model <path-to-model-json-file>` or directly load it in JavaScript.") # Clean up (optional) # shutil.rmtree(output_dir)
tensorflowjs_wizard --version
Debug
Known issues
breakingCompatibility with TensorFlow versions is crucial. Using `tensorflowjs` with a significantly older or newer `tensorflow` package can lead to conversion errors or unexpected behavior. Always check the official documentation for supported TensorFlow versions for your `tensorflowjs` package.
fix
Ensure your `tensorflow` and `tensorflowjs` package versions are compatible. Typically, `tensorflowjs` versions are released to work with the latest stable `tensorflow` at that time. Update both packages or use versions known to be compatible.
affects: All versions
gotchaCustom TensorFlow Operations or Keras Layers are not automatically converted to their JavaScript equivalents. If your model uses custom ops/layers, you will likely need to implement their logic manually in JavaScript for them to work in TensorFlow.js.
fix
For models with custom components, consult the TensorFlow.js documentation on custom operations and layers. You may need to register custom ops in JavaScript or refactor your model to use standard layers.
affects: All versions
deprecatedWhile `.h5` (HDF5) Keras model files can often be converted, the recommended and most robust format for saving TensorFlow and Keras models is the TensorFlow SavedModel format (`model.save('path', save_format='tf')`). Conversion from SavedModel is generally more reliable.
fix
Always save your TensorFlow/Keras models in the SavedModel format (`model.save('path', save_format='tf')`) and use `tfjs.converters.convert_tf_saved_model` or the `tensorflowjs_converter` CLI tool with the `tf_saved_model` input format.
affects: <=3.x for some specific nuances, but generally a best practice.
gotchaConverting very large models can be resource-intensive (memory and CPU) and slow. Ensure your conversion environment has sufficient resources.
fix
For extremely large models, consider model pruning, quantization (which `tensorflowjs_converter` supports), or splitting the model into smaller, manageable parts. Run the conversion on a machine with ample RAM and CPU.
affects: All versions
Upgrade
Version history
4.22.0latest on PyPI · released Oct 21, 2024
Audit
Dependencies
tensorflowrequiredRequired for loading and processing TensorFlow/Keras models for conversion. The `tensorflowjs` package does not declare a hard dependency, but it is unusable without it.
Agent activity
38 hits · last 30 days
node
34
OpenAI (training)
1
Resources
tensorflowjs — pip install tensorflowjs · libregistry