Reputation: 505
I want to make a model to convert Celsius to Fahrenheit with Tensorflow.js (with Node.js).
However, I am not understanding what shapes to use.
I have tried different input_shape
such as [1]
, [1,20]
and finally set it to[20]
I also have tried different tensor shape for the Celsius & Fahrenheit arrays such as tensor(celsius)
, tensor([celsius])
.
Here is the code
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)
Moreover, in the Python tutorial input_shape
is [1]
. With Node.js, only [20]
seems to work.
The shape of inputs is [1,20]
and it is correct.
Shape of labels is [1,20]
as well but triggers following error :
Debugger says :
Error when checking target: expected dense_Dense1 to have shape [,1], but got array with shape [1,20].
- EDIT
Furthermore, when i try input_shape: [1,20]
, it gives me :
expected dense_Dense1_input to have 3 dimension(s). but got array with shape 1,20
-
I expect the model to train by associating C° values to F° values.
Thank you
Upvotes: 1
Views: 514
Reputation: 18381
The error is clear:
{inputShape:[20], units: 1}
The model contains a single layer. inputShape:[20]
which means batchInputShape that is [null, 20]
will be the shape of the first layer. Likewise, units: 1
indicates that the last layer will have the shape [null, 1]
.
The features used have the shape [1, 20] matching therefore the batchInputShape of the model. However, that's not the case for the labels that have the shape [1, 20]
. It has to have the shape [1, 1]
therefore throwing the error:
expected dense_Dense1 to have shape [,1], but got array with shape [1,20]
The units size of the model has to be changed to reflect the labels shape.
{inputShape:[20], units: 20}
Upvotes: 1