【发布时间】: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