首页 > 解决方案 > -1 在张量输入的形状中意味着什么?

问题描述

我有一个 TensorFlowJS 模型,其输入如下所示:

{"name":"dense_3_input","shape":[-1,25],"dtype":"float32"}

-1 是什么意思?

构建模型的方式是使用Dense(1, input_dim=25, activation="sigmoid"),所以我不知道 -1 来自哪里或如何正确创建它正在寻找的张量。

如果我通过一个张量

tf.tensor([0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1])

我得到这个错误。

Error: The shape of dict['dense_3_input'] provided in model.execute(dict) must be [-1,25], but was [25]

当传递上述 25 0/1 的输入时,该模型在 python 中正常工作。转换为 TensorFlowJS 模型是否无法正常工作?任何见解将不胜感激。

标签: tensorflowtensorflow.jstensorflowjs-converter

解决方案


张量维度中的 -1 意味着该维度的大小将根据其他维度计算。张量形状应该是其他维度大小的乘积的倍数才能起作用

tensor size: 25, shape [-1, 25] => [1, 25]

                 shape [-1, 5] => [5, 5]

                 shape [-1, 3] => will not work

当我们不知道张量的大小但知道它将是某些值的倍数时,它很有用。

在问题的示例中,初始张量可以重新整形:

  • tf.tensor([0, 1...]).reshape([-1, 25])或者

  • 它可以直接构造为二维张量tf.tensor([[0, 1...]])


推荐阅读