我正在创建我的第一个学习机器学习的KNN Algo。我正在看一个在线的基础课程来解释它,我感觉我和他做的完全一样。
但是当我运行它的时候,我得到了js的一个非常基本的错误。我正在使用TensorFlow。
.sort((a, b) => (a.get(0) > b.get(0) ? 1 : -1))
^
TypeError: a.get is not a function
require('@tensorflow/tfjs-node');
const tf = require('@tensorflow/tfjs');
const loadCSV = require('./load-csv');
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) => (a.get(0) > b.get(0) ? 1 : -1))
.slice(0, k)
.reduce((acc, pair) => acc + pair.get(1), 0) / k
);
}
let { features, labels, testFeatures, testLabels } = loadCSV(
'kc_house_data.csv',
{
shuffle: true,
splitTest: 10,
dataColumns: ['lat', 'long'],
labelColumns: ['price'],
}
);
features = tf.tensor(features);
labels = tf.tensor(labels);
console.log(features, labels, tf.tensor(testFeatures[0]), 10);
const result = knn(features, labels, tf.tensor(testFeatures[0]), 10);
console.log('Guess', result, testLabels[0][0]);
console.log(features);
顶部的日志,以查看传入函数的内容。
Tensor {
kept: false,
isDisposedInternal: false,
shape: [ 21602, 2 ],
dtype: 'float32',
size: 43204,
strides: [ 2 ],
dataId: { id: 0 },
id: 0,
rankType: '2'
} Tensor {
kept: false,
isDisposedInternal: false,
shape: [ 21602, 1 ],
dtype: 'float32',
size: 21602,
strides: [ 1 ],
dataId: { id: 1 },
id: 1,
rankType: '2'
} Tensor {
kept: false,
isDisposedInternal: false,
shape: [ 2 ],
dtype: 'float32',
size: 2,
strides: [],
dataId: { id: 2 },
id: 2,
rankType: '1'
} 10
发布于 2021-03-09 20:54:00
经过长时间的研究,花了很多时间。
TensorFlow删除了您将使用的.get函数,而不是arraySync。例如。
pair.get(1)[0]将是:
pair.arraySync(1)[0]https://stackoverflow.com/questions/66546790
复制相似问题