{"id":2307,"library":"tensorflowjs","title":"TensorFlow.js Python Library","description":"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.","status":"active","version":"4.22.0","language":"en","source_language":"en","source_url":"https://github.com/tensorflow/tfjs","tags":["tensorflow","javascript","machine learning","model conversion","web development","keras"],"install":[{"cmd":"pip install tensorflowjs","lang":"bash","label":"Install `tensorflowjs`"}],"dependencies":[{"reason":"Required for loading and processing TensorFlow/Keras models for conversion. The `tensorflowjs` package does not declare a hard dependency, but it is unusable without it.","package":"tensorflow","optional":false}],"imports":[{"symbol":"save_keras_model","correct":"from tensorflowjs.converters import save_keras_model"},{"note":"Used for converting TensorFlow SavedModel directories.","symbol":"convert_tf_saved_model","correct":"from tensorflowjs.converters import convert_tf_saved_model"}],"quickstart":{"code":"import tensorflow as tf\nimport tensorflowjs as tfjs\nimport os\nimport shutil\n\n# Create a simple Keras model\nmodel = tf.keras.Sequential([\n    tf.keras.layers.Dense(units=1, input_shape=[1])\n])\nmodel.compile(optimizer='sgd', loss='mean_squared_error')\n\n# Train the model (dummy data)\nhs = model.fit([1, 2, 3, 4], [0, -1, -2, -3], epochs=1)\n\n# Define output directory\noutput_dir = 'my_tfjs_model'\nif os.path.exists(output_dir):\n    shutil.rmtree(output_dir)\n\n# Convert the Keras model to TensorFlow.js format\ntfjs.converters.save_keras_model(model, output_dir)\n\nprint(f\"Model converted and saved to: {output_dir}/\")\nprint(\"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.\")\n\n# Clean up (optional)\n# shutil.rmtree(output_dir)","lang":"python","description":"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."},"warnings":[{"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.","message":"Compatibility 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.","severity":"breaking","affected_versions":"All versions"},{"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.","message":"Custom 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"While `.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.","severity":"deprecated","affected_versions":"<=3.x for some specific nuances, but generally a best practice."},{"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.","message":"Converting very large models can be resource-intensive (memory and CPU) and slow. Ensure your conversion environment has sufficient resources.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}