Training loss decreased very small amount.from 9.18 to 8.97

Hello,

I am a begginer of pytorch programming. And I am trying to write RNN codes using pytorch. I want to reach training loss less than 3.5. But my loss is near 9. Would you help me to solve this problem. I attach my code below. I appreciate any help.

def forward_back_prop(rnn, optimizer, criterion, inp, target, hidden):
“”"
Forward and backward propagation on the neural network
:param decoder: The PyTorch Module that holds the neural network
:param decoder_optimizer: The PyTorch optimizer for the neural network
:param criterion: The PyTorch loss function
:param inp: A batch of input to the neural network
:param target: The target output for the batch of input
:return: The loss and the latest hidden state Tensor
“”"
train_loss = 0.0
step = 0
print_every = 40

for i in range(print_every):
    step += 1
    if train_on_gpu:
        inp, target = inp.cuda(), target.cuda()
    
    hidden = tuple([each.data for each in hidden])
    
    rnn.zero_grad() 
    
    output, hidden = rnn(inp, hidden)
      
    loss = criterion(output, target)

    loss.backward()
    
    nn.utils.clip_grad_norm_(rnn.parameters(), 5)

    optimizer.step()

    train_loss += loss.item()
    
    if step == print_every:   
        return train_loss/print_every, hidden

def train_rnn(rnn, batch_size, optimizer, criterion, n_epochs, show_every_n_batches=1):
batch_losses = []

rnn.train()

print("Training for %d epoch(s)..." % n_epochs)
for epoch_i in range(1, n_epochs + 1):
    
    # initialize hidden state
    hidden = rnn.init_hidden(batch_size)
    
    for batch_i, (inputs, labels) in enumerate(train_loader, 1):
        
        
        # make sure you iterate over completely full batches, only
        n_batches = len(train_loader.dataset)//batch_size
        
        
        if(batch_i > n_batches):
            break
        
        # forward, back prop
        loss, hidden = forward_back_prop(rnn, optimizer, criterion, inputs, labels, hidden)
        # record loss
        batch_losses.append(loss)
        
        
        # printing loss stats
        if batch_i % show_every_n_batches == 0:
            print('Epoch: {:>4}/{:<4}  Loss: {}\n'.format(
                epoch_i, n_epochs, np.average(batch_losses)))
            batch_losses = []

# returns a trained rnn
return rnn

I also attach my results.

Data params

Sequence Length

sequence_length = 20 # of words in a sequence

Batch Size

batch_size = 128

data loader - do not change

train_loader = batch_data(int_text, sequence_length, batch_size)

Training parameters

Number of Epochs

num_epochs = 20

Learning Rate

learning_rate = 0.001

Model parameters

Vocab size

vocab_size = len(vocab_to_int)

Output size

output_size = vocab_size

Embedding Dimension

embedding_dim = 200

Hidden Dimension

hidden_dim = 400

Number of RNN Layers

n_layers = 2

Show stats for every n number of batches

show_every_n_batches = 1

create model and move to gpu if available

rnn = RNN(vocab_size, output_size, embedding_dim, hidden_dim, n_layers, dropout=0.5)
if train_on_gpu:
rnn.cuda()

defining loss and optimization functions for training

optimizer = torch.optim.Adam(rnn.parameters(), lr=learning_rate)
criterion = nn.CrossEntropyLoss()

training the model

trained_rnn = train_rnn(rnn, batch_size, optimizer, criterion, num_epochs, show_every_n_batches)

saving the trained model

helper.save_model(’./save/trained_rnn’, trained_rnn)
print(‘Model Trained and Saved’)

Training for 20 epoch(s)…
Epoch: 1/20 Loss: 9.181344604492187

Epoch: 2/20 Loss: 8.977545619010925

Epoch: 3/20 Loss: 8.97732071876526

Epoch: 4/20 Loss: 8.97721266746521

Epoch: 5/20 Loss: 8.977132678031921

Epoch: 6/20 Loss: 8.977069520950318

Epoch: 7/20 Loss: 8.97701997756958

Epoch: 8/20 Loss: 8.976981210708619

Epoch: 9/20 Loss: 8.97695345878601

Epoch: 10/20 Loss: 8.976929020881652

Epoch: 11/20 Loss: 8.976907062530518

Epoch: 12/20 Loss: 8.976887893676757

Epoch: 13/20 Loss: 8.976875948905946

Epoch: 14/20 Loss: 8.976863694190978

Epoch: 15/20 Loss: 8.976851677894592

Epoch: 16/20 Loss: 8.97684371471405

Epoch: 17/20 Loss: 8.97683494091034

Epoch: 18/20 Loss: 8.976829504966735

Epoch: 19/20 Loss: 8.976822710037231

Epoch: 20/20 Loss: 8.976818060874939

The loss does look to be decreasing. How about you try increasing the learning rate?

Thank you for your response. I changed learning rate from 0.001 to 0.05. But, the training loss was not changed, constantly near 9.