Issue training RNN model with pytorch with trivial goal
I'm trying to train a simple RNN model with a trivial goal where the output matches a fixed vector regardless of the input

import torch
import torch.nn as nn

from torch.autograd import Variable
import numpy as np

class RNN(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(RNN, self).__init__()
        self.hidden_size = hidden_size
        self.i2h = nn.Linear(input_size + hidden_size, hidden_size)
        print "i2h WEIGHT size ", list(self.i2h.weight.size())
        print "i2h bias size ", list(self.i2h.bias.size())
        self.i2o = nn.Linear(hidden_size, output_size)
        print "i2o WEIGHT size ", list(self.i2o.weight.size())
        print "i2o bias size ", list(self.i2o.bias.size())
        self.softmax = nn.LogSoftmax(dim=1)

    def forward(self, input, hidden):
        combined = torch.cat((input, hidden), 1)
        hidden = self.i2h(combined)
        output = self.i2o(hidden)
        output = self.softmax(output)
        return output, hidden

    def initHidden(self):
        return Variable(torch.zeros(1, self.hidden_size))

n_hidden = 20
rnn = RNN(10, n_hidden, 3)


learning_rate = 1e-3
loss_fn = torch.nn.MSELoss(size_average=False)
out_target = Variable( torch.FloatTensor([[0.0 , 1.0, 0.0]] ) , requires_grad=False)

print "target output::: ", out_target
def train(category_tensor, line_tensor):
    hidden = rnn.initHidden()

    rnn.zero_grad()

    for i in range(line_tensor.size()[0]):
        #print "train iteration ", i, ": input data: ", line_tensor[i]
        output, hidden = rnn(line_tensor[i], hidden)


    loss = loss_fn(output, out_target)
    loss.backward()

    # Add parameters' gradients to their values, multiplied by learning rate
    for p in rnn.parameters():
        #print "parameter: ", p, " gradient: ", p.grad.data
        p.data.add_(-learning_rate, p.grad.data)

    return output, loss.data[0]

current_loss = 0
n_iters = 500

for iter in range(1, n_iters + 1):
    inp = Variable(torch.randn(100,1,10) + 5)
    output, loss = train(out_target, inp)
    current_loss += loss
    if iter % 1 == 0:
      print "weights: ",rnn.i2h.weight
      print "LOSS: ", loss
      print output


As it shows, the loss stays above 6 and never goes down. Notice also that I am biasing all the random inputs normal distributions by 5, so they are mostly positive numbers, so there should exist a weight distribution that approaches the goal output

What am I doing wrong in this example that is failing to output to approach the goal?

Your fixed output is:

torch.FloatTensor([[0.0, 1.0, 0.0]])


But you are using the following as the final layer in your RNN:

self.softmax = nn.LogSoftmax(dim=1)


Does LogSoftmax returns value in [0, 1]? Althouhgh, you can use the Softmax but I would recommend you to use the sign function and transform -1 to 0.
