codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

58 / 68 topics
51NumPy Tutorial52Pandas Tutorial53SciPy Tutorial54Matplotlib & Seaborn Basics55Machine Learning Basics (Stats & Data Distribution)56Linear & Polynomial Regression57Classification & Clustering (Decision Trees, K-Means)58TensorFlow & PyTorch Basics
Tutorials/Python Programming/TensorFlow & PyTorch Basics
🐍Python Programming

TensorFlow & PyTorch Basics

Updated 2026-05-15
30 min read

TensorFlow & PyTorch Basics

In the realm of data science and machine learning, TensorFlow and PyTorch are two of the most popular frameworks. Both are open-source libraries that provide comprehensive tools for building and training machine learning models. While they share many similarities, each has its own strengths and is suited to different types of projects.

In this tutorial, we'll dive into the basics of both TensorFlow and PyTorch. We'll cover tensors (the fundamental data structure), how to build a simple neural network, train the model, and make predictions. By the end of this guide, you'll have a solid understanding of these powerful frameworks and be ready to tackle more complex machine learning tasks.

Introduction

TensorFlow is developed by Google and is known for its scalability and flexibility. It supports multiple programming languages, including Python, C++, Java, and Go. TensorFlow's architecture allows it to run on various platforms, from CPUs to GPUs and TPUs (Tensor Processing Units).

PyTorch, on the other hand, was created by Facebook's AI Research lab. PyTorch is known for its dynamic computational graphing, which makes debugging easier and more intuitive. It also has a strong community and extensive documentation.

Both frameworks are widely used in academia and industry for tasks ranging from image recognition to natural language processing. Understanding both can provide you with the flexibility to choose the right tool for your project.

Core Content

1. Tensors

Tensors are the fundamental data structure in TensorFlow and PyTorch, similar to arrays or matrices in other libraries like NumPy. They can be thought of as multi-dimensional arrays that support a wide range of operations.

Creating Tensors

In TensorFlow:

Python
1import tensorflow as tf
2
3# Create a scalar tensor (0D)
4scalar_tf = tf.constant(42)
5
6# Create a vector tensor (1D)
7vector_tf = tf.constant([1, 2, 3])
8
9# Create a matrix tensor (2D)
10matrix_tf = tf.constant([[1, 2], [3, 4]])
11
12print(scalar_tf)
13print(vector_tf)
14print(matrix_tf)
Output
tf.Tensor(42, shape=(), dtype=int32)
tf.Tensor([1 2 3], shape=(3,), dtype=int32)
tf.Tensor(
[[1 2]
[3 4]], shape=(2, 2), dtype=int32)

In PyTorch:

Python
1import torch
2
3# Create a scalar tensor (0D)
4scalar_torch = torch.tensor(42)
5
6# Create a vector tensor (1D)
7vector_torch = torch.tensor([1, 2, 3])
8
9# Create a matrix tensor (2D)
10matrix_torch = torch.tensor([[1, 2], [3, 4]])
11
12print(scalar_torch)
13print(vector_torch)
14print(matrix_torch)
Output
tensor(42)
tensor([1, 2, 3])
tensor([[1, 2],
      [3, 4]])

Tensor Operations

Both frameworks support a wide range of operations on tensors. Here are some examples:

In TensorFlow:

Python
1import tensorflow as tf
2
3# Element-wise addition
4a = tf.constant([1, 2, 3])
5b = tf.constant([4, 5, 6])
6c = tf.add(a, b)
7print(c) # Output: [5, 7, 9]

In PyTorch:

Python
1import torch
2
3# Element-wise addition
4a = torch.tensor([1, 2, 3])
5b = torch.tensor([4, 5, 6])
6c = a + b
7print(c) # Output: tensor([5, 7, 9])

2. Building a Simple Neural Network

Let's build a simple neural network to classify handwritten digits using the MNIST dataset.

TensorFlow Example

