Error: Argument 'x' passed to 'slice' must be a Tensor, but got object.

991 views
Skip to first unread message

Loreto Parisi

unread,
Jun 21, 2018, 10:42:05 AM6/21/18
to TensorFlow.js Discussion
I have this simple sentiment example for node.js



require('@tensorflow/tfjs-node');
var tf = require('@tensorflow/tfjs');

var fs = require('fs');
var performance = require('perf_hooks').performance;

const model_path = 'file://' + __dirname + '/model/model.json';
const model_metadata = __dirname + '/model/metadata.json';

var text = 'this is a bad day';

tf.loadModel(model_path)
.then(model => {

let sentimentMetadata = JSON.parse(fs.readFileSync(model_metadata));
//console.log(sentimentMetadata);

let indexFrom = sentimentMetadata['index_from'];
let maxLen = sentimentMetadata['max_len'];
let wordIndex = sentimentMetadata['word_index'];

console.log('indexFrom = ' + indexFrom);
console.log('maxLen = ' + maxLen);

console.log('model_type', sentimentMetadata['model_type']);
console.log('vocabulary_size', sentimentMetadata['vocabulary_size']);
console.log('max_len', sentimentMetadata['max_len']);

const inputText =
text.trim().toLowerCase().replace(/(\.|\,|\!)/g, '').split(/\s+/g); // tokenized

console.log(inputText);

// Look up word indices.
const inputBuffer = tf.buffer([1, maxLen], 'float32');
for (let i = 0; i < inputText.length; ++i) {
const word = inputText[i];
if (typeof wordIndex[word] == 'undefined') { // TODO(cais): Deal with OOV words.
console.log(word, wordIndex[word]);
}
inputBuffer.set(wordIndex[word] + indexFrom, 0, i);
}
const input = inputBuffer.toTensor();

console.log(text, "\n", input);

const beginMs = performance.now();
console.log(model.toString())
console.log(model.predict.toString())
const predictOut = model.predict(input);
const score = predictOut.dataSync()[0];
predictOut.dispose();
const endMs = performance.now();

console.log({ score: score, elapsed: (endMs - beginMs) });



})
.catch(error => {
console.error(error)
})


that gives:

Error: Argument 'x' passed to 'slice' must be a Tensor, but got object.

    at assert (/Users/loretoparisi/Documents/Projects/AI/tfjs-examples/sentiment/node_modules/@tensorflow/tfjs-core/dist/util.js:57:15)

    at assertArgumentIsTensor (/Users/loretoparisi/Documents/Projects/AI/tfjs-examples/sentiment/node_modules/@tensorflow/tfjs-core/dist/util.js:5:5)

    at _loop_1 (/Users/loretoparisi/Documents/Projects/AI/tfjs-examples/sentiment/node_modules/@tensorflow/tfjs-core/dist/util.js:17:13)

    at Object.assertArgumentsAreTensors (/Users/loretoparisi/Documents/Projects/AI/tfjs-examples/sentiment/node_modules/@tensorflow/tfjs-core/dist/util.js:21:9)

    at SliceOps.slice (/Users/loretoparisi/Documents/Projects/AI/tfjs-examples/sentiment/node_modules/@tensorflow/tfjs-core/dist/ops/slice.js:34:14)

    at /Users/loretoparisi/Documents/Projects/AI/tfjs-examples/sentiment/node_modules/@tensorflow/tfjs-core/dist/ops/operation.js:11:61

    at Object.Tracking.tidy (/Users/loretoparisi/Documents/Projects/AI/tfjs-examples/sentiment/node_modules/@tensorflow/tfjs-core/dist/tracking.js:36:22)

    at Function.descriptor.value [as slice] (/Users/loretoparisi/Documents/Projects/AI/tfjs-examples/sentiment/node_modules/@tensorflow/tfjs-core/dist/ops/operation.js:11:26)

    at Object.SliceOps.slice2d (/Users/loretoparisi/Documents/Projects/AI/tfjs-examples/sentiment/node_modules/@tensorflow/tfjs-core/dist/ops/slice.js:23:25)

    at /Users/loretoparisi/Documents/Projects/AI/tfjs-examples/sentiment/node_modules/@tensorflow/tfjs-layers/dist/backend/tfjs_backend.js:118:28



This does not make any sense since my input is a Tensor:

 Tensor {

  isDisposedInternal: false,

  size: 100,

  shape: [ 1, 100 ],

  dtype: 'float32',

  strides: [ 100 ],

  dataId: {},

  id: 0,

  rankType: '2' }



