PyTorch - sparse tensors do not have strides
I am building my first sentiment analysis model for a small dataset of 1000 reviews using TF-IDF approach along with LSTM using the below code. I am preparing the train data by preprocessing it and feeding to the Vectorizer as below

def tfidf_features(X_train, X_val, X_test):
tfidf_vectorizer = TfidfVectorizer(analyzer='word', token_pattern = '(\S+)', min_df = 5, max_df = 
0.9, ngram_range=(1,2))
X_train=tfidf_vectorizer.fit_transform(X_train)
X_val=tfidf_vectorizer.transform(X_val)
X_test=tfidf_vectorizer.transform(X_test)

return X_train, X_val, X_test, tfidf_vectorizer.vocabulary_


I am converting my csr_matrix to a pytorch tensor using the below code

def spy_sparse2torch_sparse(data):
samples=data.shape[0]
features=data.shape[1]
values=data.data
coo_data=data.tocoo()
indices=torch.LongTensor([coo_data.row,coo_data.col])
t=torch.sparse.FloatTensor(indices,torch.from_numpy(values).float(),[samples,features])
return t


And I am getting the training sentences tensor as this

   tensor(indices=tensor([[  0,   0,   1,  ..., 599, 599, 599],
                   [ 97, 131,  49,  ..., 109,  65,  49]]),
   values=tensor([0.6759, 0.7370, 0.6076,  ..., 0.3288, 0.3927, 0.3288]),
   size=(600, 145), nnz=1607, layout=torch.sparse_coo)


I am creating a TensorDataSet using the below code wherein I am also converting my label data from bumpy to a torch tensor

train_data = TensorDataset(train_x, torch.from_numpy(train_y))


I have defined my LSTM network and calling it with the following parameters

n_vocab = len(vocabulary)
n_embed = 100
n_hidden = 256
n_output = 1   # 1 ("positive") or 0 ("negative")
n_layers = 2

net = Sentiment_Lstm(n_vocab, n_embed, n_hidden, n_output, n_layers)


I have also defined the loss and optimizer. Now I am training my model using the below code

print_every = 100
step = 0
n_epochs = 4  # validation loss increases from ~ epoch 3 or 4
clip = 5  # for gradient clip to prevent exploding gradient problem in LSTM/RNN

for epoch in range(n_epochs):
h = net.init_hidden(batch_size)

for inputs, labels in train_loader:
    step += 1

    # making requires_grad = False for the latest set of h
    h = tuple([each.data for each in h])   

    net.zero_grad()
    output, h = net(inputs)
    loss = criterion(output.squeeze(), labels.float())
    loss.backward()
    nn.utils.clip_grad_norm(net.parameters(), clip)
    optimizer.step()

    if (step % print_every) == 0:
        net.eval()
        valid_losses = []
        v_h = net.init_hidden(batch_size)

        for v_inputs, v_labels in valid_loader:
            v_inputs, v_labels = inputs.to(device), labels.to(device)

            v_h = tuple([each.data for each in v_h])

            v_output, v_h = net(v_inputs)
            v_loss = criterion(v_output.squeeze(), v_labels.float())
            valid_losses.append(v_loss.item())

        print("Epoch: {}/{}".format((epoch+1), n_epochs),
              "Step: {}".format(step),
              "Training Loss: {:.4f}".format(loss.item()),
              "Validation Loss: {:.4f}".format(np.mean(valid_losses)))
        net.train()


However, I am getting a major error on the line output, h = net(inputs) as RuntimeError: sparse tensors do not have strides

The workarounds given on other websites are not understandable. I am expecting an exact code change I need to make in order to fix this issue. 

Pytorch does not support sparse (S) to sparse matrix multiplication.
Let us consider :
torch.sparse.mm(c1,c2), where c1 and c2 are sparse_coo_tensor matrices.
case1: If we try c1 and c2 to be S  --&gt; It gives the erros RuntimeError: sparse tensors do not have strides.
case2: If c1 is dense (D) and c2 is S --&gt; It gives the same error.
case3: Only when c1 is S and c2 is D --&gt; It works fine.
Reference: https://blog.csdn.net/w55100/article/details/109086131
I guess the matrix multiplication happening in your Sentiment_Lstm might be falling under the first two cases. And thereby throwing this error.
By using dense input format it should work.
