Reputation: 29
I've made a game in my github repository ("Lil Square of Eight") and created some code to play against machine. Now, for study and joy purposes, I would like to create some AI with Tensorflow.js that can play the game and even win.
The game is simple, each turn a player click a square side and it's marked with player color. The player who closes the square do a score and can keep going with his turn.
I've done some tests with Tensorflow.js and I didn't understood it at all. All I could do was verify if one square has a side that can be clicked and don't give the oportunity to the other player score, or do a score itself.
const model = tf.sequential();
model.add(tf.layers.dense({ units: 16, inputShape: [8] }));
model.add(tf.layers.dense({ units: 16, inputShape: [16], activation: 'sigmoid' }));
model.add(tf.layers.dense({ units: 1, inputShape: [16] }));
model.compile({ optimizer: tf.train.adam(0.1), loss: 'meanSquaredError', lr: 0.3 });
const xs1 = tf.tensor2d([
[0, 1, 1, 1, 2, 0, 3, 0], [0, 1, 1, 0, 2, 1, 3, 0], [0, 1, 1, 0, 2, 0, 3, 1], [0, 0, 1, 1, 2, 1, 3, 0], [0, 0, 1, 1, 2, 0, 3, 1], [0, 0, 1, 0, 2, 1, 3, 1],
[0, 1, 1, 1, 2, 1, 3, 1],
[0, 0, 1, 0, 2, 0, 3, 0], [0, 1, 1, 0, 2, 0, 3, 0], [0, 0, 1, 1, 2, 0, 3, 0], [0, 0, 1, 0, 2, 1, 3, 0], [0, 0, 1, 0, 2, 0, 3, 1],
[0, 0, 1, 1, 2, 1, 3, 1], [0, 1, 1, 0, 2, 1, 3, 1], [0, 1, 1, 1, 2, 0, 3, 1], [0, 1, 1, 1, 2, 1, 3, 0]]);
await model.fit(xs1, tf.tensor2d([[0], [0], [0], [0], [0], [0], [0], [1], [1], [1], [1], [1], [2], [2], [2], [2]]), {epochs: 550});
console.log(model.predict(tf.tensor2d([[0, 1, 1, 0, 2, 0, 3, 0]])));
EDIT
As asked, I would like help in how I can create an AI with Tensorflow.js that can think and play my game in a smart way. It can learn with previuos games and start to play the game.
Upvotes: 2
Views: 2253
Reputation: 6774
What you're looking for is called Reinforcement learning. It's not as popular as most Tensorflow methods for ML. TFJS examples generally orbit around supervised learning methods.
In this case, here's my recommendation. The most popular (to me) method of game play on ML reinforcement learning, is called Q Learning. Firstly, you can research Q Learning in regular old Tensorflow and Python, then you can convert it over to TFJS once you deeply understanding the concepts. You'll be one of the first!
Here's a great series on Tensorflow AND Q Learning: https://medium.com/emergent-future/simple-reinforcement-learning-with-tensorflow-part-0-q-learning-with-tables-and-neural-networks-d195264329d0
I hope this gives you some direction!
Upvotes: 4