tensorflow / tfjs

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

Tensor.pad does not work with empty tensor on node backend #3757

Closed brysgo closed 4 years ago

brysgo commented 4 years ago

To get help from the community, we encourage using Stack Overflow and the tensorflow.js tag.

TensorFlow.js version

2.1.0

Browser version

Node 14.4.0

Describe the problem or feature request

Would expect an empty tensor to get padded like any other, it instead throws an exception. I wrote this as a work around and in my tests while writing this I wrote some tests to expect that tensorflowjs throws an error. Lo-and-behold, when I switched backends, my test failed because tfjs didn't actually throw an error, it behaved exactly as my shim did. So I figured I'd share what I learned in case it is something we want to fix.

export function pad(x, paddings, constantValue = 0) {
  return tf.tidy(() => {
    if (x.shape.flat().every((n) => n > 0)) {
      return tf.pad(x, paddings, constantValue);
    } else {
      return tf.fill(
        tf.tensor(paddings).sum(-1).add(tf.tensor(x.shape)).arraySync(),
        constantValue
      );
    }
  });
}

Code to reproduce the bug / link to feature request

https://github.com/tensorflow/tfjs/pull/3756

brysgo commented 4 years ago

I'm going to close this because while there was a difference in behavior of the back ends, it was ultimately user error since the length of the paddings did not match the rank of the tensor.

Sorry for the noise, was on the tail end of a lot of sleepless nights.

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

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