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

时间:2019-05-27 09:36:01

标签: node.js tensorflow machine-learning artificial-intelligence 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°值关联来进行训练。

谢谢

1 个答案:

答案 0 :(得分:1)

错误已清除:

{inputShape:[20], units: 1}

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

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

  

预期density_Dense1的形状为[,1],但数组的形状为[1,20]

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

{inputShape:[20], units: 20}
相关问题