问题
What's the best way to do simple math using int64, BigInt or even float64 values with tensors? I mention that I want to do simple math to say that I don't think that backing the tensor with a String will do the trick (though I'm open to anything at this point). By simple math, I mean add, subtract, multiply, divide and compare.
I'm a tensorflow newbie, using version 1.1.2 of tensorflow for javascript in Chrome.
Here's an example of Math.PI yielding an incorrect tensor value:
Math.PI; // 3.141592653589793
tf.scalar(Math.PI).toString(); // "Tensor 3.1415927410125732"
// differs here: ^^^^^^^^^^
let big = 100000000000;
tf.scalar(big).toString(); // "Tensor 99999997952" // hmmmmm?
Similarly, using BigInt (which Chrome supports), throws an error:
tf.scalar(BigInt(42).toString()).toString(true); // OK, but I can't do math with Strings
tf.scalar(BigInt(42) ).toString(true);
// Error: values passed to tensor(values) must be a number/boolean/string or an array of numbers/booleans/strings, or a TypedArray
I didn't really expect it to work with BigInt, but I at least expected Math.PI to work without any manipulation necessary.
I know this is all related to the fact that tensorflow internally uses float32 instead of float64. I tried parseFloat(PI), but I'm pretty sure that parseFloat() returns a float64(e.g. double precision). I tried specifying the dtype parameter, but to no avail.
Can anyone point me to an example of tensorflow being used with 64 or 128 bit numeric values? I'm specifically interested in using large integer values such as BigInt. The floating point part of my question was just a related issue that I encountered as I was experimenting. I thought it might provide some clues for handling large integers.
Thanks
回答1:
tensorflow.js does not support 64 bits floatings numbers.
Here is a simple operation that will output an incorrect result because of bit overflow alow the way by using float32
dtype:
tf.scalar(12045).mul(tf.scalar(12045)).print()
Changing the dtype to int32
and the problem is solved.
console.log(12045 * 12045)
tf.scalar(12045).mul(tf.scalar(12045)).print() // wrong output because of dtype
console.log(tf.getBackend())
tf.scalar(12045, 'int32').mul(tf.scalar(12045, 'int32')).print()
<html>
<head>
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"> </script>
</head>
<body>
</body>
</html>
tf.scalar(BigInt(42).toString()).toString(true)
It works because the data can still be converted to int32 or float32 without bits overflow. Here is this conversion that will not work
tf.scalar(BigInt(42455555555555555555544).toString()).print()
For now it is better to stick to int32
and float32
for tensor operations
来源:https://stackoverflow.com/questions/57334172/how-can-tensorflow-do-basic-math-with-integer-data-larger-than-32-bits