[docs]defto_array_extended(tensor:Union[SparseTensorProto,TensorProto])->Union[np.ndarray,"scipy.sparse.coo_matrix"]:# noqa: F821""" Overwrites function `onnx.reference.op_run.to_array_extended` to support sparse tensors. """ifisinstance(tensor,TensorProto):returnonnx_to_array_extended(tensor)ifisinstance(tensor,SparseTensorProto):importscipy.sparseasspshape=tuple(dfordintensor.dims)indices=onnx_to_array_extended(tensor.indices)values=onnx_to_array_extended(tensor.values)iflen(indices.shape)==1:t=sp.csr_matrix((values,indices,np.array([0,len(indices)],dtype=np.int64)),shape=(1,np.prod(shape)),)returnt.reshape(shape)iflen(indices.shape)==2:t=sp.coo_matrix((values,(indices[:,0],indices[:,1])),shape=shape)returntraiseRuntimeError(f"Unexpected indices shape: {indices.shape}.")raiseTypeError(f"Unexpected type {type(tensor)}.")