pytaco.to_array
- pytaco.to_array(t)
Converts a taco tensor to a NumPy array.
This always copies the tensor. To avoid the copy for dense tensors, see the notes section.
- Parameters
- t: tensor
A taco tensor to convert to a NumPy array.
- Returns
- arr: numpy.array
A NumPy array containing a copy of the data in the tensor object t.
Notes
Dense tensors export python’s buffer interface. As a result, they can be converted to NumPy arrays using
np.array(tensor, copy=False)
. Attempting to do this for sparse tensors throws an error. Note that as a result of exporting the buffer interface dense tensors can also be converted to eigen or any other library supporting this inferface.Also it is very important to note that if requesting a NumPy view of data owned by taco, taco will mark the array as read only meaning the user cannot write to that data without using the taco reference. This is needed to avoid raising issues with taco’s delayed execution mechanism.
Examples
We first look at a simple use of to_array
>>> import pytaco as pt >>> t = pt.tensor([2, 2], [pt.dense, pt.compressed]) >>> t.insert([0, 0], 10) >>> t.to_array()[0, 0] 10.0
One could choose to use np.array if a copy is not needed
>>> import pytaco as pt >>> import numpy as np >>> t = pt.tensor([2, 2], pt.dense) >>> t.insert([0, 0], 10) >>> a = np.array(t, copy=False) >>> a array([[10., 0.], [ 0., 0.]], dtype=float32) >>> t.insert([0, 0], 100) # Note that insert increments instead of setting! >>> t.to_array()[0, 0] 110.0