ZeroToSky
Python Tutorial 73 of 96
Python Fundamentals
  • Introduction
  • Applications
  • Input and Output
  • Variables
  • Operators
  • Keywords
  • Data Types
  • Conditional Statements
  • Loops
  • Project: Rock Paper Scissor Game

All 10 lessons →

Functions
  • Functions
  • Pass in Functions
  • Global and Local Variables
  • Recursion
  • *args and **kwargs in Function
  • First Class Function
  • Lambda Function
  • Map
  • Reduce
  • Filter Function
  • Inner Function
  • Decorators
  • Project: Number Guessing Game

All 13 lessons →

Data Structures
  • Strings
  • List
  • Tuples
  • Dictionary
  • Sets
  • Arrays
  • List Comprehension
  • Project: Tic Tac Toe Game
  • Counters
  • Heapq
  • Deque
  • OrderedDict
  • Defaultdict
  • Complete Tutorial on DSA with Python

All 14 lessons →

OOP Concepts
  • Python OOP
  • Classes and Objects
  • Constructors
  • ‘Self’ as Default Argument
  • Polymorphism
  • Inheritance
  • Abstraction
  • Encapsulation
  • Iterators
  • Project: Banking System

All 10 lessons →

Exception Handling
  • Exception Handling
  • Built-in Exception
  • User defined Exception

All 3 lessons →

File Handling
  • File Handling
  • Read Files
  • Write/Create Files
  • OS Module
  • pathlib Module
  • Directory Management

All 6 lessons →

Database Handling
  • MongoDB Introduction
  • MySQL Introduction
  • Project: Management System

All 3 lessons →

Packages or Libraries
  • Packages
  • Built-in Modules
  • DSA Libraries
  • GUI Libraries
  • Project: Screen Recorder

All 5 lessons →

Data Science with Python

All 13 lessons →

Web Development with Python
  • Flask
  • Django
  • SQLite
  • SQLAlchemy in Python
  • MySQL
  • Jinja2 (Flask)
  • Django Templates
  • Flask-RESTful
  • Django REST Framework (DRF)
  • Complete Tutorial on Web Development
  • Project: ToDo webapp

All 11 lessons →

Chapter-wise Practice Quiz
  • Python Fundamentals Quiz
  • Control Flow & Functions Quiz
  • Data Structures Quiz
  • Collections Module Quiz
  • Object-Oriented Programming Quiz
  • Exception & File Handling Quiz

All 6 lessons →

Interview Questions and Practice Problems

All 2 lessons →

TensorFlow in Python

Learn TensorFlow in Python, including installation, tensors, operations, neural networks, model training, prediction, evaluation, and practical machine learning examples.

Gaurav 9 min read Sign in to save
On this page

TensorFlow in Python

TensorFlow is an open-source machine learning framework used to build, train, evaluate, and deploy machine learning and deep learning models.

It was originally developed by Google and provides tools for numerical computation, neural networks, model training, data processing, and production deployment.

TensorFlow is widely used with Python because Python provides a simple and expressive interface for building machine learning applications.

Common applications include:

  • Image classification
  • Object detection
  • Natural language processing
  • Time-series prediction
  • Recommendation systems
  • Speech processing
  • Computer vision
  • Deep learning
  • Generative AI and machine learning research

What Is TensorFlow?

TensorFlow is a software framework for numerical computation and machine learning.

At a basic level, TensorFlow works with tensors, which are multidimensional arrays used to represent data.

For example:

Scalar       → 10
Vector       → [10, 20, 30]
Matrix       → [[1, 2], [3, 4]]
3D Tensor    → Multiple matrices

A typical TensorFlow workflow is:

Data
  ↓
TensorFlow Dataset
  ↓
Neural Network
  ↓
Training
  ↓
Validation
  ↓
Prediction
  ↓
Evaluation

Why Use TensorFlow?

TensorFlow provides tools for building machine learning systems from experimentation through deployment.

Some important features include:

  • Tensor-based numerical computation
  • Neural network APIs
  • Automatic differentiation
  • GPU and accelerator support
  • Model training and evaluation
  • Data pipelines
  • Model serialization
  • Production deployment tools
  • Integration with Keras

TensorFlow can be used for both small experiments and large machine learning systems.


Installing TensorFlow

Install TensorFlow using pip:

pip install tensorflow

After installation, verify it:

import tensorflow as tf

print(tf.__version__)

If TensorFlow is installed correctly, the installed version will be displayed.


Importing TensorFlow

The conventional import is:

import tensorflow as tf

The alias tf is widely used in TensorFlow examples.

For example:

import tensorflow as tf

x = tf.constant(10)

print(x)

What Is a Tensor?

A tensor is a multidimensional data structure.

You can think of tensors as an extension of arrays to multiple dimensions.

Scalar

A scalar has zero dimensions.

import tensorflow as tf

x = tf.constant(10)

print(x)

Conceptually:

10

Vector

A vector is a one-dimensional tensor.

x = tf.constant([10, 20, 30])

print(x)

Conceptually:

[10, 20, 30]

Matrix

A matrix is a two-dimensional tensor.

x = tf.constant([
    [1, 2],
    [3, 4]
])

print(x)

Conceptually:

[[1, 2],
 [3, 4]]

Higher-Dimensional Tensor

TensorFlow can work with tensors having three or more dimensions.

For example:

x = tf.constant([
    [
        [1, 2],
        [3, 4]
    ],
    [
        [5, 6],
        [7, 8]
    ]
])

print(x)

This can be visualized as multiple matrices grouped into one tensor.


Tensor Shape

The shape of a tensor describes its dimensions.

x = tf.constant([
    [1, 2, 3],
    [4, 5, 6]
])

print(x.shape)

Output:

(2, 3)

This means the tensor contains:

  • 2 rows
  • 3 columns

Tensor Data Type

A tensor also has a data type.

x = tf.constant([1, 2, 3])

print(x.dtype)

The output identifies the tensor's data type.

You can specify a type explicitly:

x = tf.constant(
    [1, 2, 3],
    dtype=tf.float32
)

Tensor Operations

TensorFlow provides operations for mathematical calculations.

Example:

import tensorflow as tf

a = tf.constant([1, 2, 3])
b = tf.constant([4, 5, 6])

result = tf.add(a, b)

print(result)

Output:

[5 7 9]

You can also use operators directly:

result = a + b

Common Tensor Operations

Some commonly used operations include:

tf.add()
tf.subtract()
tf.multiply()
tf.divide()
tf.matmul()

Example:

a = tf.constant([10, 20, 30])
b = tf.constant([2, 4, 5])

print(tf.add(a, b))
print(tf.subtract(a, b))
print(tf.multiply(a, b))
print(tf.divide(a, b))

Matrix Multiplication

Matrix multiplication is important in neural networks.

TensorFlow provides:

tf.matmul()

Example:

a = tf.constant([
    [1, 2],
    [3, 4]
])

b = tf.constant([
    [5, 6],
    [7, 8]
])

result = tf.matmul(a, b)

print(result)

Variables in TensorFlow

TensorFlow provides tf.Variable for values that can be updated during computation.

Example:

import tensorflow as tf

x = tf.Variable(10)

print(x)

x.assign(20)

print(x)

Variables are commonly used for model parameters such as weights and biases.


Constants vs Variables

A constant:

x = tf.constant(10)

is not intended to be modified.

A variable:

x = tf.Variable(10)

can be updated.

Feature tf.constant tf.Variable
Intended to change No Yes
Used for Fixed data Model parameters
Can be updated with assign() No Yes

TensorFlow and Keras

For building neural networks, TensorFlow commonly works with Keras, a high-level deep learning API.

A simple model can be created using:

from tensorflow import keras

model = keras.Sequential()

You can then add layers:

model.add(
    keras.layers.Dense(
        10,
        activation="relu"
    )
)

Keras provides a convenient interface for defining, training, evaluating, and using neural network models.


Creating a Simple Neural Network

A basic neural network can be created with:

import tensorflow as tf
from tensorflow import keras

model = keras.Sequential([
    keras.layers.Dense(
        16,
        activation="relu",
        input_shape=(10,)
    ),
    keras.layers.Dense(
        8,
        activation="relu"
    ),
    keras.layers.Dense(
        1,
        activation="sigmoid"
    )
])

The model contains three layers:

Input
  ↓
Dense(16)
  ↓
Dense(8)
  ↓
Dense(1)
  ↓
Output

Understanding Dense Layers

A dense layer is a fully connected neural network layer.

Example:

keras.layers.Dense(
    16,
    activation="relu"
)

The number 16 represents the number of units in the layer.

The activation function determines how the layer transforms its input.


Activation Functions

Common activation functions include:

  • ReLU
  • Sigmoid
  • Softmax
  • Tanh

ReLU

ReLU is commonly used in hidden layers:

activation="relu"

Conceptually:

ReLU(x) = max(0, x)

Sigmoid

Sigmoid produces values between 0 and 1.

activation="sigmoid"

It is commonly used for binary classification output layers.


Softmax

Softmax is commonly used for multi-class classification.

activation="softmax"

For example:

keras.layers.Dense(
    3,
    activation="softmax"
)

can produce probabilities across three classes.


Compiling a Model

Before training, a Keras model is normally compiled.

model.compile(
    optimizer="adam",
    loss="binary_crossentropy",
    metrics=["accuracy"]
)

The three important components are:

  • Optimizer
  • Loss function
  • Metrics

Optimizers

An optimizer controls how model parameters are updated during training.

A commonly used optimizer is:

optimizer="adam"

Other optimizers include:

SGD
RMSprop
Adam
Adagrad

Example:

model.compile(
    optimizer="adam",
    loss="binary_crossentropy"
)

Loss Functions

The loss function measures how far the model's predictions are from the target values.

For binary classification:

loss="binary_crossentropy"

For multi-class classification:

loss="sparse_categorical_crossentropy"

For regression:

loss="mean_squared_error"

The appropriate loss depends on the model's task and target representation.


Training a Model

Use the fit() method to train a Keras model.

model.fit(
    X_train,
    y_train,
    epochs=10,
    batch_size=32
)

Important parameters include:

  • Training data
  • Target data
  • Number of epochs
  • Batch size

What Is an Epoch?

An epoch represents one complete pass through the training dataset.

For example:

epochs=10

means the model will train over the dataset for up to 10 passes.

Training for more epochs does not automatically produce a better model. Excessive training can cause overfitting.


What Is a Batch?

A batch is a subset of the training data processed during one training step.

For example:

batch_size=32

means the model processes 32 training examples per batch, subject to the dataset size and training implementation.


Validation Data

You can provide validation data while training.

history = model.fit(
    X_train,
    y_train,
    epochs=10,
    batch_size=32,
    validation_data=(X_test, y_test)
)

The validation metrics can help monitor how the model performs on data not used to update its parameters.

For more rigorous evaluation, it is common to maintain a separate final test set.


Evaluating a TensorFlow Model

After training, use:

loss, accuracy = model.evaluate(
    X_test,
    y_test
)

print("Loss:", loss)
print("Accuracy:", accuracy)

The exact metrics depend on the metrics configured during compilation.


Making Predictions

Use:

predictions = model.predict(X_test)

For example:

predictions = model.predict(X_test)

print(predictions[:5])

The output format depends on the model's output layer and task.


Binary Classification Example

Let's build a simple binary classification model using scikit-learn's breast cancer dataset.

import tensorflow as tf
from tensorflow import keras

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

data = load_breast_cancer()

X = data.data
y = data.target

X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.2,
    random_state=42,
    stratify=y
)

scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

model = keras.Sequential([
    keras.layers.Input(shape=(X_train.shape[1],)),
    keras.layers.Dense(32, activation="relu"),
    keras.layers.Dense(16, activation="relu"),
    keras.layers.Dense(1, activation="sigmoid")
])

model.compile(
    optimizer="adam",
    loss="binary_crossentropy",
    metrics=["accuracy"]
)

model.fit(
    X_train,
    y_train,
    epochs=20,
    batch_size=32,
    validation_split=0.2
)

loss, accuracy = model.evaluate(
    X_test,
    y_test
)

