import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader

Define the training function

def train(model, train_loader, criterion, optimizer): model.train() total_loss = 0.0 correct = 0 total = 0

for inputs, labels in train_loader:
    inputs = inputs.to(device)
    labels = labels.to(device)

    optimizer.zero_grad()

    outputs = model(inputs)
    loss = criterion(outputs, labels)
    loss.backward()
    optimizer.step()

    total_loss += loss.item()

    _, predicted = outputs.max(1)
    total += labels.size(0)
    correct += predicted.eq(labels).sum().item()

return total_loss / len(train_loader), 100 * correct / total

Define the evaluation function

def evaluate(model, test_loader, criterion): model.eval() total_loss = 0.0 correct = 0 total = 0

with torch.no_grad():
    for inputs, labels in test_loader:
        inputs = inputs.to(device)
        labels = labels.to(device)

        outputs = model(inputs)
        loss = criterion(outputs, labels)

        total_loss += loss.item()

        _, predicted = outputs.max(1)
        total += labels.size(0)
        correct += predicted.eq(labels).sum().item()

return total_loss / len(test_loader), 100 * correct / total

Define the 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)
    self.rnn = nn.RNN(embedding_dim, hidden_dim, num_layers, batch_first=True)
    self.fc = nn.Linear(hidden_dim, label_size)

def forward(self, x):
    x = self.embedding(x)
    _, h = self.rnn(x)
    h = h[-1, :, :]
    out = self.fc(h)
    return out

Set hyperparameters

vocab_size = len(word_to_index) embedding_dim = 100 hidden_dim = 128 num_layers = 2 label_size = 3

Create the RNN model

model = RNN(vocab_size, embedding_dim, hidden_dim, num_layers, label_size) model = model.to(device)

Define the loss function and optimizer

criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters())

Train the model

num_epochs = 10 best_accuracy = 0.0

for epoch in range(num_epochs): train_loss, train_accuracy = train(model, train_loader, criterion, optimizer) test_loss, test_accuracy = evaluate(model, test_loader, criterion)

if test_accuracy > best_accuracy:
    best_accuracy = test_accuracy
    torch.save(model.state_dict(), 'best_model.pt')

print('Epoch:', epoch+1)
print('Train Loss:', train_loss, 'Train Accuracy:', train_accuracy)
print('Test Loss:', test_loss, 'Test Accuracy:', test_accuracy)
print()

Load the best model

model.load_state_dict(torch.load('best_model.pt'))

Evaluate the best model on the test set

test_loss, test_accuracy = evaluate(model, test_loader, criterion) print('Best Model - Test Loss:', test_loss, 'Test Accuracy:', test_accuracy)


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

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