while I get this assertion error in dist/util.js here:
function assertArgumentIsTensor(x, argName, functionName) {
assert(x instanceof tensor_1.Tensor, "Argument '" + argName + "' passed to '" + functionName + "' must be a Tensor, " +
("but got " + typeof x + "."));
}

I get an error also just doing here:

const model2 = tf.sequential({
layers: [tf.layers.dense({ units: 1, inputShape: [10] })]
});
model2.predict(tf.ones([8, 10]), { batchSize: 4 }).print();

Nikhil Thorat

unread,
Jun 21, 2018, 10:48:51 AM6/21/18
to Loreto Parisi, TensorFlow.js Discussion
I’m typing from a phone, but my best guess is you have a double import of tf (instanceof sees the Tensor type of the other tf object’s Tensor).

Can you try using an es6 import instead of require of both node and tfjs?

--
You received this message because you are subscribed to the Google Groups "TensorFlow.js Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+uns...@tensorflow.org.
Visit this group at https://groups.google.com/a/tensorflow.org/group/tfjs/.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/tfjs/e7b69a49-ae43-46ce-aaa9-1de39b4f0bfc%40tensorflow.org.

Loreto Parisi

unread,
Jun 21, 2018, 10:59:59 AM6/21/18
to TensorFlow.js Discussion, loreto...@gmail.com
Thanks! So if I use only var tf = require('@tensorflow/tfjs'); it will work only in the browser. While I if I use tf = require('@tensorflow/tfjs-node'); I will not have the core apis like .loadModel

Nikhil Thorat

unread,
Jun 25, 2018, 10:04:53 AM6/25/18
to Loreto Parisi, TensorFlow.js Discussion
Can you upload those files so I can run your code?

Loreto Parisi

unread,
Jun 25, 2018, 10:33:58 AM6/25/18
to TensorFlow.js Discussion, loreto...@gmail.com
Sure! here you are the whole example, that is mostly the same that happens here: https://github.com/loretoparisi/tensorflow-node-examples/tree/master/sentiment

Nikhil Thorat

