【问题标题】:How does the input output tensors work for tensorflowjs layers api输入输出张量如何为 tensorflowjs 层 api 工作
【发布时间】:2019-06-23 19:37:17
【问题描述】:

我有 8 个输入和 1 个输出的分类问题。我创建了以下模型:

const hidden = tf.layers.dense({
  units: 8,
  inputShape: [58, 8, 8],
  activation: 'sigmoid'
});
const output = tf.layers.dense({
  units: 1,
  activation: 'softmax'
});

var model = tf.sequential({
  layers: [
  hidden,
  output
  ]
});

现在当我预测时

const prediction = model.predict(inputTensor);
prediction.print();

我希望这个预测有 1 个输出值,但我得到了更多,这是如何工作的?

这些是形状

console.log(input.shape) // [1, 58, 8, 8]
console.log(prediction.shape) // [1, 58, 8, 1]

输出如下所示:

   [[[[0.8124214],
       [0.8544047],
       [0.6427221],
       [0.5753598],
       [0.5      ],
       [0.5      ],
       [0.5      ],
       [0.5      ]],

      [[0.7638108],
       [0.642349 ],
       [0.5315424],
       [0.6282103],
       [0.5      ],
       [0.5      ],
       [0.5      ],
       [0.5      ]],
      ... 58 of these

【问题讨论】:

  • 可以给我们inputTensor.shapeprediction.shape吗?

标签: javascript tensorflow machine-learning tensorflow.js


【解决方案1】:

input.shape[1,58,8,8],对应如下:

  • 1 是批量大小。 More 批量大小
  • 58,8,8是网络入口中指定的inputShape

同样output.shape [1, 58, 8, 8],对应如下:

  • 1 仍然是批量大小
  • 58, 8 匹配 inputShape 的内部尺寸
  • 1 是网络值的最后一个单位。

如果只需要单位值,即形状为 [1, 1] 的层,则可以使用 tf.layers.flatten() 删除内部维度。

const model = tf.sequential();

model.add(tf.layers.dense({units: 4, inputShape: [58, 8, 8]}));
model.add(tf.layers.flatten())
model.add(tf.layers.dense({units: 1}));
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
model.fit(tf.randomNormal([1, 58, 8, 8]), tf.randomNormal([1, 1]))
model.predict(tf.randomNormal([1, 58, 8, 8])).print()

// Inspect the inferred shape of the model's output, which equals
// `[null, 1]`. The 1st dimension is the undetermined batch dimension; the
// 2nd is the output size of the model's last layer.
console.log(JSON.stringify(model.outputs[0].shape));
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.1.2/dist/tf.min.js"></script>

【讨论】:

    猜你喜欢
    • 2021-01-25
    • 2017-12-18
    • 2022-01-02
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多