SoFunction
Updated on 2025-03-02

Sample code for implementing character-level last name text classification based on RNN based on pytorch

When implementing character-level last name text classification using PyTorch-based RNN, we can use a very simple RNN model to process the input character sequence and apply it to the last name classification task. Below is a basic example code, including data preprocessing, model definition, and training process.

First, we need to import the necessary libraries:

import torch
import  as nn
import  as optim
from  import Dataset, DataLoader
import numpy as np

Next, we will define the dataset and data preprocessing functions. Here, we assume that we have a dataset containing the last name and its corresponding country, each of which consists of one or more characters. We first define a dataset class and then implement the data preprocessing function:

class SurnameDataset(Dataset):
    def __init__(self, data):
         = data
    
    def __len__(self):
        return len()
    
    def __getitem__(self, idx):
        return [idx]
        
# Assume that our data format is (surname, country), for example ('Smith', 'USA')# Here it is assumed that the data has been preprocessed into the corresponding numerical representation# For example, map characters to numbers, country names to numbers, etc. 
# Data preprocessing functiondef preprocess_data(data):
    processed_data = []
    for surname, country in data:
        # Convert last name to character index list        surname_indices = [char_to_index[char] for char in surname]
        # Convert country to corresponding number        country_index = country_to_index[country]
        processed_data.append((surname_indices, country_index))
    return processed_data

Next, we define a simple RNN model to handle character-level last name classification tasks. In this example, we use a single layer of LSTM as our RNN model. The code is as follows:

class SurnameRNN():
    def __init__(self, input_size, hidden_size, output_size):
        super(SurnameRNN, self).__init__()
        self.hidden_size = hidden_size
         = (input_size, hidden_size)
         = (hidden_size, hidden_size)
         = (hidden_size, output_size)
 
    def forward(self, input, hidden):
        embedded = (input).view(1, 1, -1)
        output, hidden = (embedded, hidden)
        output = ((1, -1))
        return output, hidden
 
    def init_hidden(self):
        return ((1, 1, self.hidden_size), (1, 1, self.hidden_size))

In the above code, we define an RNN model called SurnameRNN. The input size of the model is input_size (i.e. the number of characters), the hidden layer size is hidden_size, and the output size is output_size (i.e. the number of countries). The model includes an embedding, an LSTM layer, and a fully connected layer (fc).

Next, we need to define the loss function and optimizer and train it:

input_size = len(char_to_index)  # Number of characters in surnameshidden_size = 128
output_size = len(country_to_index)  # Number of countrieslearning_rate = 0.001
num_epochs = 10
 
model = SurnameRNN(input_size, hidden_size, output_size)
criterion = ()
optimizer = ((), lr=learning_rate)
 
# Suppose we have a preprocessed data set, then follow_data# The data format is (surname_indices, country_index) 
# Divide the data into training sets and test setstrain_data = surname_data[:800]
test_data = surname_data[800:]
 
# Start trainingfor epoch in range(num_epochs):
    total_loss = 0
    for surname_indices, country_index in train_data:
        model.zero_grad()
        hidden = model.init_hidden()
        surname_tensor = (surname_indices, dtype=)
        country_tensor = ([country_index], dtype=)
 
        for i in range(len(surname_indices)):
            output, hidden = model(surname_tensor[i], hidden)
        
        loss = criterion(output, country_tensor)
        total_loss += ()
        ()
        ()
    
    print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, total_loss / len(train_data)))

During the above training process, we traverse each sample in the training dataset, input the last name characters into the model one by one, and calculate the loss and perform backpropagation to update the model parameters.

This is an example of a simple RNN model based on PyTorch for character-level last name text classification. Of course, in actual tasks, more data preprocessing, model parameter adjustment and other tasks may also be considered.

To use the above code, you need to follow these steps:

  1. Prepare your last name dataset into a list with each element containing a last name and the corresponding country (e.g. [('Smith', 'USA'), ('Li', 'China'), ...]).

  2. Data preprocessing: according to your data format, implementpreprocess_dataFunction, convert last name to character index list and country to corresponding number.

  3. Define the model: Set the appropriate input size, hidden layer size, and output size according to your dataset and task requirements, and define an RNN model (such as in the above codeSurnameRNNkind).

  4. Define loss functions and optimizers: select appropriate loss functions (such as cross entropy loss functions)()) and optimizers (such as stochastic gradient descent optimizer())。

  5. Divide datasets: Divide the dataset into training sets and test sets according to your needs.

  6. Start training: Use training set data for model training. In each epoch, each sample in the training set is traversed, input it into the model, calculate the loss and perform backpropagation and parameter updates.

  7. Evaluate the model: Use test set data to evaluate the performance of the model.

Please note that the above code only provides a basic example, and you may need to make appropriate modifications and adjustments based on the specific tasks and data characteristics. In addition, other model architectures can be explored, hyperparameters can be adjusted, etc. can be adjusted to improve model performance.

Here is a sample code for testing a trained model:

# Import the necessary librariesimport torch
from  import DataLoader
 
# Define test functionsdef test_model(model, test_data):
    ()  # Set the model to evaluation mode    correct = 0
    total = 0
    with torch.no_grad():
        for surname_indices, country_index in test_data:
            surname_tensor = (surname_indices, dtype=)
            country_tensor = ([country_index], dtype=)
            
            hidden = model.init_hidden()
            
            for i in range(len(surname_indices)):
                output, hidden = model(surname_tensor[i], hidden)
            
            _, predicted = (, 1)
            
            total += 1
            if predicted == country_tensor:
                correct += 1
    
    accuracy = correct / total
    print('Accuracy on test data: {:.2%}'.format(accuracy))
 
# Load the test datasettest_dataset = SurnameDataset(test_data)
test_loader = DataLoader(test_dataset, batch_size=1, shuffle=True)
 
# Load the trained modelmodel_path = "path_to_your_trained_model.pt"
model = (model_path)
 
# Test the modeltest_model(model, test_loader)

In the above code, we first define a test_model function to test the accuracy of the model on the test dataset. Then we load the test dataset and load the previously trained model (please replace model_path with your own model path). Finally, we call the test_model function to test the model and print out the accuracy.

Note that before running the test code, make sure you have trained the model and saved it to the specified path.

The above is the detailed content of the sample code for implementing character-level last name text classification based on RNN based on pytorch. For more information about pytorch RNN character-level last name classification, please pay attention to my other related articles!