iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Intro

TensorFlow is Googles deep learning framework. Define computational graphs of tensors; train on CPU, GPU, or TPU; deploy via SavedModel / TFLite.

TensorFlow — what it is

EXAMPLE
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# ===== The values =====
# - Eager execution by default; tf.function compiles to graphs for speed
# - Keras: high-level API for layers, models, training loops
# - SavedModel format: deploy to server, mobile (TFLite), JS (TFJS)
# - TPU + GPU + CPU all from the same code

# ===== Hello, tensor =====
a = tf.constant([1.0, 2.0, 3.0])
b = tf.constant([10.0, 20.0, 30.0])
print(a + b)

# ===== A tiny model =====
model = keras.Sequential([
    layers.Dense(64, activation='relu', input_shape=(4,)),
    layers.Dense(3, activation='softmax'),
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Fake data for the demo:
import numpy as np
X = np.random.rand(100, 4); y = np.random.randint(0, 3, 100)
model.fit(X, y, epochs=5, verbose=0)
print(model.evaluate(X, y, verbose=0))

# ===== Save + load =====
model.save('demo_model')
loaded = keras.models.load_model('demo_model')

# ===== When TensorFlow wins =====
# - Production deploys to mobile / edge (TFLite)
# - TPU acceleration on Google Cloud
# - Keras feels great for high-level prototyping
# - TensorFlow Serving for big production model fleets

# ===== When TensorFlow hurts =====
# - Cutting-edge research (PyTorch dominates papers)
# - Highly dynamic graphs / debugging (PyTorchs eager loop is friendlier)
# - Steeper learning curve when you go beyond Keras

# ===== Patterns to internalise =====
# - Use Keras for new code; reach for low-level tf.function only when you must
# - tf.data for input pipelines (.cache().prefetch())
# - Mixed precision training on modern GPUs
# - EarlyStopping + ModelCheckpoint callbacks always

# ===== Pitfalls =====
# - Forgetting to set seeds -> non-reproducible runs
# - Eager + tf.function interaction (Python side effects do not retrigger)
# - Mixing tf.Variable and tf.constant in custom training loops
# - SavedModel vs Keras .h5 vs SavedModel directory: pick one and stick

Why it matters

TensorFlow + Keras is the production-friendly deep learning stack. Build a model with layers, train with fit, save with SavedModel, deploy to server / mobile / web. The mental model is small; the ecosystem (TFLite, TFJS, TFServing) is what makes it ship-friendly.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# TensorFlow: end-to-end ML platform — research → production.
# Keras is the high-level API; tf.* is the lower-level.
Try it Yourself »

Test yourself

Q1. TensorFlow is developed by…
Q2. The high-level API is called…
Q3. Tensors are…

Discussion

Loading…