Skip to content

Instantly share code, notes, and snippets.

@kotobuki
Last active August 15, 2018 02:18
Show Gist options
  • Save kotobuki/1463996812b7b38f5c06c36a81923b58 to your computer and use it in GitHub Desktop.
Save kotobuki/1463996812b7b38f5c06c36a81923b58 to your computer and use it in GitHub Desktop.
XOR
// Reference
// https://github.com/mattcam/tfjs_xor_example
const model = tf.sequential();
model.add(tf.layers.dense({ units: 10, activation: "sigmoid", inputShape: [2] }));
model.add(tf.layers.dense({ units: 1, activation: "sigmoid" }));
model.compile({ optimizer: "rmsprop", loss: "meanSquaredError" });
const trainingData = tf.tensor2d([[0, 0], [0, 1], [1, 0], [1, 1]]);
const targetData = tf.tensor2d([[0], [1], [1], [0]]);
console.log("Learning...");
// Let's change the value of epochs and see what happens
// epochs: The number of times to iterate over the training data arrays
// epochsの値を変更して何が起こるかみてみよう
// epochs:トレーニング用データ配列を反復処理する回数
model.fit(trainingData, targetData, { epochs: 100 }).then(history => {
console.log("Finished!");
console.log("loss = " + history.history.loss[0]);
model.predict(trainingData).print();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/tensorflow/0.12.5/tf.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment