Search code examples
tensorflowtensorflow.js

Tensorflow.js model for text error: the Array of Tensors that you are passing to your model is not the size the the model expected. Expected to see 1


Error: Uncaught (in promise) Error: Error when checking model : the Array of Tensors that you are passing to your model is not the size the the model expected. Expected to see 1 Tensor(s), but instead got 150 Tensors(s).

async function load_model() {
    let m = await tf.loadLayersModel(chrome.runtime.getURL(MODEL_URL));
    return m;
}

let model = load_model();
let text = "sample text";
model.then(function (res) {
    const vocabulary = "abcdefghijklmnopqrstuvwxyz0123456789".split("");
    const trimmed = text.toLowerCase().replace(/(\.|\,|\!)/g, '').split('');
    const sequence = trimmed.map(c => {
        const wordIndex = vocabulary.indexOf(c)+1.0;
        if (wordIndex < 0) {
        return 38; //oov_index
        }
        return wordIndex;
    });
    sequence.length =150;
    let padded_input = Array.from(sequence, v => {
        if (v == null) 
            return 0.0; 
        return v;
    });
    console.log(padded_input);
    const prediction = res.predict(padded_input);
    console.log(prediction);
}, function (err) {
    console.log(err);
});

If I want to use fit function, what should I do with tensors I got?

const prediction = res.fit(tf.stack(padded_input), text);

This has an error: Error: You must compile a model before training/testing. Use LayersModel.compile(modelCompileArgs).


Solution

  • Solved by reshaping tensor.

    res.predict(tf.tensor(padded_input).reshape([1,-1]));