0

I am a beginner at the neural network. Trying to recreate some basic predict for the formula y = 2x-1 from the tensorflow.js tutorial using ml5.js but something is getting wrong. Here is my code:

const model = ml5.neuralNetwork({

    task: 'regression',
    inputs: ['x'],
    outputs: ['y'],
    debug: true
});

const trainingData = [
    {input: {x: -1}, output: {y: -3}},
    {input: {x: 0}, output: {y: -1}},
    {input: {x: 1}, output: {y: 1}},
    {input: {x: 2}, output: {y: 3}},
    {input: {x: 3}, output: {y: 5}},
    {input: {x: 4}, output: {y: 7}}
];

trainingData.forEach(data => {
    model.addData(data.input, data.output);
});

model.normalizeData();
model.train({epochs: 100}, () => {
    console.log('Model trained');

    const inputData = {x: 20};
    model.predict(inputData, (error, result) => {
        if (error) {
            console.error(error);
        } else {
            console.log(`Prediction: ${result[0].value}`);
        }
    });
});

Expected: 20 => 39, Actual result: 20 => 6.999997615814209

What am i doing wrong?

Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51

0 Answers0