Answers.Body: <p>The basis of reinforcement learning methods is to give each (game) state (or action) a value that somehow represents how good that state (or action) is. To store these values we could use something as simple as a table/hashmap, however complex games like chess or go has so many states they cannot fit into the memory. As a remedy, we think of the hashmap as a function and try to approximate it with a neural network(NN). Luckily NNs are universal approximators, which means <a href="http://neuralnetworksanddeeplearning.com/chap4.html" rel="nofollow noreferrer">they can learn any function</a>, including an arbitrary mapping from a chessboard to a number.</p>
  <p>Now the question is how to represent a game board and feed it to the neural network. In the case of tic-tac-toe, all 5 methods you listed can be considered correct.</p>
  <p>Theoretically it does not matter what <code>(w, b, v)</code> numbers are assigned to (white, black or vacant tiles), if we teach the NN that <code>f(w, w, v, ...) = 1</code> enough times, it will learn this association whether it is <code>(w, b, v) = (0, -1, 1)</code> or <code>(w, b, v) = (0, 0.5, 1)</code>.</p>
  <p>Now your first three examples use this method, however, a small <em>flaw</em> here is that it assigns numbers to nominal things, that is numbers can be ordered, yet we cannot really say that <code>black &gt; white &gt; vacant</code>. Your last two examples try to fix this by using <a href="https://machinelearningmastery.com/why-one-hot-encode-data-in-machine-learning/" rel="nofollow noreferrer">one-hot-vectors</a>.</p>
  <p>So for a game like chess, if we used numbers simply to represent the figures the NN might mistakenly mix up two figure types (eg.: a pawn is <code>19</code>, the queen is <code>20</code> and it thinks that a queen is trying to attack your king whereas it is just a pawn) and make a bad decision. However, it will learn that the decision was bad and will assign the correct value to the state and the decision in the long run.</p>
  <p>One last note: choosing the correct state representation for a problem is a crucial part of reinforcement learning (similar to picking the right features for a classification problem) and sometimes one might be too afraid to pick a very high dimensional state space. But remember, that chess is not a simple game so a large state space may not be unreasonable. Also for reference, <a href="https://www.nature.com/articles/nature14236.pdf" rel="nofollow noreferrer">Atari games</a> were trained with an input dimension of <code>84*84*4</code>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2017-11-24T15:48:53.553Z
Answers.LastActivityDate: 2021-10-01T08:19:02.813Z
Answers.LastEditDate: 2021-10-01T08:19:02.813Z
Answers.UserDisplayName: Hai Nguyen
Answers.Body: <p><em><strong>Representation of states</strong></em> is very important to prepare the data for the neural networks. You can try a different way and pick which fit best in your case.</p>
  <ul>
  <li>You can use 18 neurons as input where each state is represented by the 2 bits. But avoid 0 and 1 if you are using sigmoid activation function, which can cause saturation at the output, which means if the output(y) becomes 1 at any layer, then on backpropagating error, we have <strong>y (1-y) dE/dy</strong> in weight update part, which become zero with the saturation, which means it will stay in the same state ever.</li>
  </ul>
  <p>This problem can be solved by the following method:</p>
  <p><strong>Solution 1.</strong> You can initialize the input with some margin from 0 and 1. <strong>For example</strong> input can be [0.1, 0.9] instead for [0, 1].</p>
  <p><strong>Solution 2.</strong> Another you can initialize weights very small in the range of [-0.01,  0.01].</p>
  <p><strong>Solution 3.</strong> You can use the regularization technique, whose purpose is to suppress the weights by adding a penalizing term in error.</p>
  <ul>
  <li>To handle variance problems, you can augment some data, for proper training. Because, in tic-tac-toe, you have a small data set. To augment data, you can add some margin of range -0.1 to +0.1 in inputs with the same outputs.</li>
  </ul>
  <p>I hope this may be helpful.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-17T02:39:11.737Z
