Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - Error: Error when checking input: expected dense_Dense1_input to have 3 dimension(s). but got array with shape 1,9

Im really new to tensorflow.js and im trying to do simple model that tell you on what side of the canvas you cliked

const model = tf.sequential();
model.add(
         tf.layers.dense({
           units: 200,
           activation: "sigmoid",
           inputShape: [0, 1],
         })
       );
       model.add(
         tf.layers.dense({
           units: 2,
           activation: "softmax",
         })
       );
       model.compile({
         optimizer: optimizer,
         loss: "categoricalCrossentropy",
         metrics: ["accuracy"],
       });

my training and testing data

        const yTrain = tf.tensor2d(
          [10, 130, 60, 20, 150, 110, 3, 160, 99],
          [1, 9]
        );
        const xTrain = tf.tensor2d([0, 1, 0, 0, 1, 1, 0, 1, 0], [1, 9]);

        const yTest = tf.tensor2d(
          [5, 106, 33, 88, 104, 140, 7, 60, 154],
          [1, 9]
        );
        const xTest = tf.tensor2d([0, 1, 0, 0, 1, 1, 0, 0, 1], [1, 9]);

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

1 Reply

0 votes
by (71.8m points)

The inputShape is of size 2, therefore, the features (here xtrain and xtest) should be of dimension 3.

Additionnally, it does not make sense to have a dimension size to be 0 (that will mean that the tensor is empty).

Given your xtrain and xtest shape, [a, b], the inputShape, should be [b].

This shape mismatch between the model and the training data has been discussed here and there


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...