# 1. Define the training and evaluation function
def train(model, iterator, optimizer, criterion):
    model.train()
    
    epoch_loss = 0
    epoch_acc = 0
    
    for batch in iterator:
        optimizer.zero_grad()
        
        text, text_lengths = batch.text
        predictions = model(text, text_lengths).squeeze(1)
        
        loss = criterion(predictions, batch.label)
        acc = binary_accuracy(predictions, batch.label)
        
        loss.backward()
        optimizer.step()
        
        epoch_loss += loss.item()
        epoch_acc += acc.item()
        
    return epoch_loss / len(iterator), epoch_acc / len(iterator)


def evaluate(model, iterator, criterion):
    model.eval()
    
    epoch_loss = 0
    epoch_acc = 0
    
    with torch.no_grad():
        for batch in iterator:
            text, text_lengths = batch.text
            predictions = model(text, text_lengths).squeeze(1)
            
            loss = criterion(predictions, batch.label)
            acc = binary_accuracy(predictions, batch.label)

            epoch_loss += loss.item()
            epoch_acc += acc.item()
        
    return epoch_loss / len(iterator), epoch_acc / len(iterator)
    
    
def binary_accuracy(preds, y):
    rounded_preds = torch.round(torch.sigmoid(preds))
    correct = (rounded_preds == y).float()
    acc = correct.sum() / len(correct)
    return acc


# 2. Build a RNN model for sentiment analysis
class RNN(nn.Module):
    def __init__(self, vocab_size, embedding_dim, hidden_dim, num_layers, label_size):
        super(RNN, self).__init__()
        
        self.embedding = nn.Embedding(vocab_size, embedding_dim, padding_idx=padding_idx)
        self.rnn = nn.RNN(embedding_dim, hidden_dim, num_layers=num_layers, batch_first=True)
        self.fc = nn.Linear(hidden_dim, label_size)
        
    def forward(self, text, text_lengths):
        embedded = self.embedding(text)
        packed = nn.utils.rnn.pack_padded_sequence(embedded, text_lengths, batch_first=True)
        output, _ = self.rnn(packed)
        output, _ = nn.utils.rnn.pad_packed_sequence(output, batch_first=True)
        output = self.fc(output[:, -1, :])
        return output


# 3. Train the model and compute the accuracy
model = RNN(vocab_size, embedding_dim, hidden_dim, num_layers=1, label_size=label_size)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters())

for epoch in range(num_epochs):
    train_loss, train_acc = train(model, train_iter, optimizer, criterion)
    val_loss, val_acc = evaluate(model, val_iter, criterion)
    print(f'Epoch: {epoch+1}')
    print(f'Train Loss: {train_loss:.3f} | Train Acc: {train_acc*100:.2f}%')
    print(f'Val Loss: {val_loss:.3f} | Val Acc: {val_acc*100:.2f}%')

# 4. Train a model with better accuracy
# You can try different optimizers, learning rates, number of layers, hidden dimensions, and more.
model = RNN(vocab_size, embedding_dim, hidden_dim, num_layers=2, label_size=label_size)
optimizer = optim.SGD(model.parameters(), lr=0.1)

This code demonstrates how to build a basic RNN model for sentiment analysis using PyTorch. It includes the following steps:

  1. Define training and evaluation functions: These functions handle the training loop, calculating loss and accuracy, and evaluating the model's performance on a validation set.
  2. Build the RNN model: This section defines the RNN class, which encapsulates the embedding layer, recurrent layer, and fully connected layer.
  3. Train the model: This involves iterating over training data, calculating gradients, and updating model parameters.
  4. Improve model accuracy: The code encourages you to experiment with different hyperparameters, optimizers, and model architectures to enhance the model's performance.

By following these steps, you can gain a hands-on understanding of building and training RNN models for sentiment analysis with PyTorch.


原文地址: https://www.cveoy.top/t/topic/o60r 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录