Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

javascript - Error when checking input: expected dense_Dense1_input to have x dimension(s). but got array with shape y,z

I'm very new to Tensorflowjs and Tensorflow in general. I have some data, which is capacity used out of 100%, so a number between 0 and 100, and there are 5 hours per day these capacities are noted. So I have a matrix of 5 days, containing 5 percentages out of 100%.

I have the following model:

const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [5, 5] }));
model.compile({ loss: 'binaryCrossentropy', optimizer: 'sgd' });

// Input data
// Array of days, and their capacity used out of 
// 100% for 5 hour period
const xs = tf.tensor([
  [11, 23, 34, 45, 96],
  [12, 23, 43, 56, 23],
  [12, 23, 56, 67, 56],
  [13, 34, 56, 45, 67],
  [12, 23, 54, 56, 78]
]);

// Labels
const ys = tf.tensor([[1], [2], [3], [4], [5]]);

// Train the model using the data.
model.fit(xs, ys).then(() => {
  model.predict(tf.tensor(5)).print();
}).catch((e) => {
  console.log(e.message);
});

I'm getting an error returned: Error when checking input: expected dense_Dense1_input to have 3 dimension(s). but got array with shape 5,5. So I suspect I'm entering or mapping my data incorrectly in some way.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your error comes from a mismatch of the size of the training and test data from one hand on the other hand by what is defined as the input of your model

model.add(tf.layers.dense({units: 1, inputShape: [5, 5] }));

The inputShape is your input dimension. Here it is 5, because each features is an array of size 5.

model.predict(tf.tensor(5))

Also to test your model, your data should have the same shape as when your are training your model. Your model cannot predict anything with tf.tensor(5). Because your training data and your test data size do not match. Consider this test data instead tf.tensor2d([5, 1, 2, 3, 4], [1, 5])

Here is a working snipet


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...