Answers.LastActivityDate: 2021-10-02T22:45:10.683Z
Answers.LastEditDate: 2021-10-02T22:45:10.683Z
Answers.UserDisplayName: Ankish Bansal
Answers.Body: <p>When you are working with neural networks, as long as the data is there, the neural network is usually able to learn how to process it into a useful result.</p>
  <p>However, you usually also want to keep the number of weights to a minimum. When you use extra weights, it will take longer to train the network because you need to tune even more values for an optimal network.</p>
  <p>So, for tic-tac-toe, any of your solutions involving 9 inputs should work just fine.</p>
  <p>Also, it helps if you keep the inputs between 0 and 1 if you are using log sigmoid, and -1 and 1 if you are using hyperbolic tangent for your activation function. You can probably easily figure out what to use for other activation functions. You can take your data and transform it into another dataset with values within a specific range through a process called range normalization.</p>
  <p>For chess, you can simply encode every piece in several different ways, and it will probably not make that much of a difference.</p>
  <p>The general rule of thumb is <strong>you want to minimize the number of weights while still giving the most possible variables to the neural network</strong>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-24T02:19:53.133Z
Answers.Text: Does not creating a fully connected layer help alleviate the issue with the amount of weights that you're describing?
  
  Like the network structure in https://arxiv.org/pdf/1509.01549.pdf (page 21)
Answers.UserDisplayName: NeomerArcana
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-24T02:53:53.417Z
Answers.Text: @NeomerArcana that may work, but you are still not getting the most efficient encoding. It would be better to just have 9 inputs, rather than have 18 in a non fully connected layer.
Answers.UserDisplayName: Aiden Grossman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-24T03:13:12.080Z
Answers.Text: and the decision to use `-1`, `0`, `1` vs `0`, `0.5`, `1` is determined by the activation function in use?
Answers.UserDisplayName: NeomerArcana
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-24T03:13:35.983Z
Answers.Text: @NeomerArcana yes.
Answers.UserDisplayName: Aiden Grossman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-24T03:15:45.300Z
Answers.Text: Does it matter if it's `-1` is opponent, `0`  free and `1` your own? As in, could `-1` be free, `0` your own and `1` as your opponent work? I'm guessing it would so long as it's always consistent?
Answers.UserDisplayName: NeomerArcana
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-24T04:16:42.403Z
Answers.Text: @NeomerArcana that would work.
Answers.UserDisplayName: Aiden Grossman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2017-11-24T00:25:09.480Z
Answers.LastActivityDate: 2021-11-06T23:05:29.643Z
Answers.LastEditDate: 2021-11-06T23:05:29.643Z
Answers.UserDisplayName: Aiden Grossman
Body: <p>I've been reading a lot about TD-Gammon recently as I'm exploring options for AI in a video game I'm making. The video game is a turn-based positional sort of game, i.e. a &quot;units&quot;, or game piece's, position will greatly impact its usefulness in that board state.</p>
  <p>To work my way towards this, I thought it prudent to implement a Neural Network for a few different games first.</p>
  <p>The idea I like is encoding the board state for the Neural Network with a single output neuron, which gives that board state relative strength compared to other board states. As I understand, this is how TD-Gammon worked.</p>
  <p>However, when I look at other people's code and examples/tutorials, there seems to be a lot of variance in the way they represent the board state. Even for something as simple as tic-tac-toe.</p>
  <p>So, specifically, for tic-tac-toe, which is a better, or what is the correct representation for the board state?</p>
  <p>I have seen:</p>
  <ol>
  <li><p>9 input neurons, one for each square. A <code>0</code> indicates a free-space, <code>-1</code> the opponent, and <code>1</code> yourself.</p>
  </li>
  <li><p>9 input neurons, but using different values, such as <code>0</code> for the opponent, <code>0.5</code> for free, and <code>1</code> for yourself?</p>
  </li>
  <li><p>Could you use larger values? Like <code>0</code>, <code>1</code> and <code>2</code>?</p>
  </li>
  <li><p>27 input neurons. The first 3 being square 1, the next 3 being square 2, etc. Every neuron is <code>1</code> or <code>0</code>. The first of the set of three indicates whether this square is free or not; the second indicates whether the square is occupied by your opponent or not. In the end, only one in every 3 neurons will have a <code>1</code>, the other two will have a <code>0</code>.</p>
  </li>
  <li><p>18 input neurons. The first being <code>1</code> for the X player, the second being <code>1</code> for the O player, and both being <code>0</code> for a blank</p>
  </li>
  </ol>
  <p>Then, when branching into games where the specific pieces' abilities come into play, like in chess, how would you represent this?</p>
  <p>Would it be as simple as using higher input values for more valuable pieces? I.e. <code>-20</code> for an opponents Queen and <code>+20</code> for your own queen? Or would you need something more complex where you define 10+ values for each square, one for each unit-type and player combination?</p>
  
Tags: <neural-networks><ai-design><tic-tac-toe><board-games><state-representations>
Title: How should I represent the input to a neural network for the games of tic-tac-toe, checkers or chess?
UserDisplayName: NeomerArcana
ThreadText: # How should I represent the input to a neural network for the games of tic-tac-toe, checkers or chess?
  
  (Asked by: username_0 on 2017-11-23)
  
  I've been reading a lot about TD-Gammon recently as I'm exploring options for AI in a video game I'm making. The video game is a turn-based positional sort of game, i.e. a "units", or game piece's, position will greatly impact its usefulness in that board state.
  
  To work my way towards this, I thought it prudent to implement a Neural Network for a few different games first.
  
  The idea I like is encoding the board state for the Neural Network with a single output neuron, which gives that board state relative strength compared to other board states. As I understand, this is how TD-Gammon worked.
  
  However, when I look at other people's code and examples/tutorials, there seems to be a lot of variance in the way they represent the board state. Even for something as simple as tic-tac-toe.
  
  So, specifically, for tic-tac-toe, which is a better, or what is the correct representation for the board state?
  
  I have seen:
  
  1. 9 input neurons, one for each square. A `0` indicates a free-space, `-1` the opponent, and `1` yourself.
  2. 9 input neurons, but using different values, such as `0` for the opponent, `0.5` for free, and `1` for yourself?
  3. Could you use larger values? Like `0`, `1` and `2`?
  4. 27 input neurons. The first 3 being square 1, the next 3 being square 2, etc. Every neuron is `1` or `0`. The first of the set of three indicates whether this square is free or not; the second indicates whether the square is occupied by your opponent or not. In the end, only one in every 3 neurons will have a `1`, the other two will have a `0`.
  5. 18 input neurons. The first being `1` for the X player, the second being `1` for the O player, and both being `0` for a blank
  
  Then, when branching into games where the specific pieces' abilities come into play, like in chess, how would you represent this?
  
  Would it be as simple as using higher input values for more valuable pieces? I.e. `-20` for an opponents Queen and `+20` for your own queen? Or would you need something more complex where you define 10+ values for each square, one for each unit-type and player combination?
  
  **Tags:** neural-networks, ai-design, tic-tac-toe, board-games, state-representations
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_3 on 2017-11-24. Score: 5 | Accepted answer
  
  When you are working with neural networks, as long as the data is there, the neural network is usually able to learn how to process it into a useful result.
  
  However, you usually also want to keep the number of weights to a minimum. When you use extra weights, it will take longer to train the network because you need to tune even more values for an optimal network.
  
  So, for tic-tac-toe, any of your solutions involving 9 inputs should work just fine.
  
  Also, it helps if you keep the inputs between 0 and 1 if you are using log sigmoid, and -1 and 1 if you are using hyperbolic tangent for your activation function. You can probably easily figure out what to use for other activation functions. You can take your data and transform it into another dataset with values within a specific range through a process called range normalization.
  
  For chess, you can simply encode every piece in several different ways, and it will probably not make that much of a difference.
  
  The general rule of thumb is **you want to minimize the number of weights while still giving the most possible variables to the neural network**.
  
  > **Comments:**
  > **username_0**: Does not creating a fully connected layer help alleviate the issue with the amount of weights that you're describing?  Like the network structure in https://arxiv.org/pdf/1509.01549.pdf (page 21)  
  > **username_3**: @username_0 that may work, but you are still not getting the most efficient encoding. It would be better to just have 9 inputs, rather than have 18 in a non fully connected layer.  
  > **username_0**: and the decision to use `-1`, `0`, `1` vs `0`, `0.5`, `1` is determined by the activation function in use?  
  > **username_3**: @username_0 yes.  
  > **username_0**: Does it matter if it's `-1` is opponent, `0`  free and `1` your own? As in, could `-1` be free, `0` your own and `1` as your opponent work? I'm guessing it would so long as it's always consistent?  
  > **username_3**: @username_0 that would work.  
  
  ---
  
  ### Answer by username_1 on 2017-11-24. Score: 3
  
  The basis of reinforcement learning methods is to give each (game) state (or action) a value that somehow represents how good that state (or action) is. To store these values we could use something as simple as a table/hashmap, however complex games like chess or go has so many states they cannot fit into the memory. As a remedy, we think of the hashmap as a function and try to approximate it with a neural network(NN). Luckily NNs are universal approximators, which means [they can learn any function](http://neuralnetworksanddeeplearning.com/chap4.html), including an arbitrary mapping from a chessboard to a number.
  
  Now the question is how to represent a game board and feed it to the neural network. In the case of tic-tac-toe, all 5 methods you listed can be considered correct.
  
  Theoretically it does not matter what `(w, b, v)` numbers are assigned to (white, black or vacant tiles), if we teach the NN that `f(w, w, v, ...) = 1` enough times, it will learn this association whether it is `(w, b, v) = (0, -1, 1)` or `(w, b, v) = (0, 0.5, 1)`.
  
  Now your first three examples use this method, however, a small *flaw* here is that it assigns numbers to nominal things, that is numbers can be ordered, yet we cannot really say that `black > white > vacant`. Your last two examples try to fix this by using [one-hot-vectors](https://machinelearningmastery.com/why-one-hot-encode-data-in-machine-learning/).
  
  So for a game like chess, if we used numbers simply to represent the figures the NN might mistakenly mix up two figure types (eg.: a pawn is `19`, the queen is `20` and it thinks that a queen is trying to attack your king whereas it is just a pawn) and make a bad decision. However, it will learn that the decision was bad and will assign the correct value to the state and the decision in the long run.
  
  One last note: choosing the correct state representation for a problem is a crucial part of reinforcement learning (similar to picking the right features for a classification problem) and sometimes one might be too afraid to pick a very high dimensional state space. But remember, that chess is not a simple game so a large state space may not be unreasonable. Also for reference, [Atari games](https://www.nature.com/articles/nature14236.pdf) were trained with an input dimension of `84*84*4`.
  
  
  ---
  
  ### Answer by username_2 on 2018-06-17. Score: 2
  
  ***Representation of states*** is very important to prepare the data for the neural networks. You can try a different way and pick which fit best in your case.
  
  * You can use 18 neurons as input where each state is represented by the 2 bits. But avoid 0 and 1 if you are using sigmoid activation function, which can cause saturation at the output, which means if the output(y) becomes 1 at any layer, then on backpropagating error, we have **y (1-y) dE/dy** in weight update part, which become zero with the saturation, which means it will stay in the same state ever.
  
  This problem can be solved by the following method:
  
  **Solution 1.** You can initialize the input with some margin from 0 and 1. **For example** input can be [0.1, 0.9] instead for [0, 1].
  
  **Solution 2.** Another you can initialize weights very small in the range of [-0.01, 0.01].
  
  **Solution 3.** You can use the regularization technique, whose purpose is to suppress the weights by adding a penalizing term in error.
  
  * To handle variance problems, you can augment some data, for proper training. Because, in tic-tac-toe, you have a small data set. To augment data, you can add some margin of range -0.1 to +0.1 in inputs with the same outputs.
  
  I hope this may be helpful.
  
  
  ---
  
  