Tfjs: TypeError: tf.tensor(a).get(0) is not a function

Created on 31 Mar 2020  路  11Comments  路  Source: tensorflow/tfjs

Hello,

I am learning TensorFlow.js through an online course.
we prepared a very simple KNN function by tfjs but on the teacher's computer it works but on my computer no.

I installed the 1.7.0 version of TensorFlow ( because of the latest existed document in tfjs website is 1.7.0)
Node version: v12.16.1

when I run node on the terminal the below error appears :

 .sort((a,b)=>{ return  a.get(0)>b.get(0) ? 1 : -1 ; })//return  a.get(0)>b.get(0) ? 1 : -1 ;
                                         ^
TypeError: a.get is not a function

it is my code :

require("@tensorflow/tfjs-node");
const tf = require("@tensorflow/tfjs");
const loadCSV = require("./load-csv.js");


function knn( features , labels, predictionPoint , k){
    return features
            .sub(predictionPoint)
            .pow(2)
            .sum(1)
            .pow(0.5)
            .expandDims(1)
            .concat(labels , 1)
            .unstack()
            .sort((a,b)=>{ return  a.get(0)>b.get(0) ? 1 : -1 ; })//return  a.get(0)>b.get(0) ? 1 : -1 ;
            .slice(0, k)
            .reduce((accumulated , thisObj ) => { 
                // return accumulated + thisObj.get(1) 
            }, 0)/k;

}

let { features , labels , testFeatures , testLabels } = loadCSV("./kc_house_data.csv" , {
    shuffle : true , 
    splitTest : 10,
    dataColumns: ['lat','long'],
    labelColumns: ['price']
});

const locations = tf.tensor( features );
const prices = tf.tensor( labels );
const weAreLookingFor =  tf.tensor( testFeatures[0] );

const result = knn ( locations, prices, weAreLookingFor , 10 );
console.log("KNN result is  : ",result);

I could not find any related issues about it.
I am completely stuck because of this problem. Please Help !

core support

Most helpful comment

It will work if change some codes to :

...............
.sort((a,b)=> a.arraySync()[0]> b.arraySync()[0]? 1:-1)
.slice(0,k)
.reduce((acc,pair)=>acc+pair.arraySync()[1],0)/k;
................

All 11 comments

This problem occurred when I tried to simply test in on CodePen.io, as well. It is strange.
I run just this code :

const test=tf.tensor([12,22]);
console.log(test.get(0))

it shows :

Uncaught TypeError: test.get is not a function 
 at pen.js:-12

@alidasmeh can you please share a CodePen link where we can reproduce ? thank you

Hi,
it is the link:
https://codepen.io/alidasmeh/pen/MWwxpgR?editors=1011

I just found that when I use version "0.15.3" and prior versions the ".get()" method is working.
But in the version "1.0.0" and after that ".get()" method occurs errors.

I need to use latest version of tfjs due to my node js version is "12.6.3".

Hi again,
In Codepen I changed the tfjs version to "0.15.3" and I saw a warning in the console :
"Tensor.get() is deprecated. Use Tensor.array() and native array indexing instead. You can disable deprecation warnings with tf.disableDeprecationWarnings()."
I used the array method as well. But it did not help.
array method returns a "Promise { }". I don't know how I can get data from this object.

Tensor.array() is an asynchronize function. You will need to use await or promise to get the returned value. Or you can you Tensor.arraySync(): https://js.tensorflow.org/api/latest/#tf.Tensor.array

@alidasmeh you should use async await to use .array , I have modified the code pen please check

Thank you.
I could redevelop my code. it is working now.
But I had to write 5 lines more instead of using get method.
In the API documentation in "https://js.tensorflow.org/api/1.7.0/" the get method is mentioned. If it is not working longer, please delete it.

I had to write a code like below code to do the get method duty :

function get_an_element_from_tensor( a , index ){
    a = a.arraySync(); // return a object 
    return a[ Object.keys( a )[ index ] ]; // return the indexed element
}

can you please share the link where the get method is mentioned.

@rthadur It could be from a Udemy course called "Machine Learning with JavaScript". The course seems to be using version 0.13 of TensorFlow.

get is a method on TensorBuffer (https://js.tensorflow.org/api/1.7.0/#tf.TensorBuffer.get) not Tensor

It will work if change some codes to :

...............
.sort((a,b)=> a.arraySync()[0]> b.arraySync()[0]? 1:-1)
.slice(0,k)
.reduce((acc,pair)=>acc+pair.arraySync()[1],0)/k;
................

Was this page helpful?
0 / 5 - 0 ratings