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

102
Views
TensorFlow.js takes wrong shape of input

I was trying to build a simple example of NN using TensorFlow.js, with source converted from an other old TensorFlow (Python) project of mine.
But for some reason, the model fails to interpret the input as I wanted, which is (2, 3, 3) for input, and (2) for output

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

Full code:

  // define model
  let model = new Sequential({
    name: "test",
    layers: [
      layers.flatten({ inputShape: [3, 3] }),
      layers.dense({ units: 9, activation: 'relu' }),
      layers.dense({ units: 2 })
    ]
  });
  model.compile({
    optimizer: 'Adam',
    // should be loss=kr.losses.SparseCategoricalCrossentropy(from_logits=True)
    // as per the old code, but I don't know how yet
    // and actually, I'm not sure there would be any difference
    loss: losses.meanSquaredError, 
    metrics: ['accuracy']
  })

  // define input and output
  let X = tensor(
    [
      [
        [0, 1, 0],
        [1, 0, 1],
        [0, 1, 0]
      ],
      [
        [1, 0, 1],
        [0, 1, 0],
        [1, 0, 1]
      ],
    ]
  );
  let Y = tensor([1, 0]);

  model.fit(X, Y, { epochs: 1000 })
    .then(v => console.log("success", v))
    .catch((e: Error) => console.log(e.message)); // <- message logged from here

However, as I described, the shape of X in this case would be [2, 3, 3], not [2]:

console.log(X.shape);
> (3) [2, 3, 3]

I'm stuck at this one for several days now. Where did [*,2] even come from?

about 4 years ago · Santiago Trujillo
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!