I'm trying to create a tensorflow.js model (With javascript) to predict the square of a number, so I have a function to create the xData(x) and the yData(x²).
I've tried to use many hiden layers with different units and to train with thousand epochs but the loss is realy hight and the predictions are not good at all.
here is my code:
const tf=require("@tensorflow/tfjs-node");
let dataX=[
//0, 1, 2, 3, ..., 1000;
];
let dataY=[
//0, 1, 4, 9, ..., 1000000;
]
for (let i = 0; i < 1000; i++) {
dataX.splice(0,0, i);
dataY.splice(0,0, i**2);
}
class AI{
compile(){
const model=tf.sequential();
//Input & hidenA layer
model.add(tf.layers.dense({
inputShape: [1],
units: 32,
activation: 'sigmoid',
useBias: true
}));
//HidenB layer
model.add(tf.layers.dense({
units: 32,
activation: 'sigmoid',
useBias: true
}))
//Output layer
model.add(tf.layers.dense({
units: 1,
activation: 'sigmoid',
useBias: true
}))
let sgdOpt=tf.train.sgd(0.00001)
model.compile({
loss: 'meanSquaredError',
optimizer: sgdOpt
})
return model;
}
run(){
const model=this.compile();
const xs=tf.tensor(dataX)
const ys=tf.tensor(dataY)
try{
model.summary();
model.fit(xs, ys, {
epochs: 1000,
}).then(()=>{
const data=tf.tensor([10]);
const prediction=model.predict(data);
prediction.print();
})
}catch(e){
}
}
}
const ai=new AI();
ai.run();


