Introduction to TensorFlow / PyTorch
The battle of the giants. Learn how to build neural networks using the industry's top frameworks.
The battle of the giants. Learn how to build neural networks using the industry's top frameworks. This hands-on tutorial focuses on practical implementation of introduction to tensorflow / pytorch concepts.
Introduction to TensorFlow / PyTorch
You don't write Neural Networks from scratch in production. You use a framework. The two leaders are TensorFlow (by Google) and PyTorch (by Meta).
1. Tensors π¦
A Tensor is just a fancy name for a multi-dimensional array (like a NumPy array), but it can run on a GPU.
- Scalar: 0-D Tensor (a single number)
- Vector: 1-D Tensor
- Matrix: 2-D Tensor
- 3D Tensor: e.g., an RGB image (Height x Width x 3)
2. PyTorch: The Researcher's Choice π¬
PyTorch is loved for its "pythonic" style and dynamic computation graph. It feels like writing standard Python.
import torch
import torch.nn as nn
# Define a simple network
model = nn.Sequential(
nn.Linear(10, 50), # Input layer (10 features) -> Hidden (50 neurons)
nn.ReLU(), # Activation
nn.Linear(50, 1) # Hidden -> Output (1 prediction)
)
# Forward pass
input_data = torch.randn(1, 10)
output = model(input_data)
print(output)
3. TensorFlow / Keras: The Production Choice π
TensorFlow (via its high-level API Keras) is great for deployment and production pipelines.
import tensorflow as tf
from tensorflow.keras import layers
# Define the same network
model = tf.keras.Sequential([
layers.Dense(50, activation='relu', input_shape=(10,)),
layers.Dense(1)
])
# Compile
model.compile(optimizer='adam', loss='mse')
4. The Training Loop π
In PyTorch, you write the training loop manually (great for control). In Keras, you just call .fit().
PyTorch Loop:
optimizer.zero_grad()(Clear gradients)output = model(x)(Forward)loss = loss_fn(output, y)(Calculate error)loss.backward()(Backprop)optimizer.step()(Update weights)
Interactive Challenge: PyTorch Basics
PyTorch is installed in this environment! Let's do some tensor math.
Quiz
Quiz
Question 1 of 3What is the main difference between a Tensor and a NumPy array?
Key Takeaways
β
Tensors are GPU-ready arrays.
β
PyTorch is flexible and great for research.
β
Keras is high-level and easy to use.
What's Next?
Now that we can build a basic network, let's build one that can "see".
Next Chapter: Convolutional Neural Networks (CNNs).