Answers.Body: <h3>In short</h3>
  <ol>
  <li><p>ANNs don't have problems with &quot;different types&quot; of data as long as they are represented using real numbers: the inputs for your ANN represent lengths and are easy to understand and process.</p>
  </li>
  <li><p>The variable number of inputs is a little bit more tricky. In general, it is not a problem either. The net will compensate for the absence of some inputs from time to time (when food is too far away?). You don't necessarily have to use RNNs for your case.</p>
  </li>
  </ol>
  <h3>In full</h3>
  <p>I think that the inputs of your network could be improved.</p>
  <p>When building a new ANN, you always have to consider a few aspects:</p>
  <ul>
  <li>Neural Nets are just stateless functions that map inputs to outputs.</li>
  <li>There is no magic here: with good inputs, you can get good outputs.</li>
  <li>Think about how you would write an algorithm to solve that problem. Which would the best set of inputs be? Can some computation be done somewhere else? (using other ANNs, if you want to create ANN chains, or traditional algorithms).</li>
  <li>Preprocessing can do a big difference!</li>
  <li>Think about the space separation that your inputs require/cause.</li>
  </ul>
  <p>All these points are related to each other and they're only about the inputs you're providing to your net.</p>
  <p>Now, keeping these points in mind, you can check again your inputs to see if you can improve something:</p>
  <p><strong>Objective 1: eat food</strong></p>
  <p>To be sure the snake will get toward the food we definitely need to pass the ANN the position of the food. But you're sure that a long list of coordinates is the best option? Maybe the coordinate of the nearest food can be enough.  If you were THAT snake what would you like to receive? Or what would you receive if you were a real snake? Imagine your snake has a sensor that determines the best place to get food. It'll be like a compass, outputting only one value, from 0 to 365 (or choose whatever range you like). This compass will point at the <em>closest bigger cluster</em> of food or at the <em>closest food</em>.
  Remember: 1 value is always better than N and you should try to avoid a variable number of inputs, it's always a smell that something doesn't go well.</p>
  <p><strong>Objective 2: don't die</strong></p>
  <p>The distance from the centre works well. It's a simple threshold that the ANN will consider when moving, that's effective and easy to achieve. Why don't you also add another compass that will always tell you the position of the centre instead of the coordinates of the snake? In this way, you've used 2 values related to each other instead of 3, more difficult to decipher for an ANN.</p>
  <p>The time is not really necessary. You're inserting a value that isn't related to any other, or the environment, or the food. Any possible behaviour is not influenced by its value (if the ANN knew that it has only 15 seconds left, it wouldn't change its behaviour. The same if it knew that it's &quot;alive&quot; since 1 minute or 4000).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-13T15:41:26.223Z
Answers.LastActivityDate: 2021-12-20T23:37:54.237Z
Answers.LastEditDate: 2021-12-20T23:37:54.237Z
Answers.UserDisplayName: Alvin Sartor
Body: <p>After learning the basics of neural networks and coding one working with the MNIST dataset, I wanted to go to the next step by making one which is able to play a game. I wanted to make it work on a game like slither.io. So, in order to be able to create multiple instances of snakes and accelerate the speed of the game, I recreated a simple version of the game:</p>
  <p><a href="https://i.sstatic.net/g2oMV.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/g2oMV.gif" alt="enter image description here" /></a></p>
  <p>The core features being almost done, now comes the work on the AI. I want to keep the script very simple by using only NumPy (not that TensorFlow, PyTorch, or Spark does not interest me, but I want to understand things at a &quot;low level&quot; before using those frameworks).</p>
  <p>At first, I wanted the AI to be able to propose an output by reading pixels. But after some research, I don't really want to get into convnet, recurrent, and recursive neural net. I'd like to re-use the simple feed-forward NN I did with MNIST and adapt it.</p>
  <p>So, instead of using pixels, I think I'm going to use the following data:</p>
  <ul>
  <li>{x,y} snake's position</li>
  <li>{x,y} foods positions</li>
  <li>food value</li>
  <li>Time, in order to get the snake, eat more food in a short time.</li>
  <li>Distance from the center, not die outside the area</li>
  </ul>
  <p><a href="https://i.sstatic.net/g410X.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/g410X.png" alt="enter image description here" /></a></p>
  <p>That's a lot of different data to handle!</p>
  <p><a href="https://i.sstatic.net/DuXAQ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/DuXAQ.png" alt="enter image description here" /></a></p>
  <p>My questions</p>
  <ol>
  <li><strong>Can a simple FNN handle different kinds of data in the input layer?</strong></li>
  <li><strong>Will it properly work with a variable number of inputs?</strong></li>
  </ol>
  <p>In fact, in a specific area around the snake, the quantity of food will be variable. I came across <a href="https://ai.stackexchange.com/questions/2008/how-can-neural-networks-deal-with-varying-input-sizes">this post</a>, which kinda answers my question, but what if I want the neural network to forget some input if they are not being used, can <strong>dropout</strong> be of any use in this case. Or the value of the weights (correcting toward zero) of these inputs will be enough?</p>
  