print("Test accuracy:", accuracy)

This example demonstrates the complete basic workflow:

Load Data
   ↓
Split Data
   ↓
Scale Features
   ↓
Build Neural Network
   ↓
Compile
   ↓
Train
   ↓
Evaluate

Multi-Class Classification

For multiple classes, the output layer can use softmax.

For example, if there are three classes:

model = keras.Sequential([
    keras.layers.Input(shape=(10,)),
    keras.layers.Dense(32, activation="relu"),
    keras.layers.Dense(3, activation="softmax")
])

If the target labels are integer class IDs such as:

0
1
2

you can use:

model.compile(
    optimizer="adam",
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy"]
)

Regression with TensorFlow

TensorFlow can also be used for regression.

For example:

model = keras.Sequential([
    keras.layers.Input(shape=(10,)),
    keras.layers.Dense(32, activation="relu"),
    keras.layers.Dense(16, activation="relu"),
    keras.layers.Dense(1)
])

model.compile(
    optimizer="adam",
    loss="mean_squared_error",
    metrics=["mae"]
)

The final layer has one output because the model predicts a single continuous value.


TensorFlow Dataset API

TensorFlow provides tf.data for building efficient input pipelines.

Example:

dataset = tf.data.Dataset.from_tensor_slices(
    (X_train, y_train)
)

dataset = dataset.shuffle(1000)
dataset = dataset.batch(32)

The resulting dataset can be passed to model training:

model.fit(
    dataset,
    epochs=10
)

The tf.data API is useful when working with larger datasets and more complex data-loading pipelines.


Shuffling and Batching

A common input pipeline is:

dataset = dataset.shuffle(1000)
dataset = dataset.batch(32)

shuffle() randomizes the order of examples.

batch() groups examples into batches.

You can also use:

dataset = dataset.prefetch(
    tf.data.AUTOTUNE
)

to help overlap data preparation and model execution in supported workflows.


Saving a TensorFlow Model

A trained Keras model can be saved for later use.

For example:

model.save("my_model.keras")

The model can later be loaded:

loaded_model = keras.models.load_model(
    "my_model.keras"
)

This allows a trained model to be reused without retraining it from scratch.


TensorFlow Callbacks

Callbacks allow you to execute actions during training.

Common callbacks include:

  • EarlyStopping
  • ModelCheckpoint
  • TensorBoard
  • Learning-rate scheduling callbacks

For example:

from tensorflow.keras.callbacks import EarlyStopping

early_stopping = EarlyStopping(
    monitor="val_loss",
    patience=3,
    restore_best_weights=True
)

model.fit(
    X_train,
    y_train,
    epochs=50,
    validation_split=0.2,
    callbacks=[early_stopping]
)

Early Stopping

Early stopping can stop training when a monitored validation metric stops improving.

Example:

early_stopping = keras.callbacks.EarlyStopping(
    monitor="val_loss",
    patience=3,
    restore_best_weights=True
)

The patience parameter specifies how many epochs without improvement are tolerated before training stops.


Model Checkpointing

You can save model checkpoints during training.

checkpoint = keras.callbacks.ModelCheckpoint(
    "best_model.keras",
    monitor="val_loss",
    save_best_only=True
)

Use it during training:

model.fit(
    X_train,
    y_train,
    validation_split=0.2,
    epochs=20,
    callbacks=[checkpoint]
)

TensorBoard

TensorFlow includes TensorBoard for visualizing training and model information.

A basic callback can be created with:

tensorboard = keras.callbacks.TensorBoard(
    log_dir="./logs"
)

Use it during training:

model.fit(
    X_train,
    y_train,
    epochs=10,
    callbacks=[tensorboard]
)

TensorBoard can be used to inspect metrics and training behavior.


GPU Support

TensorFlow can use supported hardware accelerators to speed up certain computations.

You can inspect available physical devices:

import tensorflow as tf

print(
    tf.config.list_physical_devices()
)

To check GPUs:

print(
    tf.config.list_physical_devices("GPU")
)

If a supported GPU is available and TensorFlow is configured correctly, suitable operations can be executed on it.


