const tf = require("@tensorflow/tfjs-node")
const es=500;
function convertTemp(cel){
return (cel*1.8)+32 // Convert celsius to fahrenheit
}
let celsiusTemp= []
let fahrenheitTemp = []
for (let in = 0; in < 100; in++) {
var value = 100; // Keeping this only value to ensure that Tf knows the answer
celsiusTemp.push(in) // Shape [20,1]
fahrenheitTemp.push(convert(in)) // Push the answer (212) to the fahrenheit
}
const trainingMachine=async(q,w) => {
const models=tf.sequential();
models.add(tf.layers.dense({units:1,inputShape:[1]}));
models.compile({loss:'meanSquaredError',optimizer:'sgd'});
await models.fit(q,w,{epochs:es})
return models;
}
const prediction =(models,a) => {
const predictedValue =models.prediction(tf.tensor2d([a],[1,1]));
return predictedValue;
}
const q= tf.tensor2d(celsiusTemp.slice(0,15),[15,1]);
const w= tf.tensor2d(fahrenheitTemp.slice(0,15),[15,1]);
(async () => {
let trainedMachine = await trainingMachine(q,w);
for (let a of [4,6,12]) {
let predictedValue=prediction(trainedMachine, a).dataSync();
console.log(`Value: ${a} Predicted: ${predictedValue [0]}`)
}
})()