I am trying to fit exponential data to an exponential regression with tensorflow.js, such as:
y(x) = c0e^(kx)
I have followed examples where they have fitted a linear regression with just a few epochs, like here.
The problem is that when I change the tensor equation to an exponential function, even if I increase to 500-5000 epochs and providing close initial values, it does not fit properly. With a large learning rate, the variables go to very high values, and low learning rate the variables don't substantially change.
Is there anything I am doing wrong in the code? Is it because optimization is not fit for exponential functions? Is there any other way to do implement this in a browser without using tf.js?
The code I have used is:
const x = tf.tensor1d([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
const y = tf.tensor1d([2.5879,3.1153,3.7041,4.6216,5.2307,5.6205,6.9904,7.8416,9.0201,10.5586,12.1638,14.1438,16.5961,19.2497,22.3430]);
const c0 = tf.scalar(2).variable();
const k = tf.scalar(0.10).variable();
// y = c0*e^(k*x)
const fun = (x) => x.mul(k).exp().mul(c0);
const cost = (pred, label) => pred.sub(label).square().mean();
const learning_rate = 0.001;
const optimizer = tf.train.sgd(learning_rate);
// Train the model.
for (let i = 0; i < 20; i++) {
optimizer.minimize(() => cost(fun(x), y));
}
console.log(`c0: ${c0.dataSync()}, k: ${k.dataSync()}`);
const preds = fun(x).dataSync();
preds.forEach((pred, i) => {
console.log(`x: ${i}, pred: ${pred}`);
});