Train a Neural Network to distinguish between even and odd numbers Question: is it possible to train a NN to distinguish between odd and even numbers only using as input the numbers themselves?
I have the following dataset:
Number Target
1      0
2      1
3      0
4      1
5      0
6      1
...   ...
99     0
100    1

I trained a NN with two input neurons (one being the variable Number, the other being a bias neuron), nine neurons in the hidden layer and one output neuron using a very simple genetic algorithm: at each epoch, two sets of weights "fight" against each other; the one with the highest error loses and it's replaced by a modified version of the winner.
The script easily solve simple problems like the AND, the OR and the XOR operators but get stuck while trying to categorise odd and even numbers. Right now the best it managed to do is to identify 53 numbers out of 100 and it took several hours. Whether I normalize or not the inputs seems to make no difference.
If I wanted to cheat I could just pre-processed the data and feed % 2 to the NN as an input but I don't want to do that; NN should be able to approximate every function, including the modulo operator (I believe). What am I doing wrong?
 A: As with any machine learning task, the representation of your input plays a crucial role in how well you learn and generalise.
I think, the problem with the representation is that the function (modulo) is highly non-linear and not smooth in the input representation you've chosen for this problem.
I would try the following:


*

*Try a better learning algorithm (back-propagation/gradient descent and its variants).

*Try representing the numbers in binary using a fixed length precision.

*If your input representation is a b-bit number, I would ensure your training set isn't biased towards small or large numbers.  Have numbers that are uniformly, and independently chosen at random from the range $[0, 2^b-1]$.

*As you've done, use a multi-layer network (try 2 layers first: i.e., hidden+output, before using more layers).

*Use a separate training+test set.  Don't evaluate your performance on the training set.
A: Learning to classify odd numbers and even numbers is a difficult problem. A simple pattern keeps repeating infinitely. 
2,4,6,8.....
1,3,5,7.....
Nonlinear activation functions like sin(x) and cos(x) behave similarly.
Therefore, if you change your neurons to implement sin and cos instead of  popular activation functions like tanh or relu, I guess you can solve this problem fairly easily using a single neuron.
Linear transformations always precede nonlinear transformations. Therefore a single neuron will end up learning sin(ax+b) which for the right combination of a & b will output 0's and 1's alternatively in the desired frequency we want which  in this case is 1.
I have never tried sin or cos in my neural networks before. So, apologies if it ends up being a very bad idea.
A: So I'm working with neural nets right now and I ran into the same issue as you. What I ended up doing was representing the input number as an array with values equal to the binary representation of the number. Since what we are doing is classifying I represented my output as an array, not a single value.
ex:
input = [
  [0, 0, 0, 1], // 1
  [0, 0, 1, 0], // 2
  [0, 0, 1, 1], // 3
  [0, 1, 0, 0]  // 4
]
output = [
  [1, 0], // odd
  [0, 1], // even
  [1, 0], // odd
  [0, 1]  // even
]

Hope this helps!
A: I get here where was struggle with similar problem. So I write what I managed. 
As far as I know one layer perceptron is able to solve every problem, which can be at the end simplified to divide objects in any geometry using straight line. And this is this kind of problem. If you draw last bit of binary representation on paper you can also draw line, and all Odd numbers are on one side, and Even on other. For the same reason it is impossible to solve xor problem with one layer network.
Ok. This problem looks very simple, so lets take Heaviside step as activation function. After I played a little with my number I realized that problem here is with bias. I google a little, and what I found is that if you stay with geometry representation, bias enable you to change place of activation in coordinate system.
Very educational problem
A: It is well known that logic gates NOT, AND, OR can all be done with very simple neural networks (NN), and that you can build a complete arithmetic calculator with logic gates using binary numbers as input.  Therefore you should be able to create a NN to calculate n modulo k, for any n and k numbers expressed in base 2.
If you wish to calculate n modulo k for a fixed k number (for example k = 4) you can actually create an extremely simple NN that does that: express the input number n in base k, and ignore all digits other than the lowest rank digit, and you have the answer!
A: One idea evading the use of explicit "mod 2" in the input could be to codify the number as a sequence of pixels, then the problem amounts to recognize if the segment can be split into two equal segments. This is a machine vision problem and that could be learned by conventional networks.
On the other extreme, if the number is stored as a float, the question reduces (or generalizes) to recognize when a float number is approximately an integer.
A: I created such a network in here.
The representation @William Gottschalk gave was the foundation.
It just uses 1 neuron in the first hidden layer with 32 inputs. The output layer has just 2 neurons for one-hot encoding of 0 and 1.
