Get startedGet started for free

Text generation using RNN - Training and Generation

The team at PyBooks now wants you to train and test the RNN model, which is designed to predict the next character in the sequence based on the provided input for auto-completion of book names. This project will help the team further develop models for text completion.

The model instance for the RNNmodel class is preloaded for you. The data variable has been preprocessed and encoded as a sequence.

The inputs and targets variable are preloaded for you.

This exercise is part of the course

Deep Learning for Text with PyTorch

View Course

Exercise instructions

  • Instantiate the loss function which will be used to compute the error of our model.
  • Instantiate the optimizer from PyTorch's optimization module.
  • Run the model training process by setting the model to the train mode and zeroing the gradients before performing an optimization step.
  • After the training process, switch the model to evaluation mode to test it on a sample input.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Instantiate the loss function
criterion = nn.____()
# Instantiate the optimizer
optimizer = torch.optim.____(model.parameters(), lr=0.01)

# Train the model
for epoch in range(100):
    model.____()
    outputs = model(inputs)
    loss = criterion(outputs, targets)
    optimizer.____()
    loss.backward()
    optimizer.step()
    if (epoch+1) % 10 == 0:
        print(f'Epoch {epoch+1}/100, Loss: {loss.item()}')

# Test the model
model.____()
test_input = char_to_ix['r']
test_input = nn.functional.one_hot(torch.tensor(test_input).view(-1, 1), num_classes=len(chars)).float()
predicted_output = model(test_input)
predicted_char_ix = torch.argmax(predicted_output, 1).item()
print(f"Test Input: 'r', Predicted Output: '{ix_to_char[predicted_char_ix]}'")
Edit and Run Code