【问题标题】:Tensorflow.js inputShape doesn't match model inputTensorflow.js inputShape 与模型输入不匹配
【发布时间】:2019-04-18 00:10:28
【问题描述】:

这似乎很基本,但我无法弄清楚。

所以我有样本/数据/输入,它是一个 10 个整数数组的数组,输出/标签只是一个整数数组。

让我解释一下,可能是我的数据结构不正确。基于 10 个整数的输入,我告诉模型结果是标签/输出中的第 1 个整数。

除此之外,我无法对数据进行批处理,因为它们是连续的。这意味着输入向右移动 1,因此 sample[i+1] 中的前 9 个整数是 sample[i] 的最后 9 个整数加上一个新整数。

这是我的编码方式。

let labels = [1,0,0...]

let samples = [[0,1,1,0,1,0,1,1,1,0], ...]

基本上是 10 个数组的数组。

const model = tf.sequential();
let input = tf.tensor2d(samples);
model.add(tf.layers.dense({ units: 10, batchInputShape: [1, 10], activation: "sigmoid" }));
model.add(tf.layers.dense({ units: 1, activation: "softmax" }));
model.summary();
model.compile({ loss: "meanSquaredError", optimizer: "sgd", metrics: ["accuracy"] });
model.fit(labels, input, { batchSize: 1, shuffle: false, verbose: 1 });

当我尝试这个或任何其他输入组合时,我得到以下内容

UnhandledPromiseRejectionWarning: Error: Error when checking model input: the Array of Tensors that you are passing to your model is not the size the model expected. Expected to see 1 Tensor(s), but instead got the following list of Tensor(s): 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0

提前感谢您的帮助。

【问题讨论】:

  • 最后只需要切换输入和标签

标签: node.js tensorflow tensorflow.js


【解决方案1】:

inputShape 与模型输入不匹配

错误表示模型的输入与数据的形状不匹配。

如何解决这个问题?

  • 特征应具有与batchInputShape 相同的形状,或者应比InputShape 高一维。 batchInputShape 的第一个维度通常为 null,以允许在训练期间使用不同的 batchInputShape。但是通过在此处指定为 1(与 null 不同),训练的特征应该恰好具有等于 1 的第一个维度。如果它为 null,则可能具有形状为 [b, .. .InputShape]

  • 标签的形状应为[b, LastLayerUnit]。同样,通过指定批次的硬编码值(与 null 不同),标签的第一个维度应该恰好是该长度。

什么是批次维度?

here 是理解它的有趣答案。简单地说给定一个模型。以下允许训练模型:

model.fit(features, label)

features 是一个特征数组,而特征是我们要从中进行预测的一个元素。因此,批量大小是该数组的长度。模型的第一层可以有参数 inputShape 或 batchInputShape 或两者都有,因为知道 batchInputShape 将优先于 inputShape。当仅提供 inputShape 时,batchInputShape = [null, ...InputShape],因此表明我们可以使用不同长度的特征元素拟合模型,前提是标签具有相同长度的标签,这对于标签需要为每个功能提供。

因此

      inputShape = batchInputShape[1:] // python notation 
      inputShape = batchInputShape.slice(1) // js notation

无论如何,特征应该具有与inputShape 相同的形状。

const model = tf.sequential();
model.add(tf.layers.dense({ units: 10, batchInputShape: [1, 10], activation: "sigmoid" }));
model.add(tf.layers.dense({ units: 1, activation: "softmax" }));
model.summary();
model.compile({ loss: "meanSquaredError", optimizer: "sgd", metrics: ["accuracy"] });


const arr = Array.from({length: 10}, (_, k) => k+1 )
const features =  tf.tensor(arr, [1, 10])
const labels = tf.tensor([1], [1, 1])

logs = await model.fit(features, labels, { batchSize: 1, shuffle: false, verbose: 1 });
console.log(logs)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-30
    • 2023-04-08
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 2017-10-17
    相关资源
    最近更新 更多