# 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
Я уверен, что мой набор данных сбалансирован после этапа предварительной обработки, но моя модель всегда предсказывает один и тот же результат. Точность и fscore меняются для каждого ввода, однако из-за этой проблемы моя оценка запоминания равна 1,0, поскольку выходные данные всегда одинаковы, независимо от входных данных.
Это мой код уровня внимания: Реализация уровня внимания [code]class Attention(nn.Module): def __init__(self, feature_dim, step_dim, bias=True, **kwargs): super(Attention, self).__init__(**kwargs)
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) [/code] Это код RNN: [code]# 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__()
# 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 [/code] Я уверен, что мой набор данных сбалансирован после этапа предварительной обработки, но моя модель всегда предсказывает один и тот же результат. Точность и fscore меняются для каждого ввода, однако из-за этой проблемы моя оценка запоминания равна 1,0, поскольку выходные данные всегда одинаковы, независимо от входных данных.