Most TensorFlow models are defined and trained through Keras; you need it even when writing custom loops.
TensorFlow's high-level modelling API is Keras. Since TensorFlow 2.16, tf.keras refers to Keras 3, the multi-backend version (the separate Keras curriculum covers it in depth). Code written against Keras 2 can keep working by installing the tf_keras package and setting TF_USE_LEGACY_KERAS=1.
The basic workflow:
- Define a model:
tf.keras.Sequential([...])for a stack of layers, or the functional API for graphs of layers. - Compile it with an optimizer, a loss and metrics.
- Fit it on NumPy arrays or a
tf.data.Dataset, thenevaluateandpredict.
Under the hood, Keras layers hold tf.Variables, and fit() runs a compiled tf.function train step that uses a GradientTape, exactly the pieces you would assemble by hand.
Use from_logits=True in classification losses when the last layer has no softmax; it is more numerically stable. predict returns NumPy arrays; apply tf.nn.softmax to logits if you need probabilities.
Keras models also plug directly into TensorFlow tooling: tf.data input, distribution strategies, TensorBoard and export to SavedModel for serving.
Example
import numpy as np
import tensorflow as tf
x = np.random.rand(1000, 4).astype("float32")
y = (x.sum(axis=1) > 2).astype("int32")
model = tf.keras.Sequential([
tf.keras.Input(shape=(4,)),
tf.keras.layers.Dense(16, activation="relu"),
tf.keras.layers.Dense(2), # logits
])
model.compile(optimizer="adam",
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=["accuracy"])
model.fit(x, y, epochs=20, batch_size=32, validation_split=0.2, verbose=0)
loss, acc = model.evaluate(x, y, verbose=0)
print(f"accuracy {acc:.2f}") # typically above 0.9
probs = tf.nn.softmax(model.predict(x[:3], verbose=0))
print(probs.shape) # (3, 2)Remember
tf.kerasis Keras 3 since TF 2.16- Define, compile, fit, evaluate, predict
- Use
from_logits=Truewhen the model outputs raw scores
Watch out for
- Following Keras 2 tutorials that import
keras.srcinternals ortensorflow.keras.layers.experimental, which no longer exist.