
EdTech Platform Types ExplainedRead More

Deep learning is a subset of machine learning that uses neural networks to solve complex problems such as image recognition, natural language processing, and more. TensorFlow, Keras, and PyTorch are the most popular frameworks for building deep learning models. This guide will help you get started with each, compare their approaches, and provide practical code examples.
Before diving in, you should have:
1. Choose a Python Version
Make sure you have the desired Python version installed. You can list installed versions with:
ls /Library/Frameworks/Python.framework/Versions/
2. Create a Virtual Environment
Run the following command to create a virtual environment:
python -m venv myenv
3. This creates a new folder named myenv containing the virtual environment.
4. Activate the Virtual Environment
On macOS, run:
source myenv/bin/activate
5. On Windows, run:
myenv\Scripts\activate
6. Your terminal prompt should now show (myenv).
7. Install Required Libraries
Use pip to install libraries inside the virtual environment. For example, to install Matplotlib:
pip install matplotlib
8. Deactivate the Environment
When finished, deactivate with:
deactivate
For most recent versions of TensorFlow (2.x), Python 3.8, 3.9, 3.10, or 3.11 are recommended and officially supported.
Check the TensorFlow installation guide for the latest compatibility details, as support for newer Python versions may be added over time.
1. Importing TensorFlow
import tensorflow as tf
print(tf.__version__)
Why?
You need to import TensorFlow to access its deep learning tools and check your installed version.
Alternatives:
2. Creating Tensors
import tensorflow as tf
# Create a constant tensor
a = tf.constant([[1, 2], [3, 4]])
print(a)
Why?
Tensors are the basic data structure in TensorFlow, representing multi-dimensional arrays for computation.
Alternatives:
3. Building a Simple Neural Network (Sequential Model)
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(16, activation='relu', input_shape=(4,)),
tf.keras.layers.Dense(3, activation='softmax')
])
Why?
The Sequential model is the simplest way to stack layers for most problems.
How does this help?
It allows you to quickly prototype and build feedforward neural networks.
Alternatives:
4. Compiling the Model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
Why?
Compiling configures the model for training by specifying the optimizer, loss function, and metrics.
Alternatives:
import torch.optim as optim
optimizer = optim.Adam(model.parameters())
loss_fn = torch.nn.CrossEntropyLoss()
5. Training the Model
import numpy as np
# Dummy data
X_train = np.random.rand(100, 4)
y_train = np.random.randint(0, 3, 100)
model.fit(X_train, y_train, epochs=5, batch_size=8)
Why?
fit trains the model on your data.
Alternatives:
6. Evaluating the Model
loss, accuracy = model.evaluate(X_train, y_train)
print(f"Loss: {loss}, Accuracy: {accuracy}")
Why?
To measure how well your model performs on data.
Alternatives:
7. Making Predictions
predictions = model.predict(X_train[:5])
print(predictions)
Why?
To use your trained model to make predictions on new data.
Alternatives:
Keras: Same as above.
PyTorch: Use model(input_tensor).
Task | TensorFlow / Keras | PyTorch Equivalent |
| Import | import tensorflow as tf | import torch |
| Create Tensor | tf.constant() | torch.tensor() |
| Build Model | tf.keras.Sequential([...]) | torch.nn.Sequential([...]) |
| Compile Model | model.compile(...) | Define optimizer/loss manually |
| Train Model | model.fit(...) | Custom training loop |
| Evaluate Model | model.evaluate(...) | Custom evaluation loop |
| Predict | model.predict(...) | model(input) |
Keras is included as part of TensorFlow (tf.keras) and follows TensorFlow’s Python version requirements.
For standalone Keras, Python 3.8, 3.9, 3.10, or 3.11 are generally supported.
Always check the Keras release notes or TensorFlow installation guide for the latest compatibility details.
1. Importing Keras
import keras
print(keras.__version__)
Why?
You need to import Keras to access its deep learning tools and check your installed version.
Alternatives:
TensorFlow: import tensorflow as tf (and use tf.keras)
PyTorch: import torch
2. Creating Tensors (Standalone Keras uses NumPy arrays)
import numpy as np
# Create a NumPy array (Keras models accept NumPy arrays as input)
a = np.array([[1, 2], [3, 4]])
print(a)
Why?
Keras models use NumPy arrays for input data, which are easy to create and manipulate.
Alternatives:
3. Building a Simple Neural Network (Sequential Model)
from keras.models import Sequential
from keras.layers import Dense
model = Sequential([
Dense(16, activation='relu', input_shape=(4,)),
Dense(3, activation='softmax')
])
Why?
The Sequential model is the simplest way to stack layers for most problems.
How does this help?
It allows you to quickly prototype and build feedforward neural networks.
Alternatives:
4. Compiling the Model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
Why?
Compiling configures the model for training by specifying the optimizer, loss function, and metrics.
Alternatives:
import torch.optim as optim
optimizer = optim.Adam(model.parameters())
loss_fn = torch.nn.CrossEntropyLoss()
5. Training the Model
import numpy as np
# Dummy data
X_train = np.random.rand(100, 4)
y_train = np.random.randint(0, 3, 100)
model.fit(X_train, y_train, epochs=5, batch_size=8)
Why?
fit trains the model on your data.
Alternatives:
6. Evaluating the Model
loss, accuracy = model.evaluate(X_train, y_train)
print(f"Loss: {loss}, Accuracy: {accuracy}")
Why?
To measure how well your model performs on data.
Alternatives:
7. Making Predictions
predictions = model.predict(X_train[:5])
print(predictions)
Why?
To use your trained model to make predictions on new data.
Alternatives:
PyTorch: Use model(input_tensor)
Task | Keras (Standalone) | TensorFlow / tf.keras | PyTorch Equivalent |
| Import | import keras | import tensorflow as tf | import torch |
| Create Tensor/Input | np.array() | tf.constant() | torch.tensor() |
| Build Model | Sequential([...]) | tf.keras.Sequential([...]) | torch.nn.Sequential([...]) |
| Compile Model | model.compile(...) | model.compile(...) | Define optimizer/loss manually |
| Train Model | model.fit(...) | model.fit(...) | Custom training loop |
| Evaluate Model | model.evaluate(...) | model.evaluate(...) | Custom evaluation loop |
| Predict | model.predict(...) | model.predict(...) | model(input) |
For recent versions of PyTorch, Python 3.8, 3.9, 3.10, or 3.11 are officially supported.
Always check the PyTorch installation guide for the latest compatibility details, as support for newer Python versions may be added over time.
1. Importing PyTorch
import torch
print(torch.__version__)
Why?
You need to import PyTorch to access its deep learning tools and check your installed version.
Alternatives:
TensorFlow: import tensorflow as tf
Keras: import keras
2. Creating Tensors
import torch
# Create a tensor
a = torch.tensor([[1, 2], [3, 4]])
print(a)
Why?
Tensors are the basic data structure in PyTorch, representing multi-dimensional arrays for computation.
Alternatives:
3. Building a Simple Neural Network (Sequential Model)
import torch.nn as nn
model = nn.Sequential(
nn.Linear(4, 16),
nn.ReLU(),
nn.Linear(16, 3),
nn.Softmax(dim=1)
)
Why?
nn.Sequential is the simplest way to stack layers for most problems.
How does this help?
It allows you to quickly prototype and build feedforward neural networks.
Alternatives:
4. Defining Loss and Optimizer
import torch.optim as optim
loss_fn = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters())
Why?
You need to specify how the model learns (optimizer) and how to measure error (loss function).
Alternatives:
5. Training the Model (Manual Training Loop)
import numpy as np
# Dummy data
X_train = np.random.rand(100, 4).astype(np.float32)
y_train = np.random.randint(0, 3, 100)
X_train_tensor = torch.tensor(X_train)
y_train_tensor = torch.tensor(y_train)
for epoch in range(5):
optimizer.zero_grad()
outputs = model(X_train_tensor)
loss = loss_fn(outputs, y_train_tensor)
loss.backward()
optimizer.step()
print(f"Epoch {epoch+1}, Loss: {loss.item()}")
Why?
PyTorch gives you full control over the training process with manual loops.
How does this help?
You can customize every aspect of training, which is useful for research and advanced use cases.
Alternatives:
6. Evaluating the Model
with torch.no_grad():
outputs = model(X_train_tensor)
predicted = torch.argmax(outputs, dim=1)
accuracy = (predicted == y_train_tensor).float().mean().item()
print(f"Accuracy: {accuracy}")
Why?
To measure how well your model performs on data.
Alternatives:
7. Making Predictions
with torch.no_grad():
predictions = model(X_train_tensor[:5])
print(predictions)
Why?
To use your trained model to make predictions on new data.
Alternatives:
Task | PyTorch | TensorFlow / tf.keras | Keras (Standalone) |
| Import | import torch | import tensorflow as tf | import keras |
| Create Tensor | torch.tensor() | tf.constant() | np.array() |
| Build Model | nn.Sequential([...]) | tf.keras.Sequential([...]) | Sequential([...]) |
| Compile Model | Define optimizer/loss manually | model.compile(...) | model.compile(...) |
| Train Model | Manual training loop | model.fit(...) | model.fit(...) |
| Evaluate Model | Manual evaluation | model.evaluate(...) | model.evaluate(...) |
| Predict | model(input) | model.predict(...) | model.predict(...) |
If you are beginning your deep learning journey, you do not need to master all three libraries at once. Here’s a recommended approach:
Focus on understanding the core concepts (tensors, models, training loops, evaluation, prediction) in one library first—these skills will transfer easily to the others.
Trusted by top platforms for our transformative solutions and exceptional results:






