Reputation:
I'm trying to get some linear regression for a project. As I'm used to Javascript, I decided to try and use TensorFlowJS.
I'm following the tutorial from their website and have watched some videos explaining how it works, but I still can't understand why my algorithm doesn't return the result I expect.
Here is what I'm doing:
// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
// Prepare the model for training: Specify the loss and the optimizer.
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
// Generate some synthetic data for training.
const xs = tf.tensor1d([1, 2, 3, 4]);
const ys = tf.tensor1d([1, 2, 3, 4]);
// Train the model using the data.
model.fit(xs, ys).then(() => {
// Use the model to do inference on a data point the model hasn't seen before:
// Open the browser devtools to see the output
const output = model.predict(tf.tensor2d([5], [1,1]));
console.log(Array.from(output.dataSync())[0]);
});
I'm trying here to have a linear graph, where the input should always be equal to the output.
I'm trying to predict what I would get with an input of 5
, however it seems that the output is random.
Here it is on codepen so you can try: https://codepen.io/anon/pen/RJJNeO?editors=0011
Upvotes: 1
Views: 627
Reputation: 18381
Your model is making prediction after only one epoch (one cyle of training). As a result the loss is still big which leads to unaccurate prediction.
The weights of the model are initialized randomly. So with only one epoch, the prediction is very random. That's why, one needs to train for more than one epoch, or update weights after each batch (here you have only one batch also). To have a look at the loss during training, you can change your fit method that way:
model.fit(xs, ys, {
callbacks: {
onEpochEnd: (epoch, log) => {
// display loss
console.log(epoch, log.loss);
}
}}).then(() => {
// make the prediction after one epoch
})
To get accurate prediction, you can increase the number of epochs
model.fit(xs, ys, {
epochs: 50,
callbacks: {
onEpochEnd: (epoch, log) => {
// display loss
console.log(epoch, log.loss);
}
}}).then(() => {
// make the prediction after one epoch
})
Here is a snippet which shows how increasing the number of epochs will help the model to perform well
// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
// Prepare the model for training: Specify the loss and the optimizer.
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
// Generate some synthetic data for training.
const xs = tf.tensor1d([1, 2, 3, 4]);
const ys = tf.tensor1d([1, 2, 3, 4]);
// Train the model using the data.
model.fit(xs, ys, {
epochs: 50,
callbacks: {
onEpochEnd: (epoch, log) => {
console.log(epoch, log.loss);
}
}}).then(() => {
// Use the model to do inference on a data point the model hasn't seen before:
// Open the browser devtools to see the output
const output = model.predict(tf.tensor2d([6], [1,1]));
output.print();
});
<html>
<head>
<!-- Load TensorFlow.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/tensorflow/0.12.4/tf.js"> </script>
</head>
<body>
</body>
</html>
Upvotes: 3
Reputation: 7346
Your training data needs to be one more dimension, than your model shape to reflect training batches. So x and y need to be at least 2D.
Upvotes: 0