问题
Anyone would help me with this TensorFlow JS project ? It's a Chat bot with machine learn, I stuck on 'build neural network' , giveme this error
Project Link : https://github.com/ran-j/ChatBotNodeJS
The training code at /routes/index.js line 189
//Build neural network
model = tf.sequential();
model.add(tf.layers.dense({inputShape: [documents.length], units: 100}));
model.add(tf.layers.dense({units: 4}));
model.compile({loss: 'categoricalCrossentropy', optimizer: 'sgd'});
model.fit(xs, ys, {epochs: 1000});
回答1:
The error indicates that there is a mismatch between the shape defined for the model and the tensors used by the model be it the training or test tensors.
In order to get rid of the error you need both shapes to match.
Expected dense_Dense1_input to have shape a but got array with shape b
In the error a is the shape of the model and b is the shape of the tensor that is throwing the error. So one needs to change whether the shape of the model to b or the shape of the tensor to be a.
The easiest way is to change the model shape to b since the second way would imply a reshape of the tensor i.e
model.add(tf.layers.dense({inputShape: b, units: 100}));
Given the model of the question, it will be
model.add(tf.layers.dense({inputShape: [27, 48], units: 100}));
回答2:
documents.length
is the amount of training data you have and not the inputShape of your model. So your training data doesn't have the correct shape for your model.
The correct shape would be xs.shape
.
So your fist layer should be:
tf.layers.dense({inputShape: xs.shape, units: 100})
来源:https://stackoverflow.com/questions/51790230/expected-dense-dense1-input-to-have-shape-a-but-got-array-with-shape-b