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

546 views
Skip to first unread message

Loreto Parisi

unread,
May 22, 2018, 6:18:10 AM5/22/18
to TensorFlow.js Discussion
I'm running the latest version of the npm module of tfjs-node, tfjs-core and this simple example:

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


// Tiny TFJS train / predict example.
function myFirstTfjs() {
   
// Create a simple model.
   
const model = tf.sequential();
    model
.add(tf.layers.dense({units: 1, inputShape: [1]}));
 
   
// Prepare the model for training: Specify the loss and the optimizer.
    model
.compile({
      loss
: 'meanSquaredError',
      optimizer
: 'sgd'
   
});
 
   
// Generate some synthetic data for training. (y = 2x - 1)
   
const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1]);
   
const ys = tf.tensor2d([-3, -1, 1, 3, 5, 7], [6, 1]);
 
   
// Train the model using the data.
    model
.fit(xs, ys, {epochs: 500});
 
   
// Use the model to do inference on a data point the model hasn't seen.
   
// Should print approximately 39.
   
var predict=
        model
.predict(tf.tensor2d([20], [1, 1]));


    console
.log( "PREDICT\n", predict );
 
}
 
  myFirstTfjs
();


Since latest update I get this strange error:


ip
-192-168-22-127:getting_started loretoparisi$ node index.js
document
is not defined
2018-05-22 12:17:45.705023: 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
document
is not defined
cpu backend was already registered
/Users/loretoparisi/Documents/Projects/AI/tensorflow-node-examples/getting_started/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-layers/dist/common.js:31
       
throw e;
       
^


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

Nikhil Thorat

unread,
May 22, 2018, 8:59:36 AM5/22/18
to Loreto Parisi, TensorFlow.js Discussion
Hi Loreto,

Can you try calling “await” on model.fit? It returns a Promise that resolves when the training is done. You may be prematurely calling predict.

--
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/de2901dc-af72-4de1-8097-0fa35df1ca97%40tensorflow.org.

Loreto Parisi

unread,
May 24, 2018, 3:22:53 AM5/24/18
to TensorFlow.js Discussion, loreto...@gmail.com
Hello, ah so it makes sense! I cannot use "await" but I could use a promise as return type then doing:


// Train the model using the data.
    model
.fit(xs, ys, {epochs: 500})

       
.then( res => {



       
// Use the model to do inference on a data point the model hasn't seen.
       
// Should print approximately 39.
       
var predict=
                model
.predict(tf.tensor2d([20], [1, 1]));


                console
.log( "PREDICT\n", predict );

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


In this case I get something different

mbploreto:getting_started loretoparisi$ node index.js
document
is not defined
2018-05-24 09:22:33.845516: 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
document
is not defined

cpu backend was already registered
(node:83127) Warning: N-API is an experimental feature and could change at any time.
Error: Argument 'indices' passed to 'gather' must be a Tensor, but got object.
    at
assert (/Users/loretoparisi/Documents/Projects/AI/tensorflow-node-examples/getting_started/node_modules/@tensorflow/tfjs/node_modules/@tensorflow/tfjs-core/dist/util.js:57:15)

Nikhil Thorat

unread,
Jun 5, 2018, 10:20:32 AM6/5/18
to Loreto Parisi, TensorFlow.js Discussion
Can you try making an int32 Tensor for your x? We recently aligned with TensorFlow python to make tf.gather take an int32 tensor.

Loreto Parisi

unread,
Jun 6, 2018, 5:55:19 AM6/6/18
to TensorFlow.js Discussion, loreto...@gmail.com
I have tried like

// Generate some synthetic data for training. (y = 2x - 1)

   
const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1], 'int32');
   
const ys = tf.tensor2d([-3, -1, 1, 3, 5, 7], [6, 1], 'int32');


but getting the same error.

Karan Shah

unread,
Jun 7, 2018, 2:47:03 AM6/7/18
to TensorFlow.js Discussion
Don't know if it would work in your case. I got the same error while using tf.js
The problem came out to be case sensitivity where I used 'tf.Variable' instead of 'tf.variable'.

The former one has to do with class notation, latter with tensor. You could check if something like this works.

Loreto Parisi

unread,
Jun 11, 2018, 2:12:13 PM6/11/18
to TensorFlow.js Discussion
Hello, my example function is the following

// Tiny TFJS train / predict example.
function myFirstTfjs() {
 
// Create a simple model.
 
const model = tf.sequential();
  model
.add(tf.layers.dense({ units: 1, inputShape: [1] }));


 
// Prepare the model for training: Specify the loss and the optimizer.
  model
.compile({
    loss
: 'meanSquaredError',
    optimizer
: 'sgd'
 
});


 
// Generate some synthetic data for training. (y = 2x - 1)

 
const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1], 'int32');
 
const ys = tf.tensor2d([-3, -1, 1, 3, 5, 7], [6, 1], 'int32');



 
// Train the model using the data.
  model
.fit(xs, ys, { epochs: 500 })

   
.then(res => {



     
// Use the model to do inference on a data point the model hasn't seen.
     
// Should print approximately 39.
     
var predict =

        model
.predict(tf.tensor2d([20], [1, 1], 'int32'));



      console
.log("PREDICT\n", predict);
   
})
   
