question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 !

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
Famidoccommented, Jun 30, 2020

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; …

0reactions
tafsiricommented, Jun 1, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: tensor.sub(...).get is not a function - Stack Overflow
To download the tensor data from the backend, the following can be used. tf.data ( and the corresponding sync tf.dataSync ).
Read more >
tf.Tensor | TensorFlow v2.11.0
This overload raises a TypeError when the user inadvertently treats a Tensor as a boolean (most commonly in an if or while statement),...
Read more >
Model.predict() not a function - Google Groups
to TensorFlow.js Discussion. I am trying to do image segementation of cell images to identify cells in tensor flow using data provided in ......
Read more >
Problem with TensorFlow load model - Libraries
Hey, first time I'm using TensorFlow.js and I got a problem: ... function setup() { load(); } async function load() { model =...
Read more >
Uncaught (In Promise) Typeerror: Tf.Sequential Is Not A ...
tensorboard object is not iterable iterator)) 15 Remember our best friend tf. TypeError: 'int' object is not iterable """ The above exception was...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found