2

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.

2
  • wait so what exactly was the error message?? Commented Feb 21, 2020 at 1:56
  • ValueError: Error when checking : expected dense_Dense1_input to have shape [null,38] but got array with shape [38,1]. Commented Feb 21, 2020 at 2:32

1 Answer 1

1

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])
Sign up to request clarification or add additional context in comments.

2 Comments

Works fine now. Thank you.
But why is there a null in the array? [null, 38]. Shouldn't it just be [38] ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.