tensorflow - -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 模型是否无法正常工作?任何见解将不胜感激。
解决方案
张量维度中的 -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...]])
推荐阅读
- powershell - Azure Batch 需要更新到 PowerShell 版本 7+
- r - 如何自定义我的 flexdashboard 的主题?
- c# - 如何修复 Enumerable.ToDictionary 中发生的 ArgumentNullException(或 NullReferenceException?)?(C# ASP.NET MVC)
- docker-compose - 链码提交失败 - 错误:tls:第一条记录看起来不像 TLS 握手 remoteaddress=VM_IPADDRESS:9051
- javascript - 在实时播放器中显示自定义错误消息
- gcc - 在 slurm 节点的 .sh 文件中使用 gcc 编译时禁用警告
- visual-studio-code - 如何更改 VS Code 右上角的背景颜色?
- javascript - 有没有办法将 1000000 格式化为 100 万美元等等?
- arrays - 确保数组中每个类型的键至少有一个对象
- javascript - 是否可以在 useRef() 中的初始值元素上使用 document.getElementById?