How to create a CNN in pytorch

This recipe helps you create a CNN in pytorch

Recipe Objective

How to create a CNN in PyTorch?

The CNN means Convolution Neural Network which is type of Neural network, majorly used for problems like image classification, image processing. Here the Convolution word is nothing but a mathematical combination of two functions which is to produce the third function, it merges the two sets of information. There are other applications also for CNN in sequential data for e.g, audio, time series and NLP. It is widely used for applications like images, audio, videos, text and time series modelling. There are types of CNN which are as follows: 1D Convolution :- This is widely used where the input data is sequential like text or audio. 2D Convolution :- If the input data is image then 2D convolution is used. 3D Convolution :- It is used widely in medical applications like medical imaging, or detecting events in videos.

PyTorch vs Tensorflow - Which One Should You Choose For Your Next Deep Learning Project ?

Step 1 - Import library

import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
import numpy as np

Step 2 - Take Sample data

Sample_1D = torch.tensor([10,9,8,7,6,5,4,3,2,1], dtype=torch.float)
Sample_2D = torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], dtype=torch.float)
Sample_2d_image = torch.tensor([[[10,20,30,40,50,60,70,80,90,100],[10,20,30,40,50,60,70,80,90,100],[10,20,30,40,50,60,70,80,90,100]], [[10,20,30,40,50,60,70,80,90,100],[10,20,30,40,50,60,70,80,90,100],[10,20,30,40,50,60,70,80,90,100]], [[10,20,30,40,50,60,70,80,90,100],[10,20,30,40,50,60,70,80,90,100],[10,20,30,40,50,60,70,80,90,100]]], dtype=torch.float)
print("This is 1D data:", Sample_1D, Sample_1D.shape,"\n")
print("This is 2D data:",Sample_2D, Sample_2D.shape,"\n")
print("This is 2D image data:",Sample_2d_image, Sample_2d_image.shape,"\n")

This is 1D data: tensor([10.,  9.,  8.,  7.,  6.,  5.,  4.,  3.,  2.,  1.]) torch.Size([10]) 

This is 2D data: tensor([[ 1.,  2.,  3.,  4.,  5.],
        [ 6.,  7.,  8.,  9., 10.]]) torch.Size([2, 5]) 

This is 2D image data: tensor([[[ 10.,  20.,  30.,  40.,  50.,  60.,  70.,  80.,  90., 100.],
         [ 10.,  20.,  30.,  40.,  50.,  60.,  70.,  80.,  90., 100.],
         [ 10.,  20.,  30.,  40.,  50.,  60.,  70.,  80.,  90., 100.]],

        [[ 10.,  20.,  30.,  40.,  50.,  60.,  70.,  80.,  90., 100.],
         [ 10.,  20.,  30.,  40.,  50.,  60.,  70.,  80.,  90., 100.],
         [ 10.,  20.,  30.,  40.,  50.,  60.,  70.,  80.,  90., 100.]],

        [[ 10.,  20.,  30.,  40.,  50.,  60.,  70.,  80.,  90., 100.],
         [ 10.,  20.,  30.,  40.,  50.,  60.,  70.,  80.,  90., 100.],
         [ 10.,  20.,  30.,  40.,  50.,  60.,  70.,  80.,  90., 100.]]]) torch.Size([3, 3, 10]) 

Step 3 - Unsqueeze the 1D data

Sample_1D = Sample_1D.unsqueeze(0).unsqueeze(0)
Sample_1D.shape

torch.Size([1, 1, 10])

Here the Sample_1D data consist of 10 integer data, so we are going to covert it into a tensor size of [1,1,10]

Step 4 - CNN output for 1D convolution.

output_1D = nn.Conv1d(in_channels=1, out_channels=1, kernel_size=3, stride=1)
print("This is output of 1D Convolution:", output_1D(Sample_1D), "\n")
print("This is the shape of output of 1D convolution:", output_1D(Sample_1D).shape)

This is output of 1D Convolution: tensor([[[12.0904, 10.6747,  9.2589,  7.8432,  6.4275,  5.0117,  3.5960,
           2.1803]]], grad_fn=)

This is the shape of output of 1D convolution: torch.Size([1, 1, 8])

Step 5 - Unsqueeze the 2D data

Sample_2d_image = Sample_2d_image.unsqueeze(0)
Sample_2d_image.shape

torch.Size([1, 3, 3, 10])

Step 6 - CNN output for 2D Convolution.

output_2d_image = nn.Conv2d(in_channels=3, out_channels=1, kernel_size=3, stride=1)
print("This is output for 2d Convolution Image data:",output_2d_image(Sample_2d_image), "\n")
print("This is the shape for output of 2d Convolution image data:",output_2d_image(Sample_2d_image).shape)

This is output for 2d Convolution Image data: tensor([[[[0.4887, 1.4951, 2.5015, 3.5079, 4.5143, 5.5207, 6.5271, 7.5334]]]],
       grad_fn=) 

This is the shape for output of 2d Convolution image data: torch.Size([1, 1, 1, 8])

{"mode":"full","isActive":false}

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.