简体   繁体   中英

How to convert a PyTorch sparse_coo_tensor into a PyTorch dense tensor?

I create a sparse_coo tensor in PyTorch:

import torch

# create indices
i = torch.tensor([[0, 1, 1],
                  [2, 0, 2]])
# create values
v = torch.tensor([3, 4, 5], dtype=torch.float32)

# create sparse_coo_tensor
sparse_tensor = torch.sparse_coo_tensor(i, v, [2, 4])

Now I want to convert a PyTorch sparse tensor into a PyTorch dense tensor. Which function can be used?

you can use to_dense as suggested in this example :

s = torch.sparse_coo_tensor(i, v, [2, 4])
s_dense = s.to_dense()

And by the way, the documentation is here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM