tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.49k stars 1.93k forks source link

model.predict expands only tensor1d input #4010

Closed kedevked closed 3 years ago

kedevked commented 4 years ago

System information

Describe the current behavior

It seems that currently model.predict in tfjs expands the input only when the input is a tensor1d

Describe the expected behavior

in python model.predict will expand the dim whenever it does not match the inputShape

Standalone code to reproduce the issue

  const model = tf.sequential();
  model.add(tf.layers.dense({units: 1, inputShape: [2, 1]}));

  model.predict(tf.tensor1d([5])).print(); // will expand the input to [1, 1]

  model.predict(tf.tensor2d([2, 1], [1, 2])).print(); // will not be expanded to [1, 2, 1] whereas this would have worked in python

Other info / logs

https://stackoverflow.com/questions/64115997/tensorflowjs-expects-3-dimensions-even-though-i-trained-it-with-2-in-python/64169813#64169813

tafsiri commented 4 years ago

Ping @caisq for thoughts on what the expected behaviour in layers should be? I've don' think I've ever relied on automatic reshaping of input so I'm surprised the first one works. I think we've generally advised users to batch all inputs (even if its a batch of one).

related to #3985

rthadur commented 3 years ago

This has been answered on stackoverflow and was accepted, closing this issue. Feel free to @mention if this is not correct, so that we can reopen the issue. Thank you

google-ml-butler[bot] commented 3 years ago

Are you satisfied with the resolution of your issue? Yes No