初学者:不理解 Tensorflow.js 上的形状 [1]

Beginner : Not understanding shape [,1] on Tensorflow.js

我想制作一个模型,使用 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° 值相关联来进行训练。

谢谢

错误很明显:

{inputShape:[20], units: 1}

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

所用特征的形状 [1, 20] 与模型的 batchInputShape 匹配。但是,对于形状为 [1, 20] 的标签,情况并非如此。它必须具有 [1, 1] 的形状,因此会抛出错误:

expected dense_Dense1 to have shape [,1], but got array with shape [1,20]

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

{inputShape:[20], units: 20}