Spaces:
Sleeping
Sleeping
File size: 2,093 Bytes
6855218 730469b 233adb5 6855218 06c46fb 6855218 06c46fb 6855218 233adb5 1534a11 6855218 1534a11 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# Prédit 33% environ partout (dans le cas 3 classes)
# class EmotionClassifier(nn.Module):
# def __init__(self, feature_dim, num_labels):
# super(EmotionClassifier, self).__init__()
# self.fc1 = nn.Linear(feature_dim, 256)
# self.relu = nn.ReLU()
# self.dropout = nn.Dropout(0.3)
# self.fc2 = nn.Linear(256, num_labels)
# def forward(self, x):
# x = self.fc1(x)
# x = self.relu(x)
# x = self.dropout(x)
# return self.fc2(x)
import torch
import torch.nn as nn
import torch.nn.functional as F
class Attention(nn.Module):
"""Mécanisme d’attention permettant de pondérer l’importance des caractéristiques audio"""
def __init__(self, hidden_dim):
super(Attention, self).__init__()
self.attention_weights = nn.Linear(hidden_dim, 1)
def forward(self, lstm_output):
# lstm_output: (batch_size, sequence_length, hidden_dim)
attention_scores = self.attention_weights(lstm_output) # (batch_size, sequence_length, 1)
attention_weights = torch.softmax(attention_scores, dim=1) # Normalisation softmax
weighted_output = lstm_output * attention_weights # Pondération des features
return weighted_output.sum(dim=1) # Somme pondérée sur la séquence
class EmotionClassifier(nn.Module):
"""Modèle de classification des émotions basé sur BiLSTM et attention"""
def __init__(self, feature_dim, num_labels, hidden_dim=128):
super(EmotionClassifier, self).__init__()
self.lstm = nn.LSTM(feature_dim, hidden_dim, batch_first=True, bidirectional=True)
self.attention = Attention(hidden_dim * 2) # Bidirectionnel → hidden_dim * 2
self.fc = nn.Linear(hidden_dim * 2, num_labels) # Couche de classification finale
def forward(self, x):
lstm_out, _ = self.lstm(x) # (batch_size, sequence_length, hidden_dim*2)
attention_out = self.attention(lstm_out) # (batch_size, hidden_dim*2)
logits = self.fc(attention_out) # (batch_size, num_labels)
return logits
|