To get help from the community, check out our Google group.
Latest & v0.13.3
I am working on a project that utilises tfjs's linear algebra APIs. I want to use Tensor's dataSync method to retrieve data of Tensor but it is returning a flat array without retaining its original shape.
Example 1: Running dataSync on a 2D tensor

Example 2: Running dataSync on zeros of shape 2, 2

How do I retrieve the Tensor data as a typed array or a plain array with their original shapes?
Relaying some replies that I received from @justadudewhohack on BRIIM Slack server on the same issue/question

Additional questions:
Why is it more efficient to return a flat array?
Would dataSyncRaw be a good feature request? It is a method of Tensor that returns the original data of the Tensor without losing any shape.
My take on 1. one: Imagine having a large 3D or 4D tensor. You will have to create quite a lot of nested arrays for this, which is probably not very efficient.
@justadudewhohacks right, returning a nested array would be very slow (all of the internals work on the TypedArray which is much much faster and converting that array to a nested array would be very slow). You could use a TensorBuffer if you want a CPU object and want to index into it (use tensor.buffer()).
An update on this, 0.15 introduced tensor.array() and tensor.arraySync() to provide this functionality. Hope that helps!
Most helpful comment
@justadudewhohacks right, returning a nested array would be very slow (all of the internals work on the TypedArray which is much much faster and converting that array to a nested array would be very slow). You could use a TensorBuffer if you want a CPU object and want to index into it (use tensor.buffer()).