unread,
Jun 25, 2018, 2:59:27 PM6/25/18
to Loreto Parisi, TensorFlow.js Discussion, Nick Kreeger
Thanks! Going to add Nick to take a look at this deeper when he gets back (I looked briefly but couldn't see anything obviously wrong).

Nikhil Thorat

unread,
Jun 25, 2018, 6:24:41 PM6/25/18
to Loreto Parisi, TensorFlow.js Discussion, Nick Kreeger
I looked a little deeper at your model, the first layer shape (model.input.shape) is [null, null]. I think we may have a bad error for you, but this is likely your problem.

Can you look into why your first layer is [null, null] input shape?

Loreto Parisi

unread,
Jun 26, 2018, 4:30:30 AM6/26/18
to TensorFlow.js Discussion, loreto...@gmail.com, kre...@google.com
Thanks for your help, can you point me where you get this null in the code here: https://github.com/loretoparisi/tensorflow-node-examples/blob/master/sentiment/sentiment.js, because this example is based on your browser example.
Thank you.

Daniel Smilkov

unread,
Jun 26, 2018, 8:13:09 AM6/26/18
to Loreto Parisi, TensorFlow.js Discussion, Nick Kreeger
Hi Loreto,

I got your sentiment example to work. Couple things:
- I noticed in this line you are passing the inputBuffer, but you should pass the input instead.
- In package.json change "@tensorflow/tfjs-node": "^0.1.7" to "@tensorflow/tfjs-node": "0.1.6" since I see some errors on 0.1.7 on my mac. I filed this issue.
- Remove the "postinstall": "yarn upgrade --pattern @tensorflow" in package.json, since it will overwrite 0.1.6 to 0.1.7 again.

We'll also improve the error message to be:
Argument 'x' passed to 'slice' must be a Tensor, but got TensorBuffer

Hope this helps!

Daniel


Message has been deleted
Message has been deleted

Loreto Parisi

unread,
Jun 27, 2018, 5:02:24 AM6/27/18
to TensorFlow.js Discussion, loreto...@gmail.com, kre...@google.com
Hello Daniel, thank you.
It works but there is a warning message that complains I'm not using Node.js:

ip-192-168-22-127:sentiment loretoparisi$ node sentiment.js 

2018-06-27 11:01:25.185649: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA

(node:76671) Warning: N-API is an experimental feature and could change at any time.



Hi there 👋. Looks like you are running TensorFlow.js in Node.js. To speed things up dramatically, install our node backend, which binds to TensorFlow C++, by running npm i @tensorflow/tfjs-node, or npm i @tensorflow/tfjs-node-gpu if you have CUDA. Then call require('tensorflow/tfjs-node'); (-gpu suffix for CUDA) at the start of your program. Visit https://github.com/tensorflow/tfjs-node for more details.



indexFrom = 3

maxLen = 100

model_type cnn

vocabulary_size 20000

max_len 100

[ 'this', 'is', 'a', 'bad', 'day' ]

this is a bad day 

 Tensor {

  isDisposedInternal: false,

  size: 100,

  shape: [ 1, 100 ],

  dtype: 'float32',

  strides: [ 100 ],

  dataId: {},

  id: 22,

  rankType: '2' }

{ score: 0.018475884571671486, elapsed: 1594.1342370510101 }

Nikhil Thorat

unread,
Jun 27, 2018, 11:12:47 AM6/27/18
to Loreto Parisi, TensorFlow.js Discussion, Nick Kreeger
The warning is just letting you know that you can use the full power of TensorFlow, if you install the @tensorflow/tfjs-node package. It's not a fatal error.

Loreto Parisi

unread,
Jun 28, 2018, 5:47:44 PM6/28/18
to Nikhil Thorat, TensorFlow.js Discussion, kre...@google.com
Hey Nikhil, I'm using @tensorflow/tfjs-node in my example, so I argue that this warning should not be there :)
--
Dott. Ing. Loreto Parisi
Parisi Labs

Company: in...@parisilabs.com

Roie Schwaber-Cohen

unread,
Jul 3, 2018, 7:27:36 PM7/3/18
to TensorFlow.js Discussion, loreto...@gmail.com, kre...@google.com
I'm running into the same issue here:
In node, using the latest build, I load mobilnet, and then I use a canvas to load the image in.
 
let path =
let buffer = await fetch(path).then(res => res.buffer())
let img = new Image()
img.src = buffer

await ctx.drawImage(img, 0, 0, 224, 224)

const tensor = tf.fromPixels(canvas)
const expanded = tensor.expandDims(0)

const prediction = mobilenet.predict(expanded)
console.log(prediction)

The tensor looks ok:

Tensor {
  isDisposedInternal: false,
  size: 150528,
  shape: [ 1, 224, 224, 3 ],
  dtype: 'int32',
  strides: [ 150528, 672, 3 ],
  dataId: {},
  id: 2,
  rankType: '4' 
}

But the prediction fails with

Error: Argument 'x' passed to 'slice' must be a Tensor, but got object.


Nikhil Thorat

unread,
Jul 9, 2018, 3:18:47 PM7/9/18
to roie....@gmail.com, TensorFlow.js Discussion, Loreto Parisi, Nick Kreeger
I think the issue is you have multiple copies of TensorFlow.js on the page -- you should try to make sure you're not including it twice.

I'm also preparing a fix so we allow this behavior, this PR is pending: https://github.com/tensorflow/tfjs-core/pull/1146

Loreto Parisi

unread,
Jul 10, 2018, 5:05:09 AM7/10/18
to TensorFlow.js Discussion, roie....@gmail.com, loreto...@gmail.com, kre...@google.com
Great!!! Thanks a lot it's going to be a great framework!

Daniel Smilkov

unread,
Jul 12, 2018, 10:36:38 PM7/12/18
to Loreto Parisi, TensorFlow.js Discussion, roie....@gmail.com, Nick Kreeger
FYI, Nikhil's fix is in the latest tfjs v0.12.2 - before you and test the fix, make sure to remove lock files and node_modules just in case to avoid problems with caching.

Daniel


Loreto Parisi

unread,
Jul 13, 2018, 1:53:55 PM7/13/18
to TensorFlow.js Discussion, loreto...@gmail.com, roie....@gmail.com, kre...@google.com
Yes! Thank you I have upgraded to the latest tfjs-core, tfjs-node and now everything works properly!

Nikhil Thorat

unread,
Jul 13, 2018, 2:18:35 PM7/13/18
to Loreto Parisi, TensorFlow.js Discussion, Roie Schwaber-Cohen, Nick Kreeger
Reply all
Reply to author
Forward
0 new messages