Forum

Division operation error with tf.js

Hi.

I'm planning to make web application to use tf.js.

I installed tensorflow.js from npm.
And I generated a sampleApp to use ares-webos-cli and added below test codes in body section.

<!-- Import TensorFlow.js -->
<script src="./node_modules/@tensorflow/tfjs/dist/tf.min.js"></script>
<script src="./node_modules/@tensorflow/tfjs-core/dist/tf-core.min.js"></script>

<!-- the main script -->
<script type="text/javascript">
console.log('tf.version : ', tf.version.tfjs);

var num1 = tf.scalar(1.0);
var num2 = tf.scalar(2.0);
var num3 = tf.scalar(10.0);
var num4 = tf.scalar(5.0);

console.log('tf.scalar ', num1.dataSync()[0], '+', num2.dataSync()[0], ': ', num1.add(num2).dataSync()[0]);
console.log('tf.scalar ', num1.dataSync()[0], '-', num2.dataSync()[0], ': ', num1.sub(num2).dataSync()[0]);
console.log('tf.scalar ', num1.dataSync()[0], '*', num2.dataSync()[0], ': ', num1.mul(num2).dataSync()[0]);
console.log('tf.scalar ', num1.dataSync()[0], '/', num2.dataSync()[0], ': ', num1.div(num2).dataSync()[0]);
console.log('tf.scalar ', num3.dataSync()[0], '/', num4.dataSync()[0], ': ', num3.div(num4).dataSync()[0]);
console.log('tf.scalar ', num3.dataSync()[0], '*', num4.dataSync()[0], ': ', num3.mul(num4).dataSync()[0]);
console.log('tf.scalar ', num1.dataSync()[0], '+', num2.dataSync()[0], ': ', num1.add(num2).dataSync()[0]);
console.log('tf.scalar ', num1.dataSync()[0], '-', num2.dataSync()[0], ': ', num1.sub(num2).dataSync()[0]);
console.log('tf.scalar ', num1.dataSync()[0], '*', num2.dataSync()[0], ': ', num1.mul(num2).dataSync()[0]);

However, I got errors during tensor division operation like below.
I expected 1 divided by 2 to get 0.5, but it got 0.
Also after division operation, tensor operations got some problems.

image

I'd like to know how to use tf.js in webOS OSE.
Any help would be greatly appreciated.

Thanks.

Hi, youngyun.

I tested your code with tf.min.js (CDN) : https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js

In my test, the results were the same as below between the chrome of windows 10 and webOS OSE.

on the chrome browser:
image

on the webOS OSE 2.6.0
image

I'm not sure, if you want to use TensorFlow with node.js, you need to import tfjs library as this link. But if you want to use TensorFlow with Script Tag & HTML, you need to refer this link.

Dear nicolas.

Thank you for your reply.
I try to test with tf.min.js from CDN like below, however division operation still is not working.

<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"> </script>
<!-- the main script -->
<script type="text/javascript">
console.log('tf.version : ', tf.version.tfjs);

var num1 = tf.scalar(1.0);
var num2 = tf.scalar(2.0);
var num3 = tf.scalar(10.0);
var num4 = tf.scalar(5.0);

console.log('tf.scalar ', num1.dataSync()[0], '+', num2.dataSync()[0], ': ', num1.add(num2).dataSync()[0]);
console.log('tf.scalar ', num1.dataSync()[0], '-', num2.dataSync()[0], ': ', num1.sub(num2).dataSync()[0]);
console.log('tf.scalar ', num1.dataSync()[0], '*', num2.dataSync()[0], ': ', num1.mul(num2).dataSync()[0]);
console.log('tf.scalar ', num1.dataSync()[0], '/', num2.dataSync()[0], ': ', num1.div(num2).dataSync()[0]);
console.log('tf.scalar ', num3.dataSync()[0], '/', num4.dataSync()[0], ': ', num3.div(num4).dataSync()[0]);
console.log('tf.scalar ', num3.dataSync()[0], '*', num4.dataSync()[0], ': ', num3.mul(num4).dataSync()[0]);
console.log('tf.scalar ', num1.dataSync()[0], '+', num2.dataSync()[0], ': ', num1.add(num2).dataSync()[0]);
console.log('tf.scalar ', num1.dataSync()[0], '-', num2.dataSync()[0], ': ', num1.sub(num2).dataSync()[0]);
console.log('tf.scalar ', num1.dataSync()[0], '*', num2.dataSync()[0], ': ', num1.mul(num2).dataSync()[0]);
</script>

I make new image based on webOS OSE 2.6.0 and update SDK today.

  • webOS OSE : 2.6.0 (webos-image-qemux86-master-20200813023734.wic.vmdk)
  • SDK : 1.12.0

However I don't understand that the results are different.

Could you share your whole test code?

Thank you.

I didn't test it on the emulator. It only tested in the RPI4.
I don't have source code now.

But, I just use below code
Please do testing using this code as an index.html

  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"> </script>


    <script type="text/javascript">
    console.log('tf.version : ', tf.version.tfjs);

    var num1 = tf.scalar(1.0);
    var num2 = tf.scalar(2.0);
    var num3 = tf.scalar(10.0);
    var num4 = tf.scalar(5.0);

    console.log('tf.scalar ', num1.dataSync()[0], '+', num2.dataSync()[0], ': ', num1.add(num2).dataSync()[0]);
    console.log('tf.scalar ', num1.dataSync()[0], '-', num2.dataSync()[0], ': ', num1.sub(num2).dataSync()[0]);
    console.log('tf.scalar ', num1.dataSync()[0], '*', num2.dataSync()[0], ': ', num1.mul(num2).dataSync()[0]);
    console.log('tf.scalar ', num1.dataSync()[0], '/', num2.dataSync()[0], ': ', num1.div(num2).dataSync()[0]);
    console.log('tf.scalar ', num3.dataSync()[0], '/', num4.dataSync()[0], ': ', num3.div(num4).dataSync()[0]);
    console.log('tf.scalar ', num3.dataSync()[0], '*', num4.dataSync()[0], ': ', num3.mul(num4).dataSync()[0]);
    console.log('tf.scalar ', num1.dataSync()[0], '+', num2.dataSync()[0], ': ', num1.add(num2).dataSync()[0]);
    console.log('tf.scalar ', num1.dataSync()[0], '-', num2.dataSync()[0], ': ', num1.sub(num2).dataSync()[0]);
    console.log('tf.scalar ', num1.dataSync()[0], '*', num2.dataSync()[0], ': ', num1.mul(num2).dataSync()[0]);
</script>
  </head>
  <body>
  </body>
</html>
```