1

I'm making a simple program that tries to predict a value from an array of values:

require('@tensorflow/tfjs-node');
const {layers, setBackend, sequential, train} = require('@tensorflow/tfjs');

setBackend('tensorflow');

const {xs, ys} = require('./data.v2');

const [samples] = xs.shape;
const MAX_SAMPLES = samples - 3; // Leave 3 for predictions

const trainXs = xs.slice([0], MAX_SAMPLES);
const trainYs = ys.slice([0], MAX_SAMPLES);

const predict = xs.slice([MAX_SAMPLES]);
const expect = ys.slice([MAX_SAMPLES]);


const LEARNING_RATE = 0.01;

const BATCH_SIZE = 2;
const EPOCHS = 1000;


// Define a model for linear regression.
const model = sequential();

// First layer must have an input shape defined
model.add(layers.dense({units: 16, inputShape: [37,]}));
// Afterwards, TF.js does automatic shape inference
model.add(layers.dense({units: 4}));
model.add(layers.dense({units: 1}));


// https://js.tensorflow.org/api/0.10.0/#Training-Optimizers
const optimizer = train.adam(LEARNING_RATE);

// NOTE: For classification we would use a cross entropy loss fn,
// but for regression, we prefer mean squared error
// https://stackoverflow.com/a/36516373/1092007
// Prepare the model for training: Specify the loss and the optimizer.
model.compile({
    optimizer,
    loss: 'meanSquaredError'
});


fit(trainXs, trainYs, EPOCHS, BATCH_SIZE)
    .then(() => {
        console.log('Done training');
        // Use the model to do inference on a data point the model hasn't seen before:
        const item = predict.slice([0], 1);
        console.log(item.dataSync());

        model.predict(item, true)
            .print();

        expect.slice([0], 1)
            .print();
    });


async function fit(xs, ys, epochs, batchSize) {
    // Train the model using the data
    const history = await model.fit(xs, ys, {
        batchSize,
        epochs,
        shuffle: true,
        validationSplit: 0.3,
        callbacks: {
            onEpochEnd(...args) {
                const [epoch, history] = args;
                const {loss} = history;
                console.log(`Loss after epoch ${epoch}: ${loss}`);
            }
        }
    });
}

The input data is a tensor of shape [53, 37]:

[[1, 2, 3, ...], [4, 5, 6, ...], ...]

And the output is vector (shape [53]):

[3, 4, ...]

But I was wondering how do I calc the confidence level of the output of .predict()?

NOTE: I'm using tensorflow.js with Node, so the API might be a bit different from the Python API.

Roland
  • 9,321
  • 17
  • 79
  • 135
  • So you want to output something like a variance for each prediction? I usually just do a separate `dense` output layer followed by a `softplus`. Then you'd use a normal log likelihood loss instead of regular squared error. You may need to add a small constant to the `softplus` output for numerical stability. – Allen Lavoie May 18 '18 at 16:51
  • @allenlavoie sort of, the accuracy of the prediction. I've seen that using the `{metrics: ['accuracy']}` config for the model should give some metrics after each epoch end, but right now it seems to always be `0.0`. – Roland May 19 '18 at 19:30
  • Ah. Accuracy is presumably [similar to `tf.mertics.BinaryAccuracy`](https://js.tensorflow.org/api/0.11.1/#metrics.binaryAccuracy), which is discrete (it just doesn't make sense on real-valued data). Try https://js.tensorflow.org/api/0.11.1/#metrics.meanSquaredError ? I see that you're already using that for a loss. – Allen Lavoie May 21 '18 at 16:49

0 Answers0