Created
August 22, 2018 09:28
-
-
Save avdotion/0fe8b771b5e375899105dbd620df4ca9 to your computer and use it in GitHub Desktop.
Tensorflow.js NN-model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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