Reputation: 117
The code below works just fine it trains the machine to multiply any given value by 10.
What I would like to figure out is how to train it with larger numbers without receiving a NaN when it tries to print. For instance I would like to put 100 = 200 when training the bot but anything over 10 for the training input and it throws a NaN.
// Create our async function
async function runNN() {
// create model object
const model = tf.sequential();
model.add(tf.layers.dense({
units: 1,
inputShape: [1]
}));
// compile model object
model.compile({
optimizer: 'sgd',
loss: 'meanSquaredError'
});
// training datasets
// input data for training
const xs = tf.tensor([
[1],
[2],
[3],
[4],
]);
// output data for training
const ys = tf.tensor([
[10],
[20],
[30],
[40],
]);
async function firstFunction(_callback) {
// Train model with fit().method
await model.fit(xs, ys, {
epochs: 5000
})
_callback();
}
function secondFunction() {
firstFunction(function() {
// Run inference with predict() method.
model.predict(tf.tensor([
[1],
[2],
[100],
[200],
])).print();
runNN();
});
}
secondFunction()
console.log('')
}
// Call our function
runNN();
Upvotes: 0
Views: 54
Reputation: 421
If you know the range of the values that you model is supposed to be able to handle, you can just normalize the values and train the model on the normalized values. If you, for example, know that you maximum input will be 1000, then you can just divide all inputs to your model by 1000 to have only inputs in range [0, 1]. Then you use the model to predict the output value and scale the values up again.
Upvotes: 1