Recurrent neural networks for text classification

Deep Learning for Text with PyTorch

Shubham Jain

Data Scientist

RNNs for text

  • Handle sequences of varying lengths
  • Maintain an internal short-term memory
  • CNNs spot patterns in chunks
  • RNNs remember past words for greater meaning
Deep Learning for Text with PyTorch

RNNs for text classification

Sarcasm Image

Why?

  • RNNs can read sentences like humans, one word at a time
  • Understand context and order

Example: Detecting sarcasm in a tweet

"I just love getting stuck in traffic."

  • Sarcastic
Deep Learning for Text with PyTorch

Recap: Implementing Dataset and DataLoader

# Import libraries
from torch.utils.data import Dataset, DataLoader

# Create a class class TextDataset(Dataset):
def __init__(self, text): self.text = text
def __len__(self): return len(self.text)
def __getitem__(self, idx): return self.text[idx]
Deep Learning for Text with PyTorch

RNN implementation

sample_tweet = "This movie had a great plot and amazing acting."
# Preprocess the review and convert it to a tensor (not shown for brevity)
# ...
sentiment_prediction = model(sample_tweet_tensor)
  • Train an RNN model to classify tweet as positive or negative
  • Output: "Positive"
Deep Learning for Text with PyTorch

RNN variation: LSTM

Sentiment Analysis Image

Tweet:

  "Loved the cinematography, 
  hated the dialogue. 
  The acting was exceptional,
  but the plot fell flat."
  • Long Short Term Memory (LSTM) can capture complexities where RNNs may struggle
Deep Learning for Text with PyTorch

LSTM

LSTM architecture: Input gate, forget gate, and output gate

class LSTMModel(nn.Module):

def __init__(self, input_size, hidden_size, output_size): super(LSTMModel, self).__init__() self.lstm = nn.LSTM(input_size, hidden_size, batch_first=True) self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x): _, (hidden, _) = self.lstm(x) output = self.fc(hidden.squeeze(0)) return output
Deep Learning for Text with PyTorch

RNN variation: GRU

  • Email subject:

       "Congratulations!
        You've won a free trip 
        to Hawaii!"
    

 

  • Gated Recurrent Unit (GRU) can quickly recognize spammy patterns without needing the full context

Email spam

Deep Learning for Text with PyTorch

GRU

class GRUModel(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(GRUModel, self).__init__()
        self.gru = nn.GRU(input_size, hidden_size, batch_first=True)
        self.fc = nn.Linear(hidden_size, output_size)

def forward(self, x): _, hidden = self.gru(x) output = self.fc(hidden.squeeze(0)) return output
Deep Learning for Text with PyTorch

Let's practice!

Deep Learning for Text with PyTorch

Preparing Video For Download...