【问题标题】:TensorFlow.JS Integer Multiplication results Decimal NumberTensorFlow.JS 整数乘法结果十进制数
【发布时间】:2019-03-12 03:05:38
【问题描述】:

我刚刚开始使用 tensorflow.js,并创建了两个简单的张量并尝试对它们执行乘法运算。

由于某种原因,两个张量与浮点整数值相乘的结果导致其中一些张量是十进制数值。这发生在 Safari 浏览器版本 11.1.2 上,不知道为什么。

// Create new 1D Tensors
const data3 = tf.tensor1d([4, 6, 5, 9]);
const data4 = tf.tensor1d([5, 4, 23, 45]);

// Multipying and Chaining Print Operations
data3.mul(data4).print();

Safari Web 控制台上的输出:

张量 [20, 23.9999981, 117.3000031, 405.0000916]

【问题讨论】:

  • 看起来像一个错误。我会记录它here
  • 现在可以使用 (0.13.0)。

标签: javascript safari tensorflow.js


【解决方案1】:

这是意料之中的,因为默认 dtype 是 float32,我们将该数据作为浮点数上传到 GPU,其精度低于原生 JS。

创建张量时,必须明确告知 dtype 是 int32

data3 = tf.tensor1d([4, 6, 5, 9, 11], 'int32');
data4 = tf.tensor1d([5, 4, 23, 45, 23], 'int32');
data3.mul(data4).print();

即使输入是 float32,您也可能在 TF.js >=0.13.0 的输出中获得整数的原因是,当输入足够小时,我们开始将计算转发到 CPU,而不是在 GPU 上进行.

【讨论】:

    猜你喜欢
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多