Python
1import tensorflow as tf
2from tensorflow.keras import layers, models
3
4# Load the MNIST dataset
5mnist = tf.keras.datasets.mnist
6(x_train, y_train), (x_test, y_test) = mnist.load_data()
7x_train, x_test = x_train / 255.0, x_test / 255.0
8
9# Build a simple neural network
10model = models.Sequential([
11 layers.Flatten(input_shape=(28, 28)),
12 layers.Dense(128, activation='relu'),
13 layers.Dropout(0.2),
14 layers.Dense(10)
15])
16
17# Compile the model
18model.compile(optimizer='adam',
19 loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
20 metrics=['accuracy'])
21
22# Train the model
23model.fit(x_train, y_train, epochs=5)
24
25# Evaluate the model
26test_loss, test_acc = model.evaluate(x_test, y_test)
27print(f'Test accuracy: {test_acc}')

PyTorch Example

Python
1import torch
2import torch.nn as nn
3import torch.optim as optim
4from torchvision import datasets, transforms
5
6# Load the MNIST dataset
7transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
8trainset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
9trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
10
11# Define a simple neural network
12class SimpleNN(nn.Module):
13 def __init__(self):
14 super(SimpleNN, self).__init__()
15 self.flatten = nn.Flatten()
16 self.fc1 = nn.Linear(28*28, 128)
17 self.relu = nn.ReLU()
18 self.dropout = nn.Dropout(0.2)
19 self.fc2 = nn.Linear(128, 10)
20
21 def forward(self, x):
22 x = self.flatten(x)
23 x = self.fc1(x)
24 x = self.relu(x)
25 x = self.dropout(x)
26 x = self.fc2(x)
27 return x
28
29model = SimpleNN()
30criterion = nn.CrossEntropyLoss()
31optimizer = optim.Adam(model.parameters(), lr=0.001)
32
33# Train the model
34for epoch in range(5):
35 for images, labels in trainloader:
36 optimizer.zero_grad()
37 outputs = model(images)
38 loss = criterion(outputs, labels)
39 loss.backward()
40 optimizer.step()
41
42# Evaluate the model (not shown here for brevity)

3. Training a Model

Training a model involves feeding data through the network, calculating the loss, and updating the weights to minimize that loss.

TensorFlow Example

Python
1import tensorflow as tf
2from tensorflow.keras import layers, models
3
4# Load the MNIST dataset
5mnist = tf.keras.datasets.mnist
6(x_train, y_train), (x_test, y_test) = mnist.load_data()
7x_train, x_test = x_train / 255.0, x_test / 255.0
8
9# Build a simple neural network
10model = models.Sequential([
11 layers.Flatten(input_shape=(28, 28)),
12 layers.Dense(128, activation='relu'),
13 layers.Dropout(0.2),
14 layers.Dense(10)
15])
16
17# Compile the model
18model.compile(optimizer='adam',
19 loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
20 metrics=['accuracy'])
21
22# Train the model
23model.fit(x_train, y_train, epochs=5)
24
25# Evaluate the model
26test_loss, test_acc = model.evaluate(x_test, y_test)
27print(f'Test accuracy: {test_acc}')

PyTorch Example

Python
1import torch
2import torch.nn as nn
3import torch.optim as optim
4from torchvision import datasets, transforms
5
6# Load the MNIST dataset
7transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
8trainset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
9trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
10
11# Define a simple neural network
12class SimpleNN(nn.Module):
13 def __init__(self):
14 super(SimpleNN, self).__init__()
15 self.flatten = nn.Flatten()
16 self.fc1 = nn.Linear(28*28, 128)
17 self.relu = nn.ReLU()
18 self.dropout = nn.Dropout(0.2)
19 self.fc2 = nn.Linear(128, 10)
20
21 def forward(self, x):
22 x = self.flatten(x)
23 x = self.fc1(x)
24 x = self.relu(x)
25 x = self.dropout(x)
26 x = self.fc2(x)
27 return x
28
29model = SimpleNN()
30criterion = nn.CrossEntropyLoss()
31optimizer = optim.Adam(model.parameters(), lr=0.001)
32
33# Train the model
34for epoch in range(5):
35 for images, labels in trainloader:
36 optimizer.zero_grad()
37 outputs = model(images)
38 loss = criterion(outputs, labels)
39 loss.backward()
40 optimizer.step()
41
42# Evaluate the model (not shown here for brevity)

4. Making Predictions

