Deep Learning pour le texte avec PyTorch
Shubham Jain
Instructor
"- Classifier les tweets comme
- Positif
"
"
"- Opération de convolution
"- Filtre :
"
"
"- Couche de convolution : applique des filtres aux données d'entrée
"`python
class SentimentAnalysisCNN(nn.Module):
----CODE_GLUE---- ```python def __init__(self, vocab_size, embed_dim):super().__init__()self.embedding = nn.Embedding(vocab_size, embed_dim)self.conv = nn.Conv1d(embed_dim, embed_dim, kernel_size=3, stride=1, padding=1)
----CODE_GLUE----
python
self.fc = nn.Linear(embed_dim, 2)
...{{6}}"
"- La méthode __init__ configure l’architecture
super() initialise la classe de base nn.Modulenn.Embedding crée des vecteurs de mots densesnn.Conv1d pour des données unidimensionnelles {{5}}""`python
...
def forward(self, text):
embedded = self.embedding(text).permute(0, 2, 1)
----CODE_GLUE---- ```python conved = F.relu(self.conv(embedded))conved = conved.mean(dim=2)
----CODE_GLUE----
python
return self.fc(conved){{4}}"
"- La couche d'embedding convertit le texte en embedding
"`python
vocab = [\"i\", \"love\", \"this\", \"book\", \"do\", \"not\", \"like\"]
word_to_idx = {word: i for i, word in enumerate(vocab)}
----CODE_GLUE---- ```python vocab_size = len(word_to_ix)embed_dim = 10book_samples = [ (\"L'histoire était captivante et m'a tenu en haleine jusqu'à la fin.\".split(),1), (\"J'ai trouvé les personnages superficiels et l'intrigue prévisible.\".split(),0) ]
----CODE_GLUE----
python
model = SentimentAnalysisCNN(vocab_size, embed_dim)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.1){{5}}"
"python
for epoch in range(10): for sentence, label in data:
"python
for sample in book_samples:
----CODE_GLUE----
python input_tensor = torch.tensor([word_to_idx[w] for w in sample], dtype=torch.long).unsqueeze(0)
Deep Learning pour le texte avec PyTorch