LSTM Classifier is giving very low accuracy for training dataset but it has high training accuracy

Ali_Arsalan: <p>I am building an LSTM-based multi-class classifier with 17 input features and 13 output classes. When I train my network on training data it gives an accuracy of above 90% but when I test my trained model with the same data it gives an accuracy of 51%. I am new to pytorch, here is my code:</p>
<blockquote>
<p>X_train = df[[“Ia”,“Ib”,“Ic”,“Id”,“Iq”,“Mft_1”,“D_1”,“Mft_2”,“D_2”,“Mft_3”,“D_3”,“Mft_4”,“D_4”,“Mft_5”,“D_5”,“Mft_6”,“D_6”]].values<br>
y_train = df[[‘No_OCF’,‘OCF_1’,‘OCF_2’,‘OCF_3’,‘OCF_4’,‘OCF_5’,‘OCF_6’,‘OCF_14’,‘OCF_23’,‘OCF_36’,‘OCF_45’,‘OCF_16’,‘OCF_25’]].values<br>
s1= MinMaxScaler(feature_range=(0,1))<br>
X_train= s1.fit_transform(X_train)<br>
X_train = X_train.reshape((X_train.shape[0], 1, X_train.shape[1]))</p>
</blockquote>
<blockquote>
<p><span class="hashtag-raw">#Convert</span> data to tensors<br>
X_train = torch.Tensor(X_train)<br>
y_train = torch.tensor(y_train)<br>
<span class="hashtag-raw">#Create</span> DataLoader for training data<br>
train_dataset = TensorDataset(X_train, y_train)<br>
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=False)</p>
</blockquote>
<blockquote>
<p>seed = 42<br>
torch.manual_seed(seed)<br>
<span class="hashtag-raw">#Check</span> if CUDA (GPU) is available and set the seed for GPU<br>
if torch.cuda.is_available():<br>
torch.cuda.manual_seed(seed)</p>
</blockquote>
<blockquote>
<p><span class="hashtag-raw">#Define</span> LSTM model<br>
class LSTMModel(nn.Module):<br>
def <strong>init</strong>(self, input_size, hidden_size,  num_layers, output_size):<br>
super(LSTMModel, self).<strong>init</strong>()<br>
self.hidden_size = hidden_size<br>
self.num_layers = num_layers<br>
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)<br>
self.fc = nn.Linear(hidden_size, output_size)<br>
def forward(self, x):<br>
_, (hidden, _) = self.lstm(x)<br>
x= hidden[-1]<br>
x = self.fc(x)<br>
return x</p>
</blockquote>
<blockquote>
<p><span class="hashtag-raw">#Create</span> an instance of the LSTM model<br>
model = LSTMModel(input_size=17, hidden_size=64, num_layers=1, output_size=13)<br>
<span class="hashtag-raw">#Define</span> loss function and optimizer<br>
criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.Adam(model.parameters(), lr=1e-3)</p>
</blockquote>
<blockquote>
<h1>Train the model</h1>
<p>num_epochs = 5<br>
size = len(train_loader.dataset)<br>
for epoch in range(num_epochs):<br>
epoch_loss = 0.0<br>
correct = 0.0<br>
total_samples = 0.0<br>
model.train()<br>
for inputs, targets in train_loader:<br>
optimizer.zero_grad()<br>
targets = targets.argmax(dim=1)<br>
outputs = model(inputs)<br>
loss = criterion(outputs, targets)<br>
loss.backward()<br>
optimizer.step()<br>
epoch_loss += loss.item()<br>
# Compute accuracy<br>
total_samples += targets.size(0)<br>
correct += (outputs.argmax(1) == targets).sum().item()<br>
avg_loss = epoch_loss / len(train_loader)<br>
correct /= total_samples<br>
print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {avg_loss}, Accuracy: {(100*correct):&gt;0.1f}%")</p>
</blockquote>
<blockquote>
<h1>Testing on training dataset</h1>
<p>def evaluate(saved_model, dataloader):<br>
saved_model.eval()  # Set the model in evaluation mode</p>
</blockquote>
<blockquote>
<p>all_labels = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
all_predictions = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
with torch.no_grad():<br>
for inputs, labels in dataloader:<br>
# Forward pass<br>
outputs = saved_model(inputs)<br>
labels = labels.argmax(dim=1)<br>
# Predict class labels<br>
_, predictions = torch.max(outputs, 1)<br>
all_labels.extend(labels.cpu().numpy())<br>
all_predictions.extend(predictions.cpu().numpy())<br>
return all_labels, all_predictions</p>
</blockquote>
<blockquote>
<h1>Evaluate the model</h1>
<p>true_labels, predicted_labels = evaluate(saved_model, test_loader)</p>
</blockquote>
<h1><a name="calculate-accuracy-1" class="anchor" href="#calculate-accuracy-1"></a>Calculate accuracy</h1>
<blockquote>
<p>accuracy = accuracy_score(true_labels, predicted_labels)<br>
print(f’Accuracy: {accuracy:.4f}')</p>
</blockquote>
<p>I tried changing batch size and hidden size but it didn’t work.<br>
Kindly let me know if there is anything wrong with the code. Thanks.</p>