# Python: import keras import numpy as np # Build and compile model. model = keras.Sequential() model.add(keras.layers.Dense(units=1, input_shape=)) model.compile(optimizer='sgd', loss='mean_squared_error') # Generate some synthetic data for training. xs = np.array([, , , ]) ys = np.array([, , , ]) # Train model with fit(). model.fit(xs, ys, epochs=1000) # Run inference with predict(). print(model.predict(np.array([])))
# Python: keras.layers.Dense(units=1, inputShape=)
Some methods of the Model class, e.g.,
Model.compile(), also take a
Model.predict() are slightly different.
Since these method take obligatory
x (features) and
y (labels or targets)
data as inputs;
y are positional arguments separate from the ensuing
configuration object that plays the role of the keyword arguments. For example:
Model.fit() is async
Model.fit() is the primary method with which users perform model training in
TensorFlow.js. This method can often be long-running, lasting for seconds or
minutes. Therefore, we utilize the
so that this function can be used in a way that does not block the main UI
thread when running in the browser.
async is a construct that does not exist in Python. While the
method in Keras returns a History object, the counterpart of the
of History, which can be
(as in the example above) or used with the then() method.
No NumPy for TensorFlow.js
Python Keras users often use NumPy to perform basic numeric and array operations, such as generating 2D tensors in the example above.
# Python: xs = np.array([, , , ])
In TensorFlow.js, this kind of basic numeric operations are done with the package itself. For example:
tf.* namespace also provides a number of other functions for array and
linear algebra operations such as matrix multiplication. See the
TensorFlow.js Core documentation for more
Use factory methods, not constructors
This line in Python (from the example above) is a constructor call:
# Python: model = keras.Sequential()
Option string values are lowerCamelCase, not snake_case
- DataFormat, e.g.,
- Initializer, e.g.,
- Loss and metrics, e.g.,
For example, as in the example above:
With regard to model serialization and deserialization, rest assured. TensorFlow.js’s internal mechanism ensure that snake cases in JSON objects are handled correctly, e.g., when loading pretrained models from Python Keras.
Run Layer objects with apply(), not by calling them as functions
In Keras, a Layer object has the
__call__ method defined. Therefore the user can
invoke the layer’s logic by calling the object as a function, e.g.,
# Python: my_input = keras.Input(shape=[2, 4]) flatten = keras.layers.Flatten() print(flatten(my_input).shape)
This Python syntax sugar is implemented as the apply() method in TensorFlow.js:
Layer.apply() supports imperative (eager) evaluation on concrete tensors
Currently, in Keras, the call method can only operate on (Python)
objects (assuming TensorFlow backend), which are symbolic and do not hold actual
numeric values. This is what’s shown in the example in the previous section.
However, in TensorFlow.js, the apply() method of layers can operate in both
symbolic and imperative modes. If
apply() is invoked with a SymbolicTensor (a
close analogy of tf.Tensor), the return value will be a SymbolicTensor. This
happens typically during model building. But if
apply() is invoked with an
actual concrete Tensor value, it will return a concrete Tensor. For example:
This feature is reminiscent of (Python) TensorFlow’s Eager Execution. It affords greater interactivity and debuggability during model development, in addition to opening doors to composing dynamic neural networks.
Optimizers are under train., not optimizers.
In Keras, the constructors for Optimizer objects are under the
keras.optimizers.* namespace. In TensorFlow.js Layers, the factory methods for
Optimizers are under the
tf.train.* namespace. For example:
# Python: my_sgd = keras.optimizers.sgd(lr=0.2)
loadLayersModel() loads from a URL, not an HDF5 file
In Keras, models are usually saved
as a HDF5 (.h5) file, which can be later loaded using the
keras.models.load_model() method. The method takes a path to the .h5 file. The
load_model() in TensorFlow.js is
tf.loadLayersModel(). Since HDF5 is not a
browser-friendly file format,
tf.loadLayersModel() takes a TensorFlow.js-specific
tf.loadLayersModel() takes a model.json file as its input argument. The
model.json can be converted from a Keras HDF5 file using the tensorflowjs pip
Also note that
tf.loadLayersModel() returns a
In general, saving and loading
tf.Models in TensorFlow.js is done using the
tf.loadLayersModel methods, respectively. We designed these APIs to be similar to
the save and load_model API
of Keras. But the browser environment is quite different from the backend environment
on which staple deep learning frameworks like Keras run, particularly in the
array of routes for persisting and transmitting data. Hence there are
some interesting differences between the save/load APIs in TensorFlow.js and in Keras.
See our tutorial on Saving and Loading tf.Model for more
fitDataset() to train models using
In Python TensorFlow's tf.keras, a model can be trained using a
Dataset object. The model's
method accepts such an object directly. A TensorFlow.js model can be trained
the documentation of the tf.data API in TensorFlow.js).
However, unlike in Python, Dataset-based training is done through a dedicated
method, namely fitDataset.
The fit() method is
only for tensor-based model training.
Memory management of Layer and Model objects
TensorFlow.js runs on WebGL in the browser, where the weights of Layer and Model
objects are backed by WebGL textures. However, WebGL has no built-in garbage-collection
support. Layer and Model objects internally manage tensor memory for the user
during their inference and training calls. But they also allows the user to
dispose them in order to free the WebGL memory that they occupy. This is useful
in cases where many model instances are created and released within a single
page load. To dispose a Layer or Model object, use the