Tags: <neural-networks><game-ai><feedforward-neural-networks><features>
Title: How to handle varying types and length of inputs in a feedforward neural network?
UserDisplayName: AntonKad
ThreadText: # How to handle varying types and length of inputs in a feedforward neural network?
  
  (Asked by: username_0 on 2018-05-10)
  
  After learning the basics of neural networks and coding one working with the MNIST dataset, I wanted to go to the next step by making one which is able to play a game. I wanted to make it work on a game like slither.io. So, in order to be able to create multiple instances of snakes and accelerate the speed of the game, I recreated a simple version of the game:
  
  [![enter image description here](https://i.sstatic.net/g2oMV.gif)](https://i.sstatic.net/g2oMV.gif)
  
  The core features being almost done, now comes the work on the AI. I want to keep the script very simple by using only NumPy (not that TensorFlow, PyTorch, or Spark does not interest me, but I want to understand things at a "low level" before using those frameworks).
  
  At first, I wanted the AI to be able to propose an output by reading pixels. But after some research, I don't really want to get into convnet, recurrent, and recursive neural net. I'd like to re-use the simple feed-forward NN I did with MNIST and adapt it.
  
  So, instead of using pixels, I think I'm going to use the following data:
  
  * {x,y} snake's position
  * {x,y} foods positions
  * food value
  * Time, in order to get the snake, eat more food in a short time.
  * Distance from the center, not die outside the area
  
  [![enter image description here](https://i.sstatic.net/g410X.png)](https://i.sstatic.net/g410X.png)
  
  That's a lot of different data to handle!
  
  [![enter image description here](https://i.sstatic.net/DuXAQ.png)](https://i.sstatic.net/DuXAQ.png)
  
  My questions
  
  1. **Can a simple FNN handle different kinds of data in the input layer?**
  2. **Will it properly work with a variable number of inputs?**
  
  In fact, in a specific area around the snake, the quantity of food will be variable. I came across [this post](https://ai.stackexchange.com/questions/2008/how-can-neural-networks-deal-with-varying-input-sizes), which kinda answers my question, but what if I want the neural network to forget some input if they are not being used, can **dropout** be of any use in this case. Or the value of the weights (correcting toward zero) of these inputs will be enough?
  
  **Tags:** neural-networks, game-ai, feedforward-neural-networks, features
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-05-13. Score: 2 | Accepted answer
  
  ### In short
  
  1. ANNs don't have problems with "different types" of data as long as they are represented using real numbers: the inputs for your ANN represent lengths and are easy to understand and process.
  2. The variable number of inputs is a little bit more tricky. In general, it is not a problem either. The net will compensate for the absence of some inputs from time to time (when food is too far away?). You don't necessarily have to use RNNs for your case.
  
  ### In full
  
  I think that the inputs of your network could be improved.
  
  When building a new ANN, you always have to consider a few aspects:
  
  * Neural Nets are just stateless functions that map inputs to outputs.
  * There is no magic here: with good inputs, you can get good outputs.
  * Think about how you would write an algorithm to solve that problem. Which would the best set of inputs be? Can some computation be done somewhere else? (using other ANNs, if you want to create ANN chains, or traditional algorithms).
  * Preprocessing can do a big difference!
  * Think about the space separation that your inputs require/cause.
  
  All these points are related to each other and they're only about the inputs you're providing to your net.
  
  Now, keeping these points in mind, you can check again your inputs to see if you can improve something:
  
  **Objective 1: eat food**
  
  To be sure the snake will get toward the food we definitely need to pass the ANN the position of the food. But you're sure that a long list of coordinates is the best option? Maybe the coordinate of the nearest food can be enough. If you were THAT snake what would you like to receive? Or what would you receive if you were a real snake? Imagine your snake has a sensor that determines the best place to get food. It'll be like a compass, outputting only one value, from 0 to 365 (or choose whatever range you like). This compass will point at the *closest bigger cluster* of food or at the *closest food*.
  Remember: 1 value is always better than N and you should try to avoid a variable number of inputs, it's always a smell that something doesn't go well.
  
  **Objective 2: don't die**
  
  The distance from the centre works well. It's a simple threshold that the ANN will consider when moving, that's effective and easy to achieve. Why don't you also add another compass that will always tell you the position of the centre instead of the coordinates of the snake? In this way, you've used 2 values related to each other instead of 3, more difficult to decipher for an ANN.
  
  The time is not really necessary. You're inserting a value that isn't related to any other, or the environment, or the food. Any possible behaviour is not influenced by its value (if the ANN knew that it has only 15 seconds left, it wouldn't change its behaviour. The same if it knew that it's "alive" since 1 minute or 4000).
  
  
  ---
  
  