I don't know why my model that configure simple rnn with integer dataset doesn't working

I’m studying RNN and pytorch.
I just want to predict integer sequence.
This picture shows what i want to do and my idea.
please help me.
I think my concept is wrong.
I want to fix it.

class myRNN(nn.Module):
    def __init__(self, input_size, hidden_size, num_layer):
        super(myRNN, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.num_layer = num_layer

        self.rnn1 = nn.RNN(input_size, hidden_size, num_layer)
        #input_size, hidden_size, num_layer
        #hidden_size 는 한 layer내의 block size

    def forward(self, input, hidden):
        output, hidden = self.rnn1(input, hidden)
        return output, hidden

    def init_hidden(self):
        hidden = Variable(torch.zeros(num_layer, batch_size, hidden_size))
        return hidden



if __name__ == '__main__':

    seq_len = 10
    input_size = 1
    hidden_size = 10
    num_layer = 1
    batch_size = 1

    #
    rnn = myRNN(input_size, hidden_size, num_layer)
    #loss, optimizer
    optimizer = torch.optim.Adam(rnn.parameters(), lr = 0.0001)

    example = [1,2,3,4,5,6,7,8,9,10,11]

    input_data = Variable(torch.FloatTensor(example[:-1]).view(seq_len, batch_size,  input_size))
    label = Variable(torch.FloatTensor(example[1:]).view(seq_len, batch_size, input_size))
    print(input_data.size(), label.size())



    hidden = rnn.init_hidden()
    print(hidden.size())


    hidden = rnn.init_hidden()
    loss = 0
    optimizer.zero_grad()
    output, hidden = rnn(input_data, hidden)

    print('output / label' , output.size(), label.size())
    loss = nn.functional.nll_loss(output, label)
    loss.backward()
    optimizer.step()
    print(loss)

(** PS **) After I implement the RNN with another code(not the above code), i got a output with float not integer.
-0.9967 -0.4590 0.9998 -0.9367 0.5538 -1.0000 1.0000 -1.0000 1.0000

example = [1,2,3,4,5,6,7,8,9,10,11] should be smaller than num_hidden (i.e. 9 is the most)
example = [1,2,3,1,5,4,7,2,7,9,0]

also there is something wrong with data size.

BTW It would be better to paste a snippet that could work directly in someone other’s environ.

one version that workd for you:

import torch
from torch import nn
from torch.autograd import Variable

class myRNN(nn.Module):
    def __init__(self, input_size, hidden_size, num_layer):
        super(myRNN, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.num_layer = num_layer

        self.rnn1 = nn.RNN(input_size, hidden_size, num_layer)
        #input_size, hidden_size, num_layer
        #hidden_size 는 한 layer내의 block size

    def forward(self, input, hidden):
        output, hidden = self.rnn1(input, hidden)
        return output, hidden

    def init_hidden(self):
        hidden = Variable(torch.zeros(num_layer, batch_size, hidden_size))
        return hidden



if __name__ == '__main__':

    
    input_size = 1
    hidden_size = 10
    num_layer = 1
    batch_size = 1

    #
    rnn = myRNN(input_size, hidden_size, num_layer)
    #loss, optimizer
    optimizer = torch.optim.Adam(rnn.parameters(), lr = 0.0001)

    example = [1,2,3,1,3,5,4,7,2,2,7,2,9,0]
    seq_len = len(example)-1
    # change hre
    input_data = Variable(torch.FloatTensor(example[:-1]).view(-1, batch_size,  input_size))
    label = Variable(torch.FloatTensor(example[1:]).view(-1, batch_size, input_size))
    print(input_data.size(), label.size())



    hidden = rnn.init_hidden()
    print(hidden.size())


    hidden = rnn.init_hidden()
    loss = 0
    optimizer.zero_grad()
    output, hidden = rnn(input_data, hidden)

    print('output / label' , output.size(), label.size())
     
    #modify here
    output = output.view(-1,hidden_size)
    label = label.long().view(-1)
    loss = nn.functional.nll_loss(output, label)
    loss.backward()
    optimizer.step()
    print(loss)


1 Like

Oh Thank you! This works fine :slight_smile: many thanks!
I have more questions.
This script generate 13x10 output reply.
I think the RNN have to generate integer like [2, 3, 1, 3, 5, 4, 7, 2, 2, 7, 2, 9, 0] 1 x 13 data.
How can I get Integer prediction value?

I want to make a structure like the one shown above, but now it seems to be the following structure. Is it right for me to think?

It is the structure you want.

predict = output.data.max(1)[1]

1 Like

Thank you for your help :slight_smile: