adityaViswanatham
adityaViswanatham

Reputation: 55

TensorFlow.js: ValueError: Error when checking : expected dense_Dense1_input to have shape [null,38] but got array with shape [38,1]

I'm looking to train a model for a chat-bot and I come across this error. Any suggestions on how to fix this will be greatly appreciated. Thanks.

Code.

Setting up the Neural Network:

var model = await tf.sequential();
        model.add(tf.layers.dense({
            units: 8,
            inputShape: training[0].length
        }));
        // console.log(model);
        model.add(tf.layers.dense({
            units: 8
        }));
        model.add(tf.layers.dense({
            units: 8
        }));
        model.add(tf.layers.dense({
            units: output[0].length,
            activation: 'softmax'
        }))
        model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

        await model.fit(tf.stack(training), tf.stack(output), {
            epochs: 1000,
            batchSize: 8
        }).then(printCall => {

            // IIFE function to prompt for user input.
            (function () {
                console.log("(Type 'quit' to stop)");
                while (true) {
                    let inp = "Hi";
                    if (inp.toLowerCase() == "quit")
                        break;
                    var results = model.predict(tf.tensor(bagOfWords(inp, uniq_words)));
                    console.log(result);
                }
            })();
        })

Supporting data: training 2d array with dimensions (23, 38) output 2d array with dimensions (23, 6)

Bag of Words:

function bagOfWords(s, words) {
    var bag = [];
    for (var i = 0; i < uniq_words.length; i++) {
        bag.push(0);
    }
    var sWords = tokenizer.tokenize(s);
    var s_words = [];
    sWords.map(each => {
        s_words.push(natural.LancasterStemmer.stem(each));
    });

    for (var se in s_words) {
        for (var w in uniq_words) {
            if (uniq_words[w] == s_words[se])
                bag[w] = 1;
        }
    }
    return bag;
}

The above function bagOfWords returns a 1D array with dimensions (38, 1).

Please let me know if I can add anything more to help clarify the problem better. Thanks.

Upvotes: 2

Views: 878

Answers (1)

edkeveked
edkeveked

Reputation: 18371

The above function bagOfWords returns a 1D array with dimensions (38, 1)

It is not a 1d array. It is rather a 2d tensor.

expected dense_Dense1_input to have shape [null,38] but got array with shape [38,1]

The error is caused by a shape mismatch. Since tf.tensor(bagOfWords(inp, uniq_words)) is a tensor of shape [38, 1] whereas the model is expecting a tensor of shape [null, 38], the tensor can be reshaped into the latter shape

tf.tensor(bagOfWords(inp, uniq_words)).reshape([-1, 38])

Upvotes: 1

Related Questions