Error: Size(448) must match the product of shape 7

Hi,

I’m a beginner and trying to create and train a simple model, but I’ve been stuck for more than 3 days and keep receiving this error message:

“Size(448) must match the product of shape 7 when executing model.fit(…)”

Here is some information that may be helpful:

  • Input shape: inputShape = [7]
  • Train dataset input shape: trainX.shape = [887,7]
  • Train dataset output shape: ytrainData = [887]

Can anyone please help with this issue?

Here is the script:

function get_model() {
  const model = tf.sequential();
  model.add(
    tf.layers.dense({
      inputShape: 7,
      units: 124,
      activation: "relu",
      // kernelInitializer: "leCunNormal",
    })
  );
  model.add(tf.layers.dense({ units: 64, activation: "relu" }));
  model.add(tf.layers.dense({ units: 32, activation: "relu" }));
  model.add(tf.layers.dense({ units: 1, activation: "sigmoid" }));
  model.summary();
  return model;
}

async function train() {
  const model = await get_model();
  const data = await load_process_data();
  const Xtrain = data[0];
  const ytrain = data[1];

 model.compile({
    optimizer: "adam",
    loss: "binaryCrossentropy",
    metrics: ["accuracy"],
  });
model.fit(Xtrain, ytrain, {
    epochs: 20,

    callbacks: {
      onEpochEnd: async (epoch, logs) => {
        console.log(logs.loss);
        console.log(`EPOCH (${epoch + 1}): Train Accuracy: ${logs}`);
      },
    },
  });
}

@TheOutsider

Welcome to the Tensorflow Forum!

  1. Can you please try to reshape the train data before passing it to the model.fit(trainX, trainY,…).
  2. Also, please try to reshape “inputShape: 7” .

I have created a gist using MNIST dataset and reshaped the input data. Please follow and try similar approach in your usecase.

Let us know if you still face the issue.

Thanks

Hi,

Thank you for taking the time to give me feedback. However, I’m still struggling to understand why I should reshape the data. My input data consists of a list of 887 examples, each with 7 features, and the output is a single feature. So the shape of the input data is [887, 7], and the output shape is [887, 1], with an input shape of 7.

I did try to follow the example you provided and experimented with different shapes for the input, output, and input shape, but I kept running into error messages. Some of these messages were about the tensor dimension being different from what was expected.

Do you have any other ideas or possible reasons why this might be happening? Any suggestions or guidance would be greatly appreciated.

Thanks again for your help.