hello to everyone, this is the first time I’m dealing with RNN and LSTM but I have to do a project that gives 500 voice recordings (each with a sum of 2 numbers summing up: example “2 + 5”) I have to create a model to learn these voice recordings.
Is there someone who can help me???
THIS IS THE CODE:
# Define options
class Options():
    pass
opt = Options()
# Dataset options
opt.frac_train = 0.8
# Training options
opt.learning_rate = 0.001
opt.epochs = 1
# Model options
opt.hidden = 512
# Backend options
opt.cuda = False
# optimization function
opt.optim_function = "SGD"
# Momentum
opt.momentum = 0
# incremental lr
opt.incremental = False

# Imports
import math
import glob
import string
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable

#all_char="0123456789";
#dataa=[]

#datatrainingprova = matrix[:8]
#datatestprova = matrix[8:10]

# Shuffle data
shuffled_idx = torch.randperm(len(matrix))
# Num samples
num_samples = len(matrix)
num_train = math.ceil(num_samples*opt.frac_train)
num_test = num_samples - num_train

# Separate splits
train_idx = shuffled_idx[0:num_train]
test_idx = shuffled_idx[num_train:]

#len(train_idx) #= 400
#len(test_idx) #= 100

# --------------------------------

class RNN(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(RNN, self).__init__()
        # Model layout
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.output_size = output_size
        
        self.lstm1 = nn.LSTM(input_size, hidden_size)
        self.output = nn.Linear(hidden_size, output_size)
        
    def forward(self, input): 
        output = Variable(torch.zeros(1, self.output_size));
        c = torch.zeros(1, self.hidden_size); 
        h = torch.zeros(1,self.hidden_size);
        if opt.cuda:
           c = c.cuda()
           h = h.cuda()
        output= Variable(c);
        c_t = Variable(c);
        h_t = Variable(h);
        
        #h_t2 = Variable(torch.zeros(1, self.hidden_size) );
        #c_t2 = Variable(torch.zeros(1, self.hidden_size) );
        #for i in range(0, input.size(0)):
        h_t, c_t = self.lstm1(input, (h_t, c_t))
        #h_t2, c_t2 = self.lstm2(c_t, (h_t2, c_t2))
        output= self.output(c_t)
        return output[-1]
    
    #def init_hidden(self):
        #return torch.zeros(1, self.hidden_size)

rnn = RNN(1, opt.hidden, 20)

# ----------

#Setup loss criterion 
criterion = nn.MSELoss(size_average=False)
if opt.cuda: rnn.cuda()

optimizer=torch.optim.SGD(rnn.parameters(), lr = opt.learning_rate)

# ------------

num_train_t = num_train
num_test_t = num_test

# ------------

rnn = nn.LSTM(1, 20, 1, batch_first=True)
input = matrix[train_idx[i]]
input_size = input.size(0)
input = input.contiguous().view(1, input_size, 1)
input = Variable(input)

c = torch.zeros(1, 1, 20); 
h = torch.zeros(1, 1, 20);
c_t = Variable(c);
h_t = Variable(h);
h_t, c_t = rnn(input, (h_t, c_t))

# -----------------------------

# Start training
for epoch in range(1, opt.epochs+1):
    # Training mode
    rnn.train()
    # Shuffle training data indexes
    train_idx = train_idx.index(torch.randperm(num_train))
    # Initialize training loss and accuracy
    train_loss = 0
    train_accuracy = 0
    
    # Process all training samples
    for i in range(0, num_train):
        # Prepare sample
        input = matrix[train_idx[i]]
        #input = input.contiguous().view(-1, input.size(-1))
        target = torch.LongTensor([labels[train_idx[i]]])
        # Initialize hidden state
        #hidden = rnn.init_hidden()
        # Check cuda
        if opt.cuda:
            input = input.cuda()
            hidden = hidden.cuda()
            target = target.cuda()
        # Wrap for autograd
        input = Variable(input)
        #hidden = Variable(hidden)
        target = Variable(target)
        #for j in range(0, input.size(0)):
        # Forward
        output, hidden = rnn(input)             # **_<------ here I am mistaken_**
        # Compute loss
        loss = criterion(output, target)
        train_loss += loss.data[0]
        # Compute accuracy
        _,pred = output.data.max(1)
        correct = pred.eq(target.data).sum()
        train_accuracy += correct
        # Backward and optimize
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    # Evaluation mode
    rnn.eval()

Error generated: dimension out of range (expected to be in range of [-1, 0], but got 1)