PyTorch で学ぶテキストの Deep Learning
Shubham Jain
Data Scientist

なぜ?
例: ツイートの皮肉検出
"渋滞にハマるの、本当に大好き。"
# 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)

ツイート:
"映像は素晴らしいが、
セリフは最悪。
演技は抜群だが、
設定が平板。"
LSTMの構造: 入力ゲート、忘却ゲート、出力ゲート
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
メール件名:
"おめでとう!
ハワイ旅行が当選!"
GRU(Gated Recurrent Unit)は、全文脈がなくてもスパム的パターンを素早く検知できる

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
PyTorch で学ぶテキストの Deep Learning