Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

81
Views
tensorflowJS cant figure out arrays

I am trying to input an array of data and output a prediction or a guess for what the result should be for the given data, I do not know much about neural nets but have a basic understanding on how their training works... I was hoping that it would see the two given inputs combined add up to the output in the three examples I provided for it such as [1, 2] should = 3 but I have hit a wall with inputting arrays and am getting the fallowing error

UnhandledPromiseRejectionWarning: Error: input expected a batch of elements where each example has shape [1] (i.e.,tensor shape [*,1]) but the input received an input with 3 examples, each with shape [2] (tensor shape [3,2])

any ideas someone can give me on how I should be going about this would be etremely helpful -.-

import tf from '@tensorflow/tfjs';

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

// compile model object
model.compile({
    optimizer: 'sgd',
    loss: 'meanSquaredError'
});


// training datasets
// input data for training
const xs = tf.tensor2d([
    [1, 2],
    [5, 6],
    [3, 4]

]);

// output data for training
const ys = tf.tensor1d([
    3,
    11,
    7

]);


async function firstFunction(_callback) {


    // Train model with fit().method
    await model.fit(xs, ys, {
        epochs: 100000
    })
    _callback();
}


function secondFunction() {

    //We wait for the call back from first function.
    firstFunction(function() {

        // We run the prediction.                    
        prediction = model.predict(tf.tensor2d([
            [6, 9],
        ])).dataSync()[0]

    });
}

secondFunction()
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!