Deep Learning cho Văn bản với PyTorch
Shubham Jain
Data Scientist

Vì sao?
Ví dụ: Phát hiện mỉa mai trong tweet
"Tôi thật thích kẹt xe."
# Import libraries from torch.utils.data import Dataset, DataLoader# Create a class class TextDataset(Dataset):def __init__(self, text): self.text = textdef __len__(self): return len(self.text)def __getitem__(self, idx): return self.text[idx]
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)

Tweet:
"Yêu quay phim,
ghét lời thoại.
Diễn xuất xuất sắc,
nhưng cốt truyện nhạt."
Kiến trúc LSTM: Cổng vào, cổng quên, cổng ra
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
Tiêu đề email:
"Chúc mừng!
Bạn trúng chuyến đi
miễn phí tới Hawaii!"

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 cho Văn bản với PyTorch