Hi
I use  torch.sparse for categorical data, but this class is too slow.
%%time
import torch

class SparseArray:
    def __init__(self):
        self.clear()
        
    def clear(self):
        self.array = torch.sparse.FloatTensor(1,0)
        
    def addFloat(self, v):        
        tmp = torch.FloatTensor([[v]]).to_sparse()
        self.array = torch.cat([self.array,tmp], dim=1)

    def addCategorical(self, idx, categorySize):
        #assert (idx < categorySize)
        #assert (0 <= idx)
        i = torch.LongTensor([[0],[idx]])
        v = torch.FloatTensor([1])
        tmp = torch.sparse.FloatTensor(i, v, torch.Size([1, categorySize]))
        self.array = torch.cat([self.array,tmp],dim=1)

sa = SparseArray()
for i in range(1024*16):
    sa.clear()
    sa.addFloat(5.0)
    sa.addCategorical(i%100,100)

This  snippet outputs “Wall time: 2.54 s”.
Why is it so slow?
Thanks.