Using tensorflowjs
inside an Angular7 project, I am encountering trouble in accessing the numerical values of a tensor2d
object.
Indeed:
let tensor2d = tf.tensor2d( [[1,2],[3,4]])
let tensor2d_array = tensor2d.dataSync()
will give an Array
object of shape (4,)
instead of (2,2,)
as I wish.
How can I preserve the shape and get an array of my tensor. (Reshaping by hand is not practical in all cases, as I do not have access to a js matrix library in this project..).
dataSync and data returns a flatten typedArray. It means that tensor2d_array
will be of shape [4].
Instead of getting the data with the same shape as the tensor, maybe it will be practical to get only the index value that you want to retrieve. Having said that, if for a certain reason, you want to get the data array first before getting the value at the index [i, j]
, this answer shows how you can retrieve element [i, j]
of the data got using either data
or dataSync
Update
Since the version 15
, it is possible to get an array with the same shape as the tensor using tensor.array() or tensor.arraySync()