TensorFlow
Google's end-to-end machine learning platform, strongest at production deployment.
What it is
TensorFlow is an end-to-end open-source platform for machine learning. It allows you to build and deploy machine learning models easily, from training to inference, across multiple platforms and devices.
TensorFlow provides APIs for building neural networks, performing automatic differentiation, training models, and serving models for production. It supports both eager execution (imperative programming) and graph execution (declarative programming).
- Best known for
- Deployment reach — server, mobile, browser and TPU
- Licence
- Apache 2.0
When to use it
The question documentation cannot answer for you — because it cannot recommend something else.
Reach for it when
- Deploying models to mobile, browsers or embedded devices via TFLite and TensorFlow.js
- You need TPU support or the surrounding production tooling — TFX, Serving, TensorBoard
- Maintaining an existing TensorFlow codebase
Look elsewhere when
- Starting fresh research — PyTorch has won most of that ground and the ecosystem follows it
Installation
pip install tensorflowGetting started
The smallest useful thing you can do with it, and what each part means.
import tensorflow as tf
x = tf.constant([[1., 2.],[3., 4.]])
y = tf.constant([[5., 6.],[7., 8.]])
print(tf.matmul(x, y))from tensorflow import keras
model = keras.Sequential([
keras.layers.Dense(10, activation='relu', input_shape=(5,)),
keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy')Advanced usage
Where the library earns its place over a simpler alternative.
import numpy as np
X_train = np.random.rand(100,5)
y_train = np.random.randint(0,2, size=(100,1))
model.fit(X_train, y_train, epochs=10, batch_size=8)model.save('my_model')
new_model = keras.models.load_model('my_model')import tensorflow_datasets as tfds
dataset = tfds.load('mnist', split='train')
for example in dataset.take(1):
image, label = example['image'], example['label']
print(image.shape, label)x = tf.random.normal((10,3))
y_true = tf.random.normal((10,1))
weights = tf.Variable(tf.random.normal((3,1)))
bias = tf.Variable(tf.zeros(1))
optimizer = tf.optimizers.SGD(0.01)
for i in range(100):
with tf.GradientTape() as tape:
y_pred = tf.matmul(x, weights) + bias
loss = tf.reduce_mean(tf.square(y_true - y_pred))
grads = tape.gradient(loss, [weights, bias])
optimizer.apply_gradients(zip(grads, [weights, bias]))Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- InvalidArgumentError
- Check input shapes and dtypes to ensure compatibility with model layers.
- ResourceExhaustedError
- Reduce batch size or use GPU/TPU memory more efficiently.
- ModuleNotFoundError: No module named 'tensorflow'
- Ensure TensorFlow is installed in the current Python environment using pip or conda.
Best practices
- Use tf.data API for efficient data loading and preprocessing.
- Use eager execution for debugging and graph execution for production performance.
- Leverage TensorBoard for visualizing training metrics.
- Use mixed precision and hardware acceleration (GPU/TPU) for faster training.
- Organize models and code using the Keras API for simplicity and modularity.
Alternatives
Comparable options, and the reason you would pick one over the other.
Background
Why it exists, and what it was reacting to.
TensorFlow was developed by the Google Brain team and released in 2015. It was designed to provide a comprehensive ecosystem for developing, training, and deploying machine learning models. TensorFlow supports deep learning, neural networks, and large-scale numerical computations and has become one of the most widely used ML frameworks in both industry and academia.