After training a model, you can use it to make predictions on new data.

TensorFlow Example

Python
1import tensorflow as tf
2from tensorflow.keras import layers, models
3
4# Load the MNIST dataset
5mnist = tf.keras.datasets.mnist
6(x_train, y_train), (x_test, y_test) = mnist.load_data()
7x_train, x_test = x_train / 255.0, x_test / 255.0
8
9# Build a simple neural network
10model = models.Sequential([
11 layers.Flatten(input_shape=(28, 28)),
12 layers.Dense(128, activation='relu'),
13 layers.Dropout(0.2),
14 layers.Dense(10)
15])
16
17# Compile the model
18model.compile(optimizer='adam',
19 loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
20 metrics=['accuracy'])
21
22# Train the model
23model.fit(x_train, y_train, epochs=5)
24
25# Make predictions
26predictions = model.predict(x_test)
27print(predictions[0])

PyTorch Example

Python
1import torch
2import torch.nn as nn
3import torch.optim as optim
4from torchvision import datasets, transforms
5
6# Load the MNIST dataset
7transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
8trainset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
9trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
10
11# Define a simple neural network
12class SimpleNN(nn.Module):
13 def __init__(self):
14 super(SimpleNN, self).__init__()
15 self.flatten = nn.Flatten()
16 self.fc1 = nn.Linear(28*28, 128)
17 self.relu = nn.ReLU()
18 self.dropout = nn.Dropout(0.2)
19 self.fc2 = nn.Linear(128, 10)
20
21 def forward(self, x):
22 x = self.flatten(x)
23 x = self.fc1(x)
24 x = self.relu(x)
25 x = self.dropout(x)
26 x = self.fc2(x)
27 return x
28
29model = SimpleNN()
30criterion = nn.CrossEntropyLoss()
31optimizer = optim.Adam(model.parameters(), lr=0.001)
32
33# Train the model
34for epoch in range(5):
35 for images, labels in trainloader:
36 optimizer.zero_grad()
37 outputs = model(images)
38 loss = criterion(outputs, labels)
39 loss.backward()
40 optimizer.step()
41
42# Make predictions
43model.eval()
44with torch.no_grad():
45 predictions = model(x_test[0].unsqueeze(0))
46print(predictions)

Practical Example

Let's put everything together in a complete example using TensorFlow. We'll build a simple neural network to classify handwritten digits from the MNIST dataset.

Python
1import tensorflow as tf
2from tensorflow.keras import layers, models
3from tensorflow.keras.datasets import mnist
4
5# Load the MNIST dataset
6(x_train, y_train), (x_test, y_test) = mnist.load_data()
7x_train, x_test = x_train / 255.0, x_test / 255.0
8
9# Build a simple neural network
10model = models.Sequential([
11 layers.Flatten(input_shape=(28, 28)),
12 layers.Dense(128, activation='relu'),
13 layers.Dropout(0.2),
14 layers.Dense(10)
15])
16
17# Compile the model
18model.compile(optimizer='adam',
19 loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
20 metrics=['accuracy'])
21
22# Train the model
23model.fit(x_train, y_train, epochs=5)
24
25# Evaluate the model
26test_loss, test_acc = model.evaluate(x_test, y_test)
27print(f'Test accuracy: {test_acc}')
28
29# Make predictions
30predictions = model.predict(x_test)
31print(predictions[0])

Summary

ConceptDescription
TensorsMulti-dimensional arrays for data representation.
Neural NetworksModels composed of layers that learn from data.
TrainingProcess of optimizing model parameters to minimize loss.
PredictionsUsing trained models to make predictions on new data.

What's Next?

Congratulations! You've learned the basics of TensorFlow and PyTorch, including tensors, building neural networks, training models, and making predictions. In the next topic, we'll explore Web Development with Flask, where you'll learn how to build web applications using Python.

By mastering these frameworks, you're well on your way to becoming a proficient data scientist or machine learning engineer. Keep experimenting and building projects to deepen your understanding!


PreviousClassification & Clustering (Decision Trees, K-Means)Next Web Development with Flask

Recommended Gear

Classification & Clustering (Decision Trees, K-Means)Web Development with Flask