Checking Available Devices

Example:

devices = tf.config.list_physical_devices()

for device in devices:
    print(device)

This can help diagnose the runtime environment.


TensorFlow for Computer Vision

TensorFlow can be used to build image classification models.

A typical workflow is:

Images
  ↓
Resize / Normalize
  ↓
Training Dataset
  ↓
CNN
  ↓
Training
  ↓
Evaluation
  ↓
Prediction

For example, Keras provides convolutional layers:

model = keras.Sequential([
    keras.layers.Input(
        shape=(128, 128, 3)
    ),
    keras.layers.Conv2D(
        32,
        (3, 3),
        activation="relu"
    ),
    keras.layers.MaxPooling2D(),
    keras.layers.Flatten(),
    keras.layers.Dense(
        1,
        activation="sigmoid"
    )
])

TensorFlow for Natural Language Processing

TensorFlow can also be used for NLP tasks.

A simple text-processing pipeline might contain:

Text
 ↓
Tokenization
 ↓
Integer Sequences
 ↓
Embedding
 ↓
Neural Network
 ↓
Prediction

Keras provides layers such as:

keras.layers.Embedding()

For example:

model = keras.Sequential([
    keras.layers.Embedding(
        input_dim=10000,
        output_dim=64
    ),
    keras.layers.GlobalAveragePooling1D(),
    keras.layers.Dense(
        1,
        activation="sigmoid"
    )
])

TensorFlow for Time-Series Data

TensorFlow can also be used for time-series problems such as:

  • Demand forecasting
  • Sensor prediction
  • Temperature forecasting
  • Financial time-series modeling

A time-series workflow may look like:

Historical Data
      ↓
Create Sequences
      ↓
Training Dataset
      ↓
Neural Network
      ↓
Training
      ↓
Future Prediction

Depending on the problem, models can use dense networks, convolutional layers, recurrent architectures, or transformer-based approaches.


TensorFlow and Deep Learning

TensorFlow is particularly well known for deep learning.

A deep neural network may contain many layers:

Input
  ↓
Dense Layer
  ↓
ReLU
  ↓
Dense Layer
  ↓
ReLU
  ↓
Dense Layer
  ↓
Output

More specialized architectures can be used for different data types.

Examples include:

  • CNNs for image-related tasks
  • RNNs for sequence-related tasks
  • LSTM/GRU networks for certain sequential problems
  • Transformer architectures for many modern NLP and multimodal tasks

TensorFlow vs NumPy

NumPy is primarily a numerical computing library, while TensorFlow is designed for machine learning and accelerated tensor computation.

Feature NumPy TensorFlow
Numerical arrays Yes Yes
Machine learning framework No Yes
Neural networks No Yes
Automatic differentiation No Yes
GPU/accelerator support Limited/different tooling Yes
Model training No built-in ML training framework Yes
Keras integration No Yes

NumPy is often used alongside TensorFlow for data preparation and general numerical computation.


TensorFlow vs PyTorch

TensorFlow and PyTorch are both widely used machine learning frameworks.

Feature TensorFlow PyTorch
Tensor operations Yes Yes
Neural networks Yes Yes
Automatic differentiation Yes Yes
GPU support Yes Yes
High-level API Keras torch.nn and ecosystem
Data pipelines tf.data Dataset / DataLoader
Model deployment TensorFlow ecosystem PyTorch ecosystem

The choice depends on the project's requirements, team familiarity, ecosystem, deployment environment, and existing codebase.


Common TensorFlow Mistakes

Mistake 1: Using the Wrong Output Layer

The output layer should match the problem.

For binary classification:

Dense(1, activation="sigmoid")

For a three-class classification problem:

Dense(3, activation="softmax")

For regression:

Dense(1)

Mistake 2: Choosing an Incompatible Loss Function

The loss function should match the task and target representation.

For binary classification:

binary_crossentropy

For integer-label multi-class classification:

sparse_categorical_crossentropy

For regression:

mean_squared_error

Mistake 3: Training Without Validation

Training accuracy alone does not tell you how well the model generalizes.

Use validation data when appropriate:

model.fit(
    X_train,
    y_train,
    validation_split=0.2,
    epochs=20
)

Keep a separate test set when you need an unbiased final evaluation.


Mistake 4: Ignoring Feature Scaling When It Matters

Some neural network problems benefit from appropriately scaled numeric inputs.

For example:

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

The appropriate preprocessing depends on the model and dataset.


TensorFlow Best Practices

When building TensorFlow models:

  1. Start with a simple baseline.
  2. Understand the shape of your input tensors.
  3. Choose the output layer according to the prediction task.
  4. Select a loss function compatible with the target representation.
  5. Keep training, validation, and test data appropriately separated.
  6. Monitor validation metrics.
  7. Use callbacks such as early stopping when appropriate.
  8. Save trained models for reproducibility and deployment.
  9. Use tf.data for scalable input pipelines.
  10. Monitor overfitting.
  11. Scale or normalize features when appropriate.
  12. Set random seeds when reproducibility is important, while recognizing that exact reproducibility can depend on the environment and hardware.

Complete TensorFlow Example

The following example demonstrates a complete binary classification workflow:

import tensorflow as tf
from tensorflow import keras

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# Load dataset
data = load_breast_cancer()

X = data.data
y = data.target

# Split data
X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.2,
    random_state=42,
    stratify=y
)

# Scale features
scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Build model
model = keras.Sequential([
    keras.layers.Input(
        shape=(X_train.shape[1],)
    ),
    keras.layers.Dense(
        32,
        activation="relu"
    ),
    keras.layers.Dense(
        16,
        activation="relu"
    ),
    keras.layers.Dense(
        1,
        activation="sigmoid"
    )
])

# Compile model
model.compile(
    optimizer="adam",
    loss="binary_crossentropy",
    metrics=["accuracy"]
)

# Train model
history = model.fit(
    X_train,
    y_train,
    validation_split=0.2,
    epochs=20,
    batch_size=32
)

# Evaluate model
loss, accuracy = model.evaluate(
    X_test,
    y_test
)

print("Test Loss:", loss)
print("Test Accuracy:", accuracy)

# Make predictions
predictions = model.predict(X_test)

print(predictions[:5])

This example demonstrates the core TensorFlow workflow:

Dataset
   ↓
Train/Test Split
   ↓
Feature Scaling
   ↓
Model Creation
   ↓
Compilation
   ↓
Training
   ↓
Evaluation
   ↓
Prediction

Key Points to Remember

  • TensorFlow is an open-source machine learning and deep learning framework.
  • TensorFlow works with multidimensional data structures called tensors.
  • tf.constant() creates constant tensors.
  • tf.Variable() creates values that can be updated.
  • Keras provides a high-level API for building neural networks with TensorFlow.
  • Sequential is useful for straightforward layer-by-layer models.
  • Dense creates fully connected layers.
  • fit() trains a model.
  • evaluate() evaluates a model.
  • predict() generates predictions.
  • compile() configures the optimizer, loss, and metrics.
  • tf.data provides tools for building data pipelines.
  • Callbacks such as EarlyStopping and ModelCheckpoint can improve training workflows.
  • TensorFlow can use supported GPUs and other accelerators.
  • TensorFlow is widely used for computer vision, NLP, time series, and other deep learning tasks.

Conclusion

TensorFlow provides a comprehensive ecosystem for building and deploying machine learning and deep learning applications in Python.

A basic neural network can be created using Keras:

from tensorflow import keras

model = keras.Sequential([
    keras.layers.Dense(
        32,
        activation="relu"
    ),
    keras.layers.Dense(
        1,
        activation="sigmoid"
    )
])

It can then be compiled:

model.compile(
    optimizer="adam",
    loss="binary_crossentropy",
    metrics=["accuracy"]
)

and trained:

model.fit(
    X_train,
    y_train,
    epochs=10
)

Once these fundamentals are understood, you can move on to convolutional neural networks, NLP models, transfer learning, custom training loops, TensorFlow data pipelines, model optimization, and production deployment.

Related

Found a problem on this page, or something that could be clearer?

Goes straight to the author. Not published anywhere.