Overview
Multiple linear regression is very similary to single variable regression, the only signficant difference is the shape of the inputs.
Example with Multiple Variables
The following demonstrates running a 2 variable regression, using hard coded data.
await $src('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js');
let linearModel = tf.sequential();
linearModel.add(tf.layers.dense({units:1, inputShape:[2]}));
linearModel.compile({loss:'meanSquaredError',optimizer:'sgd'});
let xs = tf.tensor([[3.2,1],[4.4,1],[5.5,2],[6.71,0.8],[7.168,1.2],[9.779,5],[6.182,5],[7.59,4.5],[2.16,7]]);
let ys = tf.tensor([[1.6],[2.7],[2.9],[3.19],[1.684],[2.53],[3.366],[2.596],[2.53]]);
await linearModel.fit(xs,ys,{
epochs:80
});
let output = linearModel.predict(tf.tensor([4,6]));
prediction = Array.from(output.dataSync())[0];
Try it!
No Intercept
In order to specify that the model not use a bias (or intercept term), you specify the property useBias as false in the model.add method.
await $src('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js');
var linearModel = tf.sequential();
linearModel.add(tf.layers.dense({units:1, inputShape:[2], useBias: false}));