I get it, thanks. I’m using some kind of “sliding windows” however, when using this sliding window, the last element of the (i,j) in loader does not have the same dimensions as the previous ones. I mean, when idx+seqlength (seqlength is like sliding window in my example) is > than self.__len__ then the last data returned by get item has just idx elements. I mean, imagine sequence length is 2, then if when creating the dataset:

input size = 10
batch_size = 2
sequence_length = 2
nº of samples = 6
then in the data, target in loader would return:
data.shape = [2,2,10] target shape [2,2] for the first iter
data.shape = [2,2,10] target shape [2,2] for the second iter
and for the third iter then it will return an error like:
RuntimeError: stack expects each tensor to be equal size, but got [2, 10] at entry 0 and [1, 10] at entry 1
because the last sequence length condition is not satisfied. How could I solve that? My data are vectors with values 0 and target values 0 as well so I don’t think padding with zeros would be something reliable because when training the dataset it would be mistaking the padded sequences with some target and data. Below is the code I’m using (it’s a test code because my dataset is much larger so I rather prefer trying with something smaller first). Thanks!

class DataPrueba(Dataset):
    def __init__(self, data, seq, labels=None, transforms=None):
        self.X = data
        self.y = labels
        self.seq = seq
        self.transforms = transforms

    def __len__(self):
        return len(np.asarray(self.X))

    def __getitem__(self, i):
        if self.y is not None:
            if i + self.seq > self.__len__():
                if self.transforms is not None:
                    item = []
                    item[:self.__len__()-i] = self.transforms(self.X[i:])
                    target_item = []
                    target_item[:self.__len__()-i] = self.transforms(self.y[i:])
                    data = item
                    labels = target_item
                else:
                    data = torch.zeros_like(self.X[i:])
                    data[:self.__len__()-i] = self.X[i:]
                    labels = torch.zeros_like(self.y[i:])
                    labels[:self.__len__()-i] = self.y[i:]
            else:
                if self.transforms is not None:
                    data = self.transforms(self.X[i:i+self.seq])
                    labels = self.transforms(self.y[i:i+self.seq])
                else:
                    data = self.X[i:i+self.seq]
                    labels = self.y[i:i+self.seq]
            return torch.FloatTensor(data), labels
        else:
            if i + self.seq > self.__len__():
                if self.transforms is not None:
                    item = []
                    item[:self.__len__()-i] = self.transforms(self.X[i:])
                    data = item
                else:
                    item = []
                    item[:self.__len__()-i] = self.X[i:]
                    data = item
            else:
                if self.transforms is not None:
                    data = self.transforms(self.X[i:i+self.seq])
                else:
                    data = self.X[i:i+self.seq]
            return torch.FloatTensor(data)
        
        
x = torch.randn(6, 10)
labels = torch.randint(0, 6, (6,))
num_classes = 6;
hidden_size = 35
batch_size = 2
seq = 2
num_layers=1

transform = transforms.Lambda(lambda x: listToTensor(x))
dataset = DataPrueba(x, seq=seq, labels=labels, transforms=None)       
loader = DataLoader(dataset, batch_size=batch_size, drop_last=True)
in_size = x.shape[1]

for idx, (i, j) in enumerate(loader):
    print(idx)
    print(str(i)+'\t'+str(i.shape))
    print(str(j)+'\t'+str(j.shape))
    
# Fully connected neural network with one hidden layer
class RNN(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, num_classes):
        super(RNN, self).__init__()
        self.num_layers = num_layers
        self.hidden_size = hidden_size
        self.rnn = nn.RNN(input_size, hidden_size, num_layers, batch_first=True)
        self.fc = nn.Linear(hidden_size, num_classes)
        
    def forward(self, x):
        # Set initial hidden states (and cell states for RNN/LSTM)
        h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size)
        # Forward propagate RNN
        out, _ = self.rnn(x, h0)  
        out = self.fc(out)
        return out

model = RNN(in_size, hidden_size, num_layers, num_classes)
criterion = nn.CrossEntropyLoss()

for epoch in range(10):
    for i, (images, labels) in enumerate(loader):  
        # origin shape: [N, 1, 28, 28]
        # resized: [N, 28, 28]
        images = images.reshape(-1, seq, in_size)
        print(images.shape)
        # Forward pass
        outputs = model(images)
        loss = criterion(outputs.permute(0,2,1), labels)