Hi all,
I have built an LSTM model for binary class detection, and I am training with different hyperparameters but the loss just does not decrease.
I am feeding several time-series data through LSTM, then to predict true positive output[0,0,0,…1,0,0].
I am not too sure what else I am missing, if anyone could give me a piece of advice it will helpful
Thanks,
Here is my model,
` self.lstm = nn.LSTM(self.input_size, self.hidden_size, self.num_layers, dropout = self.drop_out, batch_first=True)
def forward(self, x):
hidden, cell = self.init_hidden()
out, (hn, cn) = self.lstm(x, (hidden, cell))
return out[:,:,-1]
def init_hidden(self):
weight = next((self.parameters())).data
hidden, cell = (weight.new(self.num_layers, self.batch_size, self.hidden_size).zero_().to(self.device),
weight.new(self.num_layers, self.batch_size, self.hidden_size).zero_().to(self.device))
return hidden, cell
and Training Loop
def train(self,input_size, batch_size,seq_length,hidden_size,num_layers,lr, epoch):
self.model.train()
for batch_idx, (data, target) in enumerate(self.train_loader):
self.optimizer.zero_grad()
data, target = data.to(self.device), target.to(self.device)
predictions = self.model(data.float())
self.globaliter += 1
loss = self.criterion(predictions.float(), target.float())
loss.backward()
self.optimizer.step()
pred = predictions.detach()
pred[pred >0.5] = 1
pred[pred<=0.5] = 0
correct_indx = pred[target == 1]
out_size = correct_indx[correct_indx == 1]
accuracy = out_size.shape[0] / target.shape[0]
if batch_idx % self.log_interval == 0:
print('\nTrain Epoch: {}\tLoss: {:.6f}\tAccuracy: {} '.format(epoch, loss.item(), accuracy * 100))
with self.train_summary_writer.as_default():
summary.scalar('loss', loss.item(), step=self.globaliter)
summary.scalar('accuracy', accuracy, step=self.globaliter)