Use sparse as input

This has been asked before, e.g. How to use sparse Tensor as input? and Error when using sparse tensor as input to module, but there are no answers yet.

The question is how to feed in a sparse tensor to the training.
My code is like

  longTensors = [[], []]
  floatTensors = []
  with open(filename) as fp:
    for line in fp:
      word = re.split(r'\t+', line)
      longTensors[0].extend([int(word[0]), int(word[1])])
      longTensors[1].extend([int(word[1]), int(word[0])])
      # value is tuple that (index, value)
      floatTensors.extend([float(word[2]),float(word[2])])
  i = torch.LongTensor(longTensors)
  v = torch.FloatTensor(floatTensors)
  # matrix size may need for torch.sparse.FloatTensor(i, v, torch.Size([n, n]))
  sparse_tensor = torch.sparse.FloatTensor(i, v)
  dataLoader = data.DataLoader(sparse_tensor)
  for epoch in range(5):
    for record in dataLoader:
       # do the training

But got the error that

for record in dataLoader:
  File "/opt/conda/envs/pytorch-py3.6/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 286, in __next__
    return self._process_next_batch(batch)
  File "/opt/conda/envs/pytorch-py3.6/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 307, in _process_next_batch
    raise batch.exc_type(batch.exc_msg)
RuntimeError: Traceback (most recent call last):
  File "/opt/conda/envs/pytorch-py3.6/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 57, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/opt/conda/envs/pytorch-py3.6/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 57, in <listcomp>
    samples = collate_fn([dataset[i] for i in batch_indices])
RuntimeError: Sparse tensors do not have strides.

Any suggestion on how to solve this issue?

Thanks

1 Like