Skip to content

Instantly share code, notes, and snippets.

@avdotion
Created August 22, 2018 09:28
Show Gist options
  • Save avdotion/0fe8b771b5e375899105dbd620df4ca9 to your computer and use it in GitHub Desktop.
Save avdotion/0fe8b771b5e375899105dbd620df4ca9 to your computer and use it in GitHub Desktop.
Tensorflow.js NN-model
// This is the model
const model = tf.sequential();
// Create hidden layer
const hiddens = tf.layers.dense({
units: 4,
inputShape: [2],
activation: 'sigmoid'
});
// Add the hidden layer
model.add(hiddens);
// Create another layer
const outputs = tf.layers.dense({
units: 1,
activation: 'sigmoid'
});
model.add(outputs);
// Train it with gradient descent
model.complile({
optimizer: tf.train.sgd(0.1),
loss: tf.losses.meanSquaredError
});
const xs = tf.tensor2d([
[0, 0],
[0.5, 0.5],
[1, 1]
]);
const ys = tf.tensor2d([
[1],
[0.5],
[0]
]);
// Learning (or training if you prefer)
async function train(depth) {
for (var i = 0; i < depth; i++) {
const response = await model.fit(xs, ys, {epochs: 10, shiffle: true});
console.log(response.history.loss[0]);
}
}
// Predict presumably new data
train(100).then(() => {
console.log('training complete!');
model.predict(xs).print();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment