【问题标题】:Beginner : Not understanding shape [,1] on Tensorflow.js初学者:不了解 Tensorflow.js 上的形状 [,1]
【发布时间】:2019-05-27 09:36:01
【问题描述】:

我想使用 Tensorflow.js(使用 Node.js)制作一个模型以将摄氏温度转换为华氏温度。

但是,我不明白要使用什么形状

我尝试了不同的input_shape,例如[1][1,20],最后设置为[20] 我还为摄氏和华氏数组尝试了不​​同的张量形状,例如tensor(celsius)tensor([celsius])

这里是代码


var model = tf.sequential()
model.add(tf.layers.dense({inputShape:[20], units: 1}))

async function trainModel(model, inputs, labels) {
    // Prepare the model for training.  
    model.compile({
      optimizer: tf.train.adam(),
      loss: tf.losses.meanSquaredError,
      metrics: ['mse'],
    });

    const batchSize = 28;
    const epochs = 500;

    return await model.fit(inputs, labels, {
      epochs,
      shuffle: true,
    //   callbacks: tfvis.show.fitCallbacks(
    //     { name: 'Training Performance' },
    //     ['loss', 'mse'], 
    //     { height: 200, callbacks: ['onEpochEnd'] }
    //   )
    });
  }

c = tf.tensor([celsius]) // celsius = [1,2,3,4,...]
console.log(c.shape) // ==> [1,20]

f = tf.tensor([fahrenheit])
console.log(f.shape) // ==> [1,20]

trainModel(model, c, f)

此外,在 Python 教程中 input_shape[1] 。使用 Node.js,似乎只有 [20] 可以工作。

输入的形状是[1,20],它是正确的。

标签的形状也是[1,20],但会触发以下错误:

调试器说:

Error when checking target: expected dense_Dense1 to have shape [,1], but got array with shape [1,20].

- 编辑

此外,当我尝试input_shape: [1,20] 时,它给了我:

expected dense_Dense1_input to have 3 dimension(s). but got array with shape 1,20

-

我希望模型通过将 C° 值与 F° 值相关联来进行训练。

谢谢

【问题讨论】:

  • @joyzaza 给我expected dense_Dense1_input to have 3 dimension(s). but got array with shape 1,20

标签: node.js tensorflow machine-learning artificial-intelligence tensorflow.js


【解决方案1】:

错误很明显:

{inputShape:[20], units: 1}

模型包含单层。 inputShape:[20] 这意味着[null, 20] 的batchInputShape 将是第一层的形状。同样,units: 1 表示最后一层的形状为[null, 1]

使用的特征具有与模型的 batchInputShape 匹配的形状 [1, 20]。但是,形状为[1, 20] 的标签并非如此。它必须具有[1, 1] 的形状,因此会引发错误:

预计dense_Dense1 的形状为[,1],但得到的数组的形状为[1,20]

必须更改模型的单位大小以反映标签形状。

{inputShape:[20], units: 20}

【讨论】:

  • Python 教程放units=1, input_shape=[1]。我在 Node 上试过,但没有用。只有一个变量(重复不同的值)所以它不是 unit:1 和 shape:1 吗?
  • 如果你使用{inputShape:[1], units: 1},那么你的特征和标签应该有[b, 1]的形状,b是元素的数量。
猜你喜欢
  • 1970-01-01
  • 2011-02-06
  • 1970-01-01
  • 2022-11-21
  • 2020-09-21
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 2019-04-30
相关资源
最近更新 更多