Skip to content

Instantly share code, notes, and snippets.

@draganczukp
Created April 8, 2019 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save draganczukp/e04008c9c8c1f3faa3d0521494a15636 to your computer and use it in GitHub Desktop.
Save draganczukp/e04008c9c8c1f3faa3d0521494a15636 to your computer and use it in GitHub Desktop.
const tf = require("@tensorflow/tfjs");
require('@tensorflow/tfjs-node');
const model = tf.sequential();
const inputs = [
[0,0],
[0,1],
[1,0],
[1,1]
];
const targets = [
[0],
[1],
[1],
[1],
];
let intervID;
model.add(tf.layers.dense({
units: 1,
inputShape: [2],
activation: 'sigmoid'
}));
// model.add(tf.layers.dense({
// units: 1,
// });
model.compile({
optimizer: 'sgd',
loss: 'meanSquaredError'
});
const x: tf.Tensor = tf.tensor(inputs);
const y: tf.Tensor = tf.tensor(targets);
const train = () => model.fit(x, y, {
shuffle: true,
epochs: 10
});
const trainModel = async ()=>{
train()
.then(result => {
let loss = result.history.loss[0];
console.log(loss)
if(loss < 0.01){
clearInterval(intervID);
predict();
}
})
.catch(console.error);
}
const predict = () => {
model.predict(inputs).print();
}
setInterval(trainModel, 100);
// trainModel();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment