I want to fit a nonlinear multivariable equation using TensorFlow. The equation is given below. The parameters to fit are a0, a1, and a2. The independent variables are S and R, while F is the dependent variable. The corresponding data for S,R,F are provided in the code below, as Sdata, Rdata, and Fdata respectively.
F = a0 + a1*S + a2*R
I
const Sdata = tf.tensor1d([13.8,13.8,20.2,12.1,14.1,29.4,13.7,16.6,18.9,15.5]);
const Fdata = tf.tensor1d([46.7,130.7,78.1,72.2,40.1,78.6,57.4,170.7,80.2,45.2]);
const Rdata = tf.tensor1d([1.5,4.5,2.5,3.0,3.5,3.0,2.5,3.0,3.0,2.5])
const a0 = tf.scalar(Math.random()).variable();
const a1 = tf.scalar(Math.random()).variable();
const a2 = tf.scalar(Math.random()).variable();
const fun = (r,s) => a2.mul(r).add(a1.mul(s)).add(a0)
const cost = (pred, label) => pred.sub(label).square().mean();
const learningRate = 0.01;
const optimizer = tf.train.sgd(learningRate);
// Train the model.
for (let i = 0; i < 800; i++) {
optimizer.minimize(() => cost(fun(Rdata,Sdata), Fdata));
}
As shown in my code, I assumed the function "fun" can take two independent variable. Instead of getting a0 = -6.6986, a1 = 0.8005, and a2 = 25.2523, I am getting NaNs.
Does it mean it is not possible to fit multivariable functions in tensorflow.js? I don't think so. I will appreciate any insight to this.
Because of the learning rate, the model is oscillating to find the best parameters. Actually, the parameters keep on increasing to Infinity.
Tuning the learning rate will allow the model to find the best parameters. In this case 0.001
seems to give good result. If you want to improve the accuracy of the model, you can consider normalizing all your input data to be of the same magnitude order - between 0 and 1
const Sdata = tf.tensor1d([13.8,13.8,20.2,12.1,14.1,29.4,13.7,16.6,18.9,15.5]);
const Fdata = tf.tensor1d([46.7,130.7,78.1,72.2,40.1,78.6,57.4,170.7,80.2,45.2]);
const Rdata = tf.tensor1d([1.5,4.5,2.5,3.0,3.5,3.0,2.5,3.0,3.0,2.5])
const a0 = tf.scalar(Math.random()).variable();
const a1 = tf.scalar(Math.random()).variable();
const a2 = tf.scalar(Math.random()).variable();
const fun = (r,s) => a2.mul(r).add(a1.mul(s)).add(a0)
const cost = (pred, label) => pred.sub(label).square().mean();
const learningRate = 0.001;
const optimizer = tf.train.sgd(learningRate);
// Train the model.
for (let i = 0; i < 800; i++) {
console.log("training")
optimizer.minimize(() => cost(fun(Rdata,Sdata), Fdata));
}
console.log(`a: ${a0.dataSync()}, b: ${a1.dataSync()}, c: ${a2.dataSync()}`);
const preds = fun(Rdata,Sdata).dataSync();
preds.forEach((pred, i) => {
console.log(`x: ${i}, pred: ${pred}`);
});
<html>
<head>
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"> </script>
</head>
<body>
</body>
</html>