Lstm losss does not decrease after 3rd epoch

Aimen_Malik: <p>import torch<br>
import torch.nn as nn<br>
import torchvision<br>
import torch.optim as opt<br>
from torch.utils.data import DataLoader<br>
import matplotlib.pyplot as plt<br>
from TmyDataSet import LMDBDataSet<br>
import lmdb<br>
from tqdm import tqdm<br>
from time import sleep</p>
<p>support_num = 21<br>
start_num = 40<br>
train_len = 5<br>
pre_len = 4</p>
<p>class RNN(nn.Module):<br>
def <strong>init</strong>(self, train_len, n_hidden, pre_len=4):  # hidden size<br>
super(RNN, self).<strong>init</strong>()<br>
self.n_hidden = n_hidden<br>
self.lstm1 = nn.LSTM(5, self.n_hidden, 1)<br>
self.linear2 = nn.Linear(self.n_hidden, pre_len)<br>
self.pre_len = pre_len<br>
self.train_len = train_len</p>
<pre><code>def forward(self, x_train):
    h, c = self.lstm1(x_train)
    out = h.view(-1, self.n_hidden)
    output = self.linear2(out)
    return output
</code></pre>
<p>class LMDBDataSet(Dataset):<br>
def <strong>init</strong>(self, sequence_length, start_num, support_num, pre_step, max_pressure=40, min_time=34992, max_time=39052):<br>
self.db_path = ‘/home/aimen/PycharmProjects/HydraulicSupport_pressure/’<br>
self.startSupportNum = start_num<br>
self.supportNum = support_num<br>
self.train_length = sequence_length<br>
self.pre_step = pre_step<br>
self.max_pressure = max_pressure<br>
self.min_time = min_time<br>
self.max_time = max_time<br>
self.length = int(self.max_time) - int(self.min_time) - self.pre_step - self.train_length - 1</p>
<pre><code>def __getitem__(self, index):
    data = []
    label = []

    if (index + self.pre_step + self.train_length) &lt; self.length:
        for id in range(self.startSupportNum, self.startSupportNum + self.supportNum):  ## append the data for support id
            env = lmdb.open(self.db_path + "support_id_lmdb"+str(id))
            txn = env.begin(write=False)
            temp = []
            for sample_time in range(index + int(self.min_time),
                                     index + int(self.min_time) + int(self.train_length)): ## append the data for time
                data_value = txn.get(str(sample_time).encode())
                temp.append(np.float32(float(data_value) / self.max_pressure))
            data.append(temp)

            temp = []
            for label_time in range(index + int(self.min_time) + int(self.train_length),
                                    index + int(self.min_time) + int(self.train_length) + int(self.pre_step)):
                label_value = txn.get(str(label_time).encode())
                temp.append(np.float32(float(label_value) / self.max_pressure))
            label.append(temp)

    data = np.array(data)
    label = np.array(label)
    return data, label

def __len__(self):
    return 4000
</code></pre>
<p>dataset = LMDBDataSet(sequence_length=train_len, start_num=start_num, support_num=support_num, pre_step=pre_len)</p>
<p>dataloader_train = DataLoader(dataset, batch_size=1, shuffle=False, num_workers=1)<br>
dataloader_test = DataLoader(dataset, batch_size=1, shuffle=False, num_workers=1)</p>
<p>model = RNN(train_len=5, n_hidden=16)<br>
optimizer = opt.Adam(model.parameters(), lr=0.001, weight_decay=0.001)<br>
scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, 0.999)<br>
LossFunc = nn.MSELoss()</p>
<p>state = {‘model’: model.state_dict(), ‘optimizer’: optimizer.state_dict()}</p>
<p>model.train()<br>
epoch_num = 100<br>
for epoch in range(epoch_num):<br>
loss = 0</p>
<pre><code>train_bar = tqdm(dataloader_train)
for i, data in enumerate(train_bar):
    x_train, y_train = data
    sleep(0.001)
    y_pre = model(x_train)

    y_pre = y_pre.reshape(-1, y_pre.shape[0])
    Y_train = y_train.reshape(y_train.shape[-1], -1)

    loss = LossFunc(Y_train, y_pre)

    y_pre = y_pre.detach().numpy()
    Y_train = Y_train.detach().numpy()

    optimizer.zero_grad()
    loss.backward()

    optimizer.step()
    scheduler.step()
    loss += loss
    train_bar.desc = "train epoch[{}/{}] " \
                         "loss:{:.3f}".format(epoch + 1, epoch_num, loss)

print("------", loss / len(dataloader_train))
</code></pre>

ptrblck: <p>Double check the shapes of the output activation in:</p>
<pre><code class="lang-python">self.lstm1 = nn.LSTM(5, self.n_hidden, 1)
...

def forward(self, x_train):
    h, c = self.lstm1(x_train)
    out = h.view(-1, self.n_hidden)
</code></pre>
<p>as <code>nn.LSTM</code> uses <code>batch_size=False</code> by default. The output will should thus have the shape <code>[seq_len, batch_size, output_size]</code>. Flattening the <code>h</code> tensor might thus be wrong.</p>

Aimen_Malik: <p>thank you sir! it worked.</p>