Tensor Flow - Single Variable Regresssion

Overview


Linear regressions can be implemented in tensorflow using the sequential model. In the

Implementation


The following code implements a single variable regression by creating a sequential model with one input and one output. It uses stochastic gradient descent to find the optimal point with the standard mean squared error loss function.


const model = tf.sequential();
model.add(tf.layers.dense({
  inputShape:[1],
  units:1
}));

let learningRate = 0.1;
model.compile({
  opitimizer:tr.train.sgd(learningRate),
  loss:'meanSquaredError'
});

await model.fit(X,y,{
  epochs:100
});
				

Notice that there is no activation function defined, which means the model output is just passed out with no activation function applied.

Example with Hard Coded Data


The following demonstrates running a linear regression against 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:[1]}));
linearModel.compile({loss:'meanSquaredError',optimizer:'sgd'});

let xs = tf.tensor([[3.2],[4.4],[5.5],[6.71],[7.168],[9.779],[6.182],[7.59],[2.16]]);
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:[4], useBias: false}));

				

Setting the Initial Weights


Typically it is not necessary to set the initial weights of a regression, however, the following shows how to do it.


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:[4], useBias: false}));
let weights = tf.tensor([[0.14455926418304443]]);
let bias = tf.tensor([1.638200283050537]);
linearModel.layers[0].setWeights([weights, bias]);
				
Try it!

Notice that the input weights is an array of arrays. That is, you need an array for each output unit. In this case, there is only one output unit.

Contents