简体   繁体   中英

coo_matrix TypeError: data type not understood

Steps

import torch
import numpy as np
from scipy.sparse import coo_matrix

row = torch.ones([10000], dtype=torch.float32)
col = torch.ones([10000], dtype=torch.float32)
data = torch.ones([10000], dtype=torch.float32)
fn = 5120
bm = 10000

coo_matrix((data, (row, col)), shape=(fn, bm))

Error Produced

Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib64/python3.8/site-packages/scipy/sparse/coo.py", line 161, in init
self.data = np.array(obj, copy=copy, dtype=data_dtype)
TypeError: data type not understood

Versions
Python 3.8.8
numpy 1.17.5
torch 1.7.1

There are two ways to solve this problem:-

  1. Use a tensor based function that accepts the tensors as default (Use torch.sparse_coo_tensor )
  2. Convert the tensors to numpy arrays using

tensor_data.cpu().detach().numpy()

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