Hi @ptrblck i wonder if you could clarify me on something. I’m running this bidirectional LSTM which i feed with dataloaders and loss is reducing but the accuracy remains the same. I feel like i’m doing something wrong on the preds variable when i call the softmax function. The code is as below.
class BiLSTM(nn.Module):
def __init__(self, input_dim, hidden_dim, layer_dim, output_dim):
    super().__init__()
    self.input_dim = input_dim
    self.hidden_dim = hidden_dim
    self.layer_dim = layer_dim
    self.lstm = nn.LSTM(input_dim, hidden_dim, layer_dim, dropout=0.2, bidirectional=True, batch_first=True) 
    self.fc_1 =  nn.Linear(hidden_dim*2, 128) #fully connected 1  
    self.fc = nn.Linear(hidden_dim, output_dim) #fully connected last layer
    self.relu = nn.ReLU()  #activation function
       
def forward(self,x):
    h_0 = torch.zeros(self.layer_dim*2, x.size(0), self.hidden_dim).requires_grad_() #hidden state
    c_0 = torch.zeros(self.layer_dim*2, x.size(0), self.hidden_dim).requires_grad_() #internal state
    # Propagate input through LSTM
    output, (hn, cn) = self.lstm(x, (h_0, c_0)) #lstm with input, hidden, and internal state
    output = output[:,-1,:]
    out = self.relu(output)
    out = self.fc_1(out) #first Dense
    out = self.relu(out) #relu
    out = self.fc(out) #Final Output
    #sigmoid
    out = torch.sigmoid(out)
    #out = F.log_softmax(out, dim=1).argmax(dim=1)
    
    return out

input_dim = 42
hidden_dim = 128
layer_dim = 2
output_dim = 1
lr = 0.001
n_epochs = 100
iterations_per_epoch = len(trn_ds)
best_acc = 0
patience, trials = 10, 0
model = BiLSTM(input_dim, hidden_dim, layer_dim, output_dim)
model = model.train()
criterion = nn.BCELoss()
opt = torch.optim.Adam(model.parameters(), lr=lr)
print(‘Start model training’)
for epoch in range(1, n_epochs + 1):
for i, (x_batch, y_batch) in enumerate(trn_dl):
    y_batch = y_batch.unsqueeze(1)
    y_batch = y_batch.float()
    out = model(x_batch)
    loss = criterion(out.float(), y_batch)
    #loss.requires_grad = True
    opt.zero_grad()
    loss.backward()
    opt.step()

model.eval()
correct, total = 0, 0
for x_val, y_val in val_dl:
    x_val, y_val = [t.cpu() for t in (x_val, y_val)]
    out = model(x_val)
    preds = F.log_softmax(out, dim=1).argmax(dim=1)
    total += y_val.size(0)
    correct += (preds == y_val).sum().item()

acc = correct / total

#if epoch % 5 == 0:
print(f’Epoch: {epoch:3d}. Loss: {loss.item():.4f}. Acc.: {acc:2.2%}’)
if acc > best_acc:
    trials = 0
    best_acc = acc
    torch.save(model.state_dict(), 'bestSimpleLSTM.pth')
    print(f'Epoch {epoch} best model saved with accuracy: {best_acc:2.2%}')
else:
    trials += 1
    if trials >= patience:
        print(f'Early stopping on epoch {epoch}')
        break

Thank you.