i can't get good result from LSTM model

username: www.253274151
cooked: <p>HELLO guys, i really need some help</p>
<p>i try to use lstm model to train some audio data(they are 3-d tensors)  to classify the man with audio if happy or not. i have try to make timesteps from 10000 to 20 since  it’s proper for lstm.</p>
<p>but when i use lr from 0.0001-0.001, the loss may become down at first but  up continuously.<br>
with lower lr, it seems to work but later bad again.</p>
<p>this is my code. Could anyone give me some suggestions. thanks a lot.</p>
<p>MODEL:</p>
<p><span class="hashtag-raw">#neural</span> network<br>
from torch import nn</p>
<p>class neural_network(nn.Module):<br>
def <strong>init</strong>(self, input_size, hidden_dim, output_size, num_layers, dropout=0.5):<br>
super(neural_network,self).<strong>init</strong>()</p>
<pre><code>    self.hidden_dim  = hidden_dim
    self.output_size = output_size
    self.num_layers  = num_layers
    self.lstm        = nn.LSTM(input_size, hidden_dim, num_layers, 
                               dropout=dropout, batch_first=True,
                               bidirectional=True,
                              )
    '''
    self.lstm = nn.LSTM(input_size = 79,
                        hidden_size = 128,
                        num_layers = 3,
                        batch_first = False,
                        bidirectional =True,
                        dropout = 0.2)
    '''
    self.fc1 = nn.Linear(hidden_dim*2, output_size)
    self.softmax = torch.nn.Softmax(dim=1)

def forward(self,x,hidden):
    output,hidden = self.lstm(x,hidden)
    output = output[:,-1,:]
    # hidden = torch.cat([hidden[-2], hidden[-1]], dim = 1)
    # hidden = self.dropout(hidden)
    #print(output.shape)
    #print(output.shape)
    output = self.fc1(torch.relu(output))
    #output = self.fc1(hidden)
    #output =  self.softmax(output)
    return output,hidden



def init_hidden(self, batch_size):
    weight = next(self.parameters()).data
    # print("weghit :", weight.shape)
    hidden = (weight.new(self.num_layers*2, batch_size, self.hidden_dim).zero_().to(device),
            weight.new(self.num_layers*2, batch_size, self.hidden_dim).zero_().to(device))
    return hidden
</code></pre>
<blockquote>
<p>Blockquote</p>
</blockquote>
<h1><a name="some-parameters-1" class="anchor" href="#some-parameters-1"></a>some parameters</h1>
<p>input_size  = 79<br>
output_size = 2<br>
hidden_dim  = 128<br>
num_layers  = 3<br>
dropout     = 0.2<br>
batch_size  = 128</p>
<h1><a name="h-2" class="anchor" href="#h-2"></a></h1>
<p>criterion_audio = torch.nn.CrossEntropyLoss() #<br>
optimizer_audio = torch.optim.Adam(model_audio.parameters(),lr=0.00001)<br>
<span class="hashtag-raw">#optimizer_audio</span> = torch.optim.SGD(model_audio.parameters(), lr=1e-6, momentum=0.8)<br>
num_epochs = 300</p>
<p>TRAIN:</p>
<h1><a name="h-3" class="anchor" href="#h-3"></a>定义训练模型</h1>
<p>def train(model, device, train_loader, criterion, optimizer, num_epochs,batch_size):<br>
history = list()<br>
<span class="hashtag-raw">#f1_scores</span> = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
<span class="hashtag-raw">#recall_scores</span> = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
<span class="hashtag-raw">#precision_scores</span> = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
for epoch in range(num_epochs):<br>
t1 = time.time()<br>
hs = model.init_hidden(batch_size)<br>
train_loss = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
accuracy_scores  = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
# train_correct = 0.0<br>
model.train()<br>
# accuracy_score_temp = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
for data, target in train_loader:<br>
data   = data.to(device)<br>
target = target.to(device)<br>
output,hs = model(data, hs)<br>
hs = tuple([h.data for h in hs])<br>
<span class="hashtag-raw">#output</span> = output[:,-1,:]<br>
loss = criterion(output, target.long())<br>
train_loss.append(loss.item()) # 累计损失<br>
loss.backward() # 反向传播<br>
optimizer.step() # 参数更新<br>
<span class="hashtag-raw">#print</span>(output.shape)<br>
preds = output.argmax(dim=1) # 找出概率最大的类<br>
<span class="hashtag-raw">#print</span>(preds)<br>
accuracy_scores.append(accuracy_score(target.cpu().data, preds.cpu().data))</p>
<pre><code>        '''
        while i&lt;data.shape[1]:
            # print(data[:,i:i+200,:].shape)
            output, hs = model(data[:,i:i+200,:], hs) # 模型训练
            output = output[:,-1,:]
            hs = tuple([h.data for h in hs])
            # hs     = tuple([h.data for h in hs])
            loss   = criterion(output, target.long()) # 计算损失
            train_loss_temp.append(loss.item()) # 累计损失
            pred = torch.tensor([output.argmax()]) # 找出概率最大的类
            # 计算准确率
            print(output)
            accuracy_score_temp.append(accuracy_score(target.cpu().data, pred.cpu().data))
            loss.backward() # 反向传播
            optimizer.step() # 参数更新
            i += 200
            # train_correct += torch.sum(output==target) # 比较
            t3 = time.time()
        print(f'第{count}个样本 准确率{np.mean(accuracy_score_temp)} \
                用时{t3-t2}s  \
                误差{np.mean(train_loss_temp)} 标签{target.item()}')
        train_loss.append(np.mean(train_loss_temp))
        accuracy_scores.append(np.mean(accuracy_score_temp))
        '''
    print(f'Epoch {epoch}/{num_epochs} --- train loss {np.round(np.mean(train_loss), 5)}')
    print(f'---  准确率{np.mean(accuracy_scores)}')
    print(f'--- time_consume {np.round((time.time() - t1), 5)}s ')
</code></pre>