本文介绍了tensorflow如何对大于32位的整数数据进行基本数学运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用带有张量的int64,BigInt甚至float64值进行简单数学运算的最佳方法是什么?我提到我想做一个简单的数学运算,就是说我不认为用String支持张量可以解决问题(尽管此时我可以接受任何方法).通过简单的数学运算,我的意思是加,减,乘,除和比较.

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.

我是tensorflow的新手,在Chrome中使用tensorflow的1.1.2版本用于javascript.

I'm a tensorflow newbie, using version 1.1.2 of tensorflow for javascript in Chrome.

以下是 Math.PI 产生不正确的张量值的示例:

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?

类似地,使用 BigInt (Chrome支持)会引发错误:

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

我并不是真的希望它能与BigInt一起使用,但我至少希望Math.PI在不需要任何操作的情况下即可工作.

I didn't really expect it to work with BigInt, but I at least expected Math.PI to work without any manipulation necessary.

我知道这全都与tensorflow内部使用float32而不是float64的事实有关.我尝试了parseFloat(PI),但我很确定parseFloat()返回的是float64(例如,双精度).我尝试指定dtype参数,但无济于事.

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.

任何人都可以指出一个使用64或128位数值的tensorflow的示例吗?我特别喜欢使用BigInt之类的大整数值.我问题的浮点部分只是我在实验中遇到的一个相关问题.我认为这可能为处理大整数提供一些线索.

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.

谢谢

推荐答案

tensorflow.js不支持64位浮点数.

tensorflow.js does not support 64 bits floatings numbers.

这是一个简单的操作,由于使用 float32 dtype的方式出现位溢出,因此将输出不正确的结果:

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()

将dtype更改为 int32 即可解决问题.

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>

之所以起作用,是因为数据仍然可以转换为int32或float32,而不会发生位溢出.这是无法进行的转换

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()

目前,最好使用 int32 float32 进行张量操作

For now it is better to stick to int32 and float32 for tensor operations

这篇关于tensorflow如何对大于32位的整数数据进行基本数学运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:56
查看更多