.catch(error => {
      console
.error(error);
   
})
}

darling_kandie

unread,
Jun 11, 2018, 11:36:00 PM6/11/18
to TensorFlow.js Discussion
I have a similar problem right now on latest v.0.11.6 when I do tf.exp(SymbolicTensor), where the SymbolicTensor is the return of the tf.layer.dense.apply method

Stan Bileschi

unread,
Jun 12, 2018, 10:42:45 AM6/12/18
to TensorFlow.js Discussion
The following code (copied below) and versions worked for me on my system.  Can you try it on yours?  Thanks.

(From package-lock.json)
tfjs-core 0.11.6
tfjs-layers 0.6.6

FYI, the tf.layers.dense expects float32 input.  The same error is repeatable in the API documentation like so:


// tiny_train_predict.js
require('@tensorflow/tfjs-node');
var tf = require('@tensorflow/tfjs');
tf.setBackend('tensorflow');

inputType = 'int32'                                                                                                                                                     

// Tiny TFJS train / predict example.                                                                                                                                   
function myFirstTfjs() {
  // Create a simple model.                                                                                                                                             
  const model = tf.sequential();
  model.add(tf.layers.dense({ units: 1, inputShape: [1] }));
  // Prepare the model for training: Specify the loss and the optimizer.                                                                                                
  model.compile({
    loss: 'meanSquaredError',
    optimizer: 'sgd'
  });
  // Generate some synthetic data for training. (y = 2x - 1)                                                                                                            
  const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1], inputType);
  const ys = tf.tensor2d([-3, -1, 1, 3, 5, 7], [6, 1], inputType);
  // Train the model using the data.                                                                                                                                    
  model.fit(xs, ys, { epochs: 500 })
      .then(res => {
        // Use the model to do inference on a data point the model hasn't seen.                                                                                         
        // Should print approximately 39.                                                                                                                               
        var predict =
            model.predict(tf.tensor2d([20], [1, 1], inputType));
        console.log("PREDICT\n", predict.dataSync());
      })
      .catch(error => {
        console.error(error);
      })
}

console.log("hello world");
myFirstTfjs();


{
  "name": "parisi",
  "version": "1.0.0",
  "description": "debugging argument error",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "@tensorflow/tfjs": "^0.11.6",
    "@tensorflow/tfjs-node": "^0.1.7"
  }
}


Output was (note the inclusion of the call to "dataSync()" to get the value of the tensor.
$ node tiny_train_predict.js
2018-06-12 10:26:22.982041: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX
hello world
(node:230386) Warning: N-API is an experimental feature and could change at any time.
PREDICT
 Float32Array [ 38.95066452026367 ]

Stan Bileschi

unread,
Jun 12, 2018, 10:43:41 AM6/12/18
to TensorFlow.js Discussion

sorry broken photo.

Stan Bileschi

unread,
Jun 12, 2018, 11:02:08 AM6/12/18
to TensorFlow.js Discussion
Hi darling_kandie

I used the following code including the tf.exp function and got the following result.  Does this help address your issue?  Thanks.

require('@tensorflow/tfjs-node');
var tf = require('@tensorflow/tfjs');
tf.setBackend('tensorflow');
function myFirstTfjs() {
  const model = tf.sequential();
  model.add(tf.layers.dense({ units: 1, inputShape: [1] }));
  model.compile({
    loss: 'meanSquaredError',
    optimizer: 'sgd'
  });
  // Generate some synthetic data for training. (y = 2x - 1)                                                                                                            
  const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1]);
  const ys = tf.tensor2d([-3, -1, 1, 3, 5, 7], [6, 1]);
  // Train the model using the data.                                                                                                                                    
  model.fit(xs, ys, { epochs: 500 })
      .then(res => {
        p10 = model.predict(tf.tensor2d([5.5], [1, 1]))
        pExp10 = tf.exp(p10);
        console.log("2*5.5 - 1 SHOULD BE CLOSE TO 10");
        console.log(p10.dataSync());
        console.log("exp(10) SHOULD BE CLOSE TO 22026");
        console.log(pExp10.dataSync());
      })
      .catch(error => {
        console.error(error);
      })
}

console.log("hello world");
myFirstTfjs();



$ node tiny_train_predict.js
2018-06-12 11:00:27.261473: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX
hello world
(node:242759) Warning: N-API is an experimental feature and could change at any time.
2*5.5 - 1 SHOULD BE CLOSE TO 10
Float32Array [ 9.994277954101562 ]
exp(10) SHOULD BE CLOSE TO 22026
Float32Array [ 21900.7890625 ]


 

Loreto Parisi

unread,
Jun 12, 2018, 11:11:21 AM6/12/18
to TensorFlow.js Discussion
Hello,
so I have updated the repo with that code, but I'm still getting an error: https://github.com/loretoparisi/tensorflow-node-examples

The error was

:getting_started loretoparisi$ node index.js 

2018-06-12 17:09:45.567768: 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

