@vdw  :Hi Chris , Thanks for your valuable inputs. I have understood your explanation to to part of using final hidden state instead of using all the hidden states (Output).
As last few time steps will have more of padding vectors. Hence you have suggested to take the final hidden state rather than taking hidden state for each time step. Hope my understanding is correct?
Yes, I have taken care of the view part. I am using view only in the final step , to reduce the tensor size from ( batch_size x 1(no.of layers) x hidden size) → (batchsize, no.of layers * hidden size). Could I have used squeeze instead of view to reduce the dimension of the tensor?
As suggested , i have simplified the model. Emebedding + Padding + LSTM  —>hidden[-1] to  1 Linear layer.
I tried applying a non linear activation ( ReLU) after computing 1 Liear Layer or tried applying it on the hidden layer of LSTM. But doesnt seem to have any effect.
I would like to one more thing, in my current training function , I am intializing  the hidden layers after running every batch. I felt I was wrong in intializing the hidden parameters each time. But when i try to inplace the learnt hidden parameters each time  , colab is crashing after 4 or 5 epoch due to high memory usage.
Could you kindly give your inputs on this regard too?
Below is the forward class, yoy may find many unused arguments. Kindly excuse. Please do give your comments
class SentiClassify_Model(nn.Module):
def init(self,vocabLen,dims,hidden_size,seqLengths,batchSize,output_size=2):
super().__init__()

#output_size =  2

self.hidden_size = hidden_size

self.batchSize = batchSize

self.seqLengths = seqLengths

self.embed = nn.Embedding(vocabLen,dims)

self.lstm_cell = nn.LSTM(input_size=dims,hidden_size=hidden_size)

self.lf = nn.Linear(self.hidden_size,output_size)

self.F = nn.ReLU(inplace=False)

def forward(self,input,hidden=None,verbose=False):
embeds = self.embed(input)

packedSeq = pack_padded_sequence(embeds.permute(1,0,2),self.seqLengths,batch_first=True,enforce_sorted=False)

output,hidden = self.lstm_cell(packedSeq,hidden)

linear = self.lf(hidden[-1].permute(1,0,2)).view(self.batchSize,-1)

print(linear.size())

return linear

if verbose:

  print("input shape",input.shape)

  print('embed shape',embeds.shape)

  print("Rehaped output",reshaped_out.size())

  print("After Fully conn layer :",lin_output.size())

def init_hiddenlayer(self,hiddenLayers=1,batch_size=None,device=‘cpu’):
return (torch.zeros(1*hiddenLayers,batch_size,self.hidden_size,device=device),torch.zeros(1*hiddenLayers,batch_size,self.hidden_size,device=device))