Classification tasks are fundamental in machine learning, involving the categorization of input data into distinct classes or categories based on their features. In this tutorial, we'll learn how to implement data classification using PyTorch deep learning framework.
We'll cover the following topics:
- Introduction to classification
- Preparing data
- Building the classifier model
- Training the model
- Prediction and accuracy check
- Conclusion
- Source code listing
Let's get started.
Please note that this tutorial provides a basic understanding of implementing data classification with PyTorch. Keep in mind that parameters and model definitions may need adjustments when working with larger datasets.
Introduction to classification
Classification is a supervised learning technique in machine learning and statistics used to categorize data points into predefined classes or categories based on their features. It involves training a model on a labeled dataset, where each data point is associated with a specific class or category. The goal of classification is to learn a mapping from input features to class labels, allowing the model to make accurate predictions on unseen data.
In classification tasks, the input data is typically represented as a set of features, and the output is a discrete class label or category. The model learns patterns and relationships in the input data to classify new instances into one of the predefined classes. Common examples of classification tasks include spam email detection, sentiment analysis, image recognition, and medical diagnosis.
Preparing data
We'll begin by loading the necessary libraries for this tutorial.
We use the Iris dataset and load it using the load_iris() function from scikit-learn. We check the size of the dataset, which contains 150 samples with 4 features. The number of features is important for defining the input size of our model later on.
Next, we define model parameters and hyperparameters.
- input_size defines the input size of the data X.
- hidden_size is size of hidden layer. The hidden layer is an intermediate layer between the input and output layers.
- num_classes is the number of classes in target variable y. We can calculate it with len(set(y)).
- learning_rate defines the learning rate for the optimization algorithm used to train the neural network.
- batch_size is the batch size for mini-batch gradient descent during training.
- num_epochs specifies the number of epochs or iterations over the entire dataset during training.
Then we split data into train and test parts. Here, we use 20 percent of data as test data.
The input data needs to be converted to PyTorch tensors. Below, we convert the training data into tensors and create PyTorch dataset and DataLoader for train set.
Building the classifier model
We'll define a Classifier class using PyTorch's nn.Linear module. The model class consists of the first fully connected linear layer, ReLU activation function, and the second fully connected linear layer.
The forward() method is for the forward pass through the neural network. It takes input and output tensor data.
We define the train() method for training the model. This method requires a DataLoader for the training dataset, a loss function, an optimizer, and the number of training epochs.
The predict() method is used for prediction.
Training the model
Next, we instantiate the Classifier model, providing the input size, hidden layer size, and number of classes. This creates a neural network model with the specified architecture.
The loss function is defined to compute the error between the predicted and actual class labels. The nn.CrossEntropyLoss() is commonly used for classification tasks where the output can belong to multiple classes. We use stochastic gradient descent (torch.optim.SGD) as the optimization algorithm with a specified learning rate (lr), which is responsible for updating the model parameters during training to minimize the loss.
Prediction and accuracy check
After training, we predict the test data using the trained model. First, we convert the test data to PyTorch tensor type. Then, we pass this tensor to the trained model for prediction. The returned prediction data is converted to a numpy array type for further analysis.
Next, we calculate the prediction accuracy by comparing the predicted labels with the actual labels from the test set. The accuracy is computed as the ratio of correctly predicted samples to the total number of samples in the test set. Finally, we print the accuracy and generate a classification report to evaluate the performance of the model on the test data. The classification report provides metrics such as precision, recall, F1-score, and support for each class label, offering insights into the model's performance across different classes.
The result is as follows.
precision recall f1-score support
0 1.00 1.00 1.00 10
1 1.00 0.89 0.94 9
2 0.92 1.00 0.96 11
accuracy 0.97 30
macro avg 0.97 0.96 0.97 30
weighted avg 0.97 0.97 0.97 30
Source code listing
No comments:
Post a Comment