Hello All,
I am building an LSTM based classifier for EEG motor imagery Data for 2 classes. The data is from a 64 channel EEG and each channel has 20000 data points. The model is being trained for 50 epochs and converges for a decent loss. While testing the model for an individual file, the Sigmoid outputs only negative value irrespective of the class. Please tell me where could I be going wrong. Sorry for a trivial question, I am relatively new to the field and recently shifted to Pytorch
Attaching my code below
class pytorch_lstm(net.Module):
def __init__(self, features, hidden_size, sequence_length, loss_function='BCE'):
super(pytorch_lstm, self).__init__()
self.loss = loss_function
self.features = features
self.hidden_size = hidden_size
self.seq_length = sequence_length
self.lstm = net.LSTM(
input_size=self.features,
hidden_size=hidden_size,
num_layers=1,
batch_first=True
)
self.linear = net.Linear(self.hidden_size * self.seq_length, 1)
def init_Hidden(self):
hidden_state = torch.zeros(1, 1, self.hidden_size).cuda()
cell_state = torch.zeros(1, 1, self.hidden_size).cuda()
self.hidden = (hidden_state, cell_state)
def forward(self, X):
lstm_out, self.hidden = self.lstm(X, self.hidden)
out = lstm_out.contiguous().view(1, -1)
out = self.linear(out)
return out
def train_model(self, model, dataloader, num_epochs):
least_loss = 10
model.train()
if self.loss == 'BCE':
self.criterion = net.BCEWithLogitsLoss().cuda()
elif self.loss == 'MSE':
self.criterion = net.MSELoss().cuda()
else:
raise ValueError('PROVIDE A VALID LOSS FUNCTION')
optimizer = torch.optim.Adam(model.parameters())
training_loss = []
for i in range(num_epochs):
optimizer.zero_grad()
st = time.time()
epoch_loss = 0
for _, (x, y) in enumerate(dataloader):
model.init_Hidden()
output = model(x)
loss = self.criterion(output.view(-1), y)
loss.backward()
optimizer.step()
print(loss.item())
epoch_loss += loss.item()
et = time.time()
avg_loss = epoch_loss / 438
print('----------------------------------------------------------')
print('\033[1m' + 'TOTAL_TIME_PER_EPOCH = ' + str(et - st) + '\033[1m')
print('\033[1m' + 'AVERAGE_LOSS = ' + str(avg_loss) + '\033[1m')
training_loss.append(avg_loss)
if avg_loss<least_loss:
torch.save(model.state_dict(), 'path' + self.loss +'.pth')
least_loss = avg_loss
plt.plot(training_loss)
plt.xlabel('EPOCHS')
plt.ylabel('LOSS')
plt.show()
The model outputs something like -
tensor([[-0.0545]], device='cuda:0') CLASS 1
tensor([[-0.0541]], device='cuda:0') CLASS 2
tensor([[-0.0522]], device='cuda:0') CLASS 1
Interestingly, the sigmoid outputs are quite close, Can someone please tell me why ?
Thanks in Advance