I'm trying to perform a classification with a SVM model. I'm using SciKit.js cause, according to the datasheet, it's the same as its Python version, and I know it a little.
First, as a learning exercice, I want a model able to predict in which quart of a square a point is (bottom-left, bottom-right, top-left, top-right).
Here is my code :
// Importing libs, following datasheet
import * as tf from 'https://cdn.skypack.dev/@tensorflow/tfjs'
// import {setBackend, trainTestSplit} from 'https://cdn.skypack.dev/scikitjs'
import * as sk from 'https://cdn.skypack.dev/scikitjs'
sk.setBackend(tf)
// function creating dataset
function genererVecteur() {
let datas_gen = {};
let valeurs = [];
let labels = [];
let x, y;
let position;
for(let i = 0; i < 100; i++){
x = Math.random();
y = Math.random();
position = y < 0.5 ? 'bas ' : `haut `;
position += x < 0.5 ? 'gauche' : 'droite';
valeurs.push(([x, y]));
labels.push(position);
}
datas_gen.datas = valeurs;
datas_gen.labels = labels;
return datas_gen;
}
// Normalizing datas
const scaler = new sk.StandardScaler();
scaler.fit(X_train);
let X_train_std = scaler.transform(X_train);
let X_test_std = scaler.transform(X_test);
// Normalizing labels, as the SVC training function accept only numbers as "targets", not strings
const encodeur = new encodeurSurN();
encodeur.fit(Y_train);
let Y_train_std = encodeur.encode(Y_train);
let Y_test_std = encodeur.encode(Y_test);
async function goIA(){
// creating model
let svc = new sk.LinearSVC();
// training model
await svc.fit(X_train_std, Y_train_std);
console.log("modèle entraîné");
//Test
let Y_predict = svc.predict(X_test_std);
console.log("prédictions faites");
//Evaluation
console.log(Y_predict);
// console.log(Y_predict.item()); // not a function
// console.log(Y_predict[0]); // undefined
}
goIA();
svc.fit
method is waiting for 2 arguments : a samples array, and a targets array. But the target array can't contain strings, so I created a class "convert" my labels to ints. (instead of just changing my dataset, as I want to easily read predicted positions, so I've an encode
and a decode
method.)
Here is my encodeurSurN
class, I wrote it to use it the same way as the scaler for datas :
class encodeurSurN{
constructor(){
}
fit(vecteur){
this.valeursUniques = Array.from(new Set(vecteur));
}
encode(vecteur){
let vecteur_codes = [];
for(let i = 0; i < vecteur.length; i++){
vecteur_codes.push(this.valeursUniques.indexOf(vecteur[i]));
}
return vecteur_codes;
}
decode(vecteur){
let vecteur_valeurs = [];
for(let i = 0; i < vecteur.length; i++){
vecteur_valeurs.push(this.valeursUniques[vecteur[i]]);
}
return vecteur_valeurs;
}
}
My problem is that I want to reed what is in Y_predict
, so I can have an idea of the code to write to compare it to Y_test
and check is my model is good enough, but the console.log(Y_predict);
line is just returning me the Tensor as :
Tensor {kept: false, isDisposedInternal: false, shape: Array(1), dtype: 'float32', size: 30, …}
dataId:{id: 94413}
dtype:"float32"
id:76638
isDisposedInternal:false
kept:false
rankType:"1"
shape:[30]
size:30
strides:[]
isDisposed:(...)
rank:(...)
[[Prototype]]:Object
But I don't have any Idea of what it is, as I don't know TensorFlow, and google don't seems to have an easy way to see my labels in this.
So I tried to use console.log(Y_predict.item());
as suggested here, even if it's about Pytorch... maybe there is common key words ? (topic suggested as an answer to my title when i was writing my own topic). but I had the error "Not a function".
I also tried to use dataSync() and data() and it's... working ? well I've an array full of 1
. So the prediction is just false. Is it the good way to access the predictions in a tensor ? Or maybe it's the good way and there are errors in my script ?
P.S. : If you want to try the code, it can be long to run the await svc.fit(X_train_std, Y_train_std);
instruction.
It seems it's a young library, here is a link to datasheet : quick start