Tfjs: Tensor.dataSync() does not retain the original shape

Created on 26 Nov 2018  路  4Comments  路  Source: tensorflow/tfjs

To get help from the community, check out our Google group.

TensorFlow.js version

Latest & v0.13.3

Describe the problem

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?

core

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()).

All 4 comments

Relaying some replies that I received from @justadudewhohack on BRIIM Slack server on the same issue/question

Additional questions:

  1. Why is it more efficient to return a flat array?

  2. 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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

neilljordan picture neilljordan  路  4Comments

RELNO picture RELNO  路  3Comments

nsthorat picture nsthorat  路  3Comments

lastnod picture lastnod  路  3Comments

kylemcdonald picture kylemcdonald  路  3Comments