Error when creating a model using Core API

Alaa Eddine Chouchane :

I tried to follow the tutorial on TensorFlow official site to create a machine learning model based on Core API. But I got the following error:

Error: Argument 'b' passed to 'matMul' must be a Tensor or TensorLike, but got 'null'

I am working on windows 10 and with [email protected]

const tf = require('@tensorflow/tfjs');
const tfcore = require('@tensorflow/tfjs-core')


const w1 = tf.variable(tf.randomNormal([784, 32]));
const b1 = tf.variable(tf.randomNormal([32]));
const w2 = tf.variable(tf.randomNormal([32, 10]));
const b2 = tf.variable(tf.randomNormal([10]));

function model(x) {
  console.log(w1);
  return x.matMul(w1).add(b1).relu().matMul(w2).add(b2).softmax();
}


model(tfcore);

Could you please help me with this error?

va barbosa :

as @edkeveked stated you need to supply two tensors for tf.matMul:

approach 1

const a = tf.tensor2d([1, 2], [1, 2]);
const b = tf.tensor2d([1, 2, 3, 4], [2, 2]);

a.matMul(b);

or approach 2

const a = tf.tensor2d([1, 2], [1, 2]);
const b = tf.tensor2d([1, 2, 3, 4], [2, 2]);

tf.matMul(a, b);

in your example, by passing in tfcore to model() you are using approach 2 and therefore need to pass a second tensor to matMul. however, if you pass a tensor to model() it should work like approach 1.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=29864&siteId=1