# Small Steps 2 – Teaching a Neural Network to Learn the Letter A from B-Z(567 words)

So in the previous article we managed to get our neural network to learn the difference between A and B. I mentioned at the end I was going to next test and teach it on various versions of A and B to see how effective it is, but rather then that I figured teaching a network to learn A from every other letter would be more interesting.

Get the source to everything below in Step2

Now the code below is rather un-pythonic but it does show us loading each of the letters and then training the network to learn that an A is an A and that every other letter is not an A. I had initially tried to teach it how to recognise each letter however I found this resulted in a huge neural network which was slow to train. For the moment teaching the network what an A is should be fine for now.

```import bpnn

if __name__ == '__main__':

hiddennodes = 3
x = 5
y = 5

apat = [
[bdata,],
[cdata,],
[ddata,],
[edata,],
[fdata,],
[gdata,],
[hdata,],
[idata,],
[jdata,],
[kdata,],
[ldata,],
[mdata,],
[ndata,],
[odata,],
[pdata,],
[qdata,],
[rdata,],
[sdata,],
[tdata,],
[udata,],
[vdata,],
[wdata,],
[xdata,],
[ydata,],
[zdata,],
]

an.train(apat)

cla.savenn(an,filename='aznn.n')```

Again like before what the above does is open up each of our sample images and then trains the network on them. I ended up playing around with the number of nodes and managed to get a low error rate with 25 inputs and 3 hidden nodes. This is interesting as the last network used 400 inputs and 3 hidden nodes, and at first I was skeptical if the network had learnt this pattern correctly.

Of course we need something to test the effectiveness of our network and so I created the below test script which should take care of this and should let us see if the network does work correctly.

```import unittest

class TestClassifyAfromB(unittest.TestCase):
def setUp(self):
self.x = 10
self.y = 10

def testLearnA(self):
self.assertTrue(guess > 0.95)

def testLearnB(self):
self.assertTrue(guess < 0.05)

def testLearnC(self):
for let in 'B2 B3 C D E F G H I J K L M N O P Q R S T U V W X Y Z'.split(' '):
self.assertTrue(guess < 0.05)

if __name__ == '__main__':
unittest.main()```

The above is just a quick and dirty test and the results of which are,

```\$python TestStep2.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.015s

OK```

All good! The next goal is to build a large sample of different letters in different fonts and get the network to pick out the letter A from many examples. This will indicate that it has learnt the pattern of what an A looks like rather then the letter A as given in the above examples.