cpu backend was already registered

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

Error: No data provided for "dense_Dense1_input". Need data for each key in: dense_Dense1_input

    at new ValueError (/Users/loretoparisi/Documents/Projects/AI/tensorflow-node-examples/getting_started/node_modules/@tensorflow/tfjs-layers/dist/errors.js:36:28)

    at standardizeInputData (/Users/loretoparisi/Documents/Projects/AI/tensorflow-node-examples/getting_started/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:115:23)

    at Model.standardizeUserData (/Users/loretoparisi/Documents/Projects/AI/tensorflow-node-examples/getting_started/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:628:13)

    at Model.<anonymous> (/Users/loretoparisi/Documents/Projects/AI/tensorflow-node-examples/getting_started/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:910:49)

    at step (/Users/loretoparisi/Documents/Projects/AI/tensorflow-node-examples/getting_started/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:48:23)

    at Object.next (/Users/loretoparisi/Documents/Projects/AI/tensorflow-node-examples/getting_started/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:29:53)

    at /Users/loretoparisi/Documents/Projects/AI/tensorflow-node-examples/getting_started/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:23:71

    at new Promise (<anonymous>)

    at __awaiter (/Users/loretoparisi/Documents/Projects/AI/tensorflow-node-examples/getting_started/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:19:12)

    at Model.fit (/Users/loretoparisi/Documents/Projects/AI/tensorflow-node-examples/getting_started/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:903:16)

darling_kandie

unread,
Jun 12, 2018, 11:27:51 AM6/12/18
to TensorFlow.js Discussion
Stan,

Thanks, but that does not address my issue, because I am attempting to use tf.exp in the middle of a model, where for instance X = tf.layers.dense({units: 12}).apply(input) returns a SymbolicTensor.  tf.exp(X) in this case throws an error.

In your example, the return of model.predict is going to be a regular tensor.  

I get the error that the OP got, which is "{tf.exp...} expects a Tensor, but got an Object";  the Object it refers to is that same SymbolicTensor construct.

Stan Bileschi

unread,
Jun 12, 2018, 11:33:41 AM6/12/18
to TensorFlow.js Discussion
Hello,

I was able to reproduce your stack trace locally.  It looks like the issue has to do with depending both on "@tensorflow/tfjs" and "@tensorflow/tfjs-core".  If the tfjs-core link is removed, the code works as intended.  Can you verify?   If so, we should file an issue to determine why this happens and make it clearer what is happening.

Thanks,
Stan

Stan Bileschi

unread,
Jun 12, 2018, 11:44:20 AM6/12/18
to darling_kandie, TensorFlow.js Discussion
Ah I see,

It sounds like you wish to use core TensorFlow operations in between the layers of a model?  In that case tfjs-layers will need for you to wrap your custom computation into a `Layer`, for book-keeping, etc.  I think you will need to use a custom layer for this.   See this example for how to build and use a custom layer.


--
You received this message because you are subscribed to a topic in the Google Groups "TensorFlow.js Discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/a/tensorflow.org/d/topic/tfjs/aeeX1U7TDuI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tfjs+unsubscribe@tensorflow.org.

Loreto Parisi

unread,
Jun 12, 2018, 1:18:16 PM6/12/18
to TensorFlow.js Discussion, folk...@gmail.com
That sounds strange, since it was an example of the TFJS getting started, isn't it?
To unsubscribe from this group and all its topics, send an email to tfjs+uns...@tensorflow.org.

Stan Bileschi

unread,
Jun 12, 2018, 1:27:15 PM6/12/18
to Loreto Parisi, TensorFlow.js Discussion, darling_kandie
Hi Loreto & darling_kandle,

  I think we are having two conversations here and they are getting a bit of crosstalk.   To answer your question though, the getting_started example (here) does not use core methods between the layers of the model (there is only one layer to the model).  In order to use a model with layers other than the stock layers, one would need to create a custom layer.  Directly using the output of layer.apply() returns an Object which is intended for use in the model construction.

Sincerely,
-Stan

darling_kandie

unread,
Jun 12, 2018, 2:17:34 PM6/12/18
to TensorFlow.js Discussion, loreto...@gmail.com, folk...@gmail.com
aha thanks.  

Loreto Parisi

unread,
Jun 21, 2018, 11:20:06 AM6/21/18
to TensorFlow.js Discussion, loreto...@gmail.com, folk...@gmail.com
Thanks, I get the same very error: 
Error: Argument 'x' passed to 'slice' must be a Tensor, but got object.

in this more complex example for sentiment pre-trained model here: https://github.com/loretoparisi/tensorflow-node-examples/blob/master/sentiment/sentiment.js

My guess is that I'm importing in Node.js the libraries in the wrong way:

require('@tensorflow/tfjs-node');
const tf = require('@tensorflow/tfjs');
tf.setBackend('tensorflow');

and my package.json had as dependencies:

"dependencies": {
"@tensorflow/tfjs-core": "^0.11.3",
"@tensorflow/tfjs-node": "^0.1.7"
}
Reply all
Reply to author
Forward
0 new messages