Learn practical skills, build real-world projects, and advance your career

PyTorch is a deep learning framework with its Pythonic nature. Although there are many advanced applications, here I would like to demonstrate a solution for a relatively simple case. This is the multi-class classification problem based on Iris data set. It's the one of the best known practices.The model will be developed classifies the samples based on four features:

  • sepal length (cm)
  • sepal width (cm)
  • petal length (cm)
  • petal width (cm)

Target labels (species) are:

  • Iris-setosa
  • Iris-versicolour
  • Iris-virginica

We will develop a model by using PyTorch having input layer (features), hidden layers and output layer (classes) as usual in neural networks.

We will follow these steps:

  • Preparing and loading dataset
  • Creating a model
  • Training and validating the model
  • Testing with unseen data

We start with importing necessary libraries.

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
from sklearn.model_selection import train_test_split

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

Dataset

We use dataset on Kaggle and you may use also this URL https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data for downloading .csv file.

dataset = pd.read_csv("../input/iris-dataset/iris.data.csv")