Russell C.
Russell C.

Reputation: 608

Tensorflow.js prediction seems to have an upper limit of 1?

I was playing around with some tensorflow code I got from a youtube tutorial that predicts data of flowers. Here is the script (the training data is assigned to variable "iris" and the testing data is assigned to variable "irisTesting":

const trainingData = tf.tensor2d(iris.map(item => [
    item.sepal_length, item.petal_length, item.petal_width,
]));
const outputData = tf.tensor2d(iris.map(item => [
    item.species === "setosa" ? 1 : 0,
    item.species === "virginica" ? 1 : 0,
    item.species === "versicolor" ? 1 : 0,
    item.sepal_width
]));
const testingData = tf.tensor2d(irisTesting.map(item => [
    item.sepal_length, item.petal_length, item.petal_width
]));

const model = tf.sequential();

model.add(tf.layers.dense({
    inputShape: [3],
    activation: "sigmoid",
    units: 5,
}));
model.add(tf.layers.dense({
    inputShape: [5],
    activation: "sigmoid",
    units: 4,
}));
model.add(tf.layers.dense({
    activation: "sigmoid",
    units: 4,
}));
model.compile({
    loss: "meanSquaredError",
    optimizer: tf.train.adam(.06),
});
const startTime = Date.now();
model.fit(trainingData, outputData, {epochs: 100})
    .then((history) => {
         //console.log(history);
        console.log("Done training in " + (Date.now()-startTime) / 1000 + " seconds.");
        model.predict(testingData).print();
    });

When the console prints the predicted sepal_width, it seems to have an upper limit of 1. The training data has sepal_width values of well over 1, but here is the data that is logged:

Tensor
    [[0.9561102, 0.0028415, 0.0708825, 0.9997129],
     [0.0081552, 0.9410981, 0.0867947, 0.999761 ],
     [0.0346453, 0.1170913, 0.8383155, 0.9999373]]

The last (fourth) column would be the predicted sepal_width value. The predicted values should be larger than 1 however it seems that something is preventing it from being larger than 1.

This is the original code: https://gist.github.com/learncodeacademy/a96d80a29538c7625652493c2407b6be

Upvotes: 0

Views: 190

Answers (3)

Sebastian Speitel
Sebastian Speitel

Reputation: 7346

The activation function of your last layer is sigmoid.

The Sigmoid function looks like this: Sigmoid

source

And as you can see it is restricted in the range of 0 to 1. So if you want other output values you need to adjust your last activation function accordingly.

Upvotes: 1

oscfri
oscfri

Reputation: 2638

You're using a sigmoid activation function in the final layer to predict the sepal_width. Sigmoid is continuous function bounded between 0 and 1. See Wikipedia for a more thorough explanation.

You should try to use a different activation function if you want to predict the sepal_width. For a list of available activation functions you can check Tensorflow's API page (this is for the Python version, but the it should be similar for the JavaScript version). You can try 'softplus', 'relu' or even 'linear', but I cannot say if any of these are suitable for your application. Try and experiment to see which is best.

Upvotes: 1

MTMD
MTMD

Reputation: 1232

The original code from here addresses a classification problem. It is not meaningful to add item.sepal_width in your outputData because it is not another class.

Upvotes: 1

Related Questions