Mein RNN mit Aufmerksamkeitsmodell sagt immer dieselbe Klasse voraus, auch wenn meine Daten nicht unausgeglichen sindPython

Python-Programme
Anonymous
 Mein RNN mit Aufmerksamkeitsmodell sagt immer dieselbe Klasse voraus, auch wenn meine Daten nicht unausgeglichen sind

Post by Anonymous »

Dies ist mein Aufmerksamkeitsschichtcode:
Implementierung der Aufmerksamkeitsschicht

Code: Select all

class Attention(nn.Module):
def __init__(self, feature_dim, step_dim, bias=True, **kwargs):
super(Attention, self).__init__(**kwargs)

self.supports_masking = True
self.bias = bias
self.feature_dim = feature_dim
self.step_dim = step_dim
self.features_dim = 0

weight = torch.zeros(feature_dim, 1)
nn.init.kaiming_uniform_(weight)
self.weight = nn.Parameter(weight)

if bias:
self.b = nn.Parameter(torch.zeros(step_dim))

def forward(self, x, mask=None):
feature_dim = self.feature_dim
step_dim = self.step_dim
eij = torch.mm(
x.contiguous().view(-1, feature_dim),
self.weight
).view(-1, step_dim)

if self.bias:
eij = eij + self.b

eij = torch.tanh(eij)
a = torch.exp(eij)

if mask is not None:
a = a * mask
a = a / (torch.sum(a, 1, keepdim=True) + 1e-10)
weighted_input = x * torch.unsqueeze(a, -1)
return torch.sum(weighted_input, 1)
Dies ist RNN-Code:

Code: Select all

# Instantiate the model w/ hyperparams
weights_matrix = weights_matrix
output_size = 13  # number of classes to predict
hidden_dim = 64
drop_prob = 0.5
# The RNN model that will be used to perform classification
class AttentionLSTM(nn.Module):
def __init__(self, weights_matrix, output_size, hidden_dim, drop_prob):
super(AttentionLSTM, self).__init__()

# embedding layers
self.embedding, self.num_embeddings, self.embeddings_size = create_emb_layer(weights_matrix, True)
# embedding dropout
self.dropout = nn.Dropout2d(drop_prob)

# First lstm and GRU layers
self.lstm1 = nn.LSTM(self.embeddings_size, hidden_dim, batch_first=True, bidirectional=True)
self.gru1 = nn.GRU(hidden_dim * 2, hidden_dim, bidirectional=True, batch_first=True)

# attention layer
self.attention = Attention(hidden_dim*2, seq_length)
# Second lstm and GRU layers
self.lstm2 = nn.LSTM(hidden_dim * 2, hidden_dim, batch_first=True, bidirectional=True)
self.gru2 = nn.GRU(hidden_dim * 2, hidden_dim, bidirectional=True, batch_first=True)
# linear
self.fc = nn.Linear(hidden_dim * 2, hidden_dim * 2)
self.out = nn.Linear(hidden_dim * 2, output_size)

# activation functions
self.sigmoid = nn.Sigmoid() # for hidden layers
self.softmax = nn.Softmax(dim=1) # for output layer

def forward(self, x):
batch_size = x.size(0)
# embedding output
x = x.long()
embeds = self.embedding(x)
embeds = torch.squeeze(torch.unsqueeze(embeds, 0))

# lstm, and gru outputs
lstm_out1, _ = self.lstm1(embeds)
gru_out1, _ = self.gru1(lstm_out1)
gru_out1 = gru_out1.view(batch_size, -1, hidden_dim * 2)
attention_out = self.attention(gru_out1, seq_length)
attention_out = attention_out.view(batch_size, -1, hidden_dim * 2)
attention_out = self.sigmoid(attention_out)
lstm_out2, _ = self.lstm2(attention_out)
# slice lstm_out to just get output of last element of the input sequence
lstm_out2 = lstm_out2[:, -1]
gru_out2, _ = self.gru2(lstm_out2)
# linear outputs
fc_out = self.softmax(self.fc(gru_out2))
final_out = self.out(fc_out)
return final_out
Ich bin sicher, dass mein Datensatz nach dem Vorverarbeitungsschritt ausgeglichen ist, aber mein Modell sagt immer die gleiche Ausgabe voraus. Präzision und fscore ändern sich für jede Eingabe. Dieses Problem führt jedoch dazu, dass mein Erinnerungswert 1,0 beträgt, da die Ausgabe unabhängig von der Eingabe immer gleich ist.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post