Commodore 64 Neural Network on a Commodore 64


by John Walker
September 4, 1987

When you first start to learn about computers, it's only natural to compare how a computer “thinks” with how people do. As you learn more, you tend to stop making such comparisons because you come to realize that far from the literal, dumb servants they're often pictured as in movies, computers are completely different from people both in how they operate and the kinds of problems they can solve.

But it's still fascinating to compare a computer to a human brain. Researchers are beginning to discover principles which may explain how the brain works, and while much of this research is extremely complicated and requires large, expensive computers, your Commodore 64 can be programmed in BASIC to mimic one fundamental part of brain function, pattern recognition. In doing this, your computer is not only doing something the brain does (and your reaction may be “I didn't know a computer could do that!”), it's doing it the brain's way—by simulating the neurons (brain cells) with which you think.

Computer Storage and Human Memory

First, let's compare what the word “memory” means in a computer as opposed to a human being. A computer memory is really a huge collection of pigeonholes into which numbers are stuffed. If you want to store something that isn't a number, such a piece of text, a picture, or a sequence of musical notes, you first have to convert it to numbers, then put those numbers into the pigeonholes of the computer's memory. Machine language programming is just translating the instructions for solving a problem into numbers the computer can remember. A computer stores numbers.

Attneave's Cat
Attneave's Cat

Human memory is richer and much more complicated. What comes to mind when you look at the picture above? Even though this picture is nothing more than 63 straight black lines on a white piece of paper, your brain immediately recognizes it as a sleeping cat. You can even imagine how a real cat would look if seen from other angles. The human brain seems to store and recall patterns. These patterns don't have to be pictures. You can recall pieces of music from only a few notes of the melody, think of words that rhyme with “frog” or that end with “ple”, and quickly name the American presidents that have the same names as automobiles.

It isn't that human memory is better or worse than computer memory, but rather that they are entirely different things. Some computer scientists prefer the term “storage” to “memory” because it more accurately describes what the computer does. If you walk up to a computer and ask it “what is the name of the famous bridge in the same state as Disneyland” you won't have much success, but most people will immediately answer, “The Golden Gate Bridge”. On the other hand, if you try to memorize a list of 20,000 two digit numbers, you probably won't succeed, yet a Commodore 64 can do this in less than a second and not make a single mistake. That computer and human memory are so different isn't all that surprising when you consider how differently constructed are the brain and a computer.

What Brains Are Made Of

A Neuron

The portion of the brain believed responsible for thought and memory consists primarily of nerve cells, or neurons. Each neuron has three parts, dendrites, a cell body, and an axon. The dendrites connect to the axons of other neurons. When these other neurons are stimulated, the dendrites convey the signal to the cell body via a synapse or connection, which either excites or inhibits the neuron (with a different strength for each synapse). When the excitation sufficiently outweighs inhibition, the neuron “fires”. This sends a signal down its axon which in turn excites or inhibits other neurons, and perhaps causes a muscle to move.

A Neuron Network
A Neuron Network

Because neurons primarily connect to other neurons they form networks of great complexity. Figure 3 shows two fields of five neurons each, in which each neuron connects to every neuron in the other field. In this simple case we have 10 neurons with 5 connections (or synapses) each, for a total of 50 synapses. Now consider the brain. Researchers believe that the brain contains between ten and a hundred billion neurons, each of which connects to anywhere from a thousand to a hundred thousand other neurons, forming at least ten trillion connections, and probably far more. Compare this to the read-write memory of the Commodore 64, which is made up of about a quarter million transistors, and remember that each transistor is only a switch—far simpler than a neuron.

The Brain Simulator

Ten billion neurons, ten trillion connections: does it make sense to talk about simulating the brain on a computer? Can we make a computer recognize patterns the way a brain does? Remarkably, we can. A simple program can simulate the behavior of a network of interconnected neurons. You can show this program patterns and it will remember them. Then if you show it a similar pattern, it will find the pattern it's learned that is most like the pattern it's shown. The technical name for this is an “associative memory”, so called because it recalls items based on similarity, like the brain, as opposed to location, like a computer.

The Brain Simulator is written in BASIC for the 64 and 128 (in 64 mode). When you've finished typing in the program, save a copy to tape or disk. To run the program, simply load it and type RUN. You'll see two blank windows on the screen. Below them is a legend that explains the action of the function keys. When you type a letter or number, the dot pattern for that symbol appears in the left window. Try typing a few letters and numbers to see how this works. When you start the program, it doesn't know any patterns—so we'll teach it some. To learn a pattern, place it in the left window by pressing the key for the letter or number and then press F1. The program trains its simulated neurons to memorize the pattern (this takes about 30 seconds). READY reappears on the screen when the pattern has been learned. Go ahead and teach the program three different-looking letters, “A”, “T”, and “Z”.

Now let's try recalling a pattern. Press the “A” key to place an A in the left window. Now press F3—this introduces errors in the pattern by randomly changing about 10% of the dots in the pattern each time you press it. After you press F3, you'll have a pattern that looks something like an A, but doesn't exactly match what we taught the program. Press F5 to start the recall process. The pattern is run back and forth through the neuron network until it stabilizes on a fixed pattern (an arrow in the middle of the screen shows the direction of the transfer). After the neuron network has “thought” about the problem for a few cycles, you'll probably get back the original A we taught the program (just like the brain, this process doesn't always remember the right thing; if the random changes made the pattern more like another pattern the program has learned, that one will be found instead).

Try entering T and Z, creating errors in them by pressing F3 one or two times, and recalling with F5. Note how the neuron network almost always recalls the correct pattern even though you've given it something quite different from what it learned. Enter “I” and try recalling with F5. The network recognizes this as T because I looks more like T than A or Z, the other patterns it has learned. This is what your brain does when it sees a pattern of lines and immediately thinks of a sleeping cat. Many researchers think the basic process the brain uses is much the same as the one used by this program.

You can make the program forget everything it has learned by pressing F4. If you press F6, the program exits to BASIC. When you leave the program everything it has learned is forgotten, but you can save learned patterns on the disk by pressing F7 and entering a file name. The next time you run the program you can reload the program's memory by pressing F8 and entering the same file name.

How it Works

The remarkable thing about this program is that it doesn't “know” it's recognizing letters and numbers. As far as the program is concerned, it could be learning phrases of music, combinations of medical symptoms, or pictures of animals.

The program simulates two fields of neurons with the arrays F1% and F2%, and displays these fields in the two windows on the screen. When you type a letter or number, the dot pattern for that symbol is read from the character ROM and stored in F1%. Lighted dots are stored as 1 and background dots as −1. The character patterns are 6 dots wide by 7 dots high, so F1% and F2% both hold 42 numbers.

Each neuron in a field potentially connects to every neuron in the other field. Each connection, which is equivalent to a synapse in the brain, has its own weight: positive to excite, negative to inhibit, and zero if there is no connection. These weights are stored in the 42×42 matrix M%, for a total of 1,764 connections. To learn a pattern, we form a matrix from the pattern in F1% and add it to the weight matrix M% (see lines 1020–1060 in the program). To recall a pattern we take the the pattern in F1% and multiply it by the weight matrix (lines 1410–1480). If the value is 1 or more, we place a 1 in that position of F2%; if it's negative, we store a −1 there. If the value is zero, we leave the value in F2% alone. Then we take the value in F2% and multiply it back through the the matrix, but swapping rows and columns, and store it back into F1% (lines 1540–1610). We keep this up until the pattern in F1% stops changing. That's our final value, the pattern we've recalled from the network.

Limits of Learning

The number of different patterns a neuron network can learn is determined by the number of neurons and connections. This very small network can learn only 2 or 3 distinct patterns before it begins to get confused. If you try to teach it 4 or 5 patterns, it will often recall the wrong pattern, or even a spurious pattern you never taught it. That's where the enormous complexity of the brain comes in; your brain has at least five billion times as many connections as this little program, so your ability to learn and remember is correspondingly greater.

But like a singing dog, it's not how well it sings but the fact it sings at all. With less than 250 lines of BASIC, we can make the Commodore 64 play the brain's game, pattern recognition, the brain's way, by simulating a neuron network. It can learn simply by being shown patterns and recall a similar pattern when shown something it's never seen before. Research into neuron networks is one of the most exciting and rapidly expanding fields in science today, bringing together computer science, psychology, mathematics, and biology to discover how the brain accomplishes the remarkable things it does. This research may lead to computers that can recognize faces, understand speech, and answer complicated questions. But more importantly, we may find answers to questions as old as mankind: “What is thought?”, “What is memory?”, and “How do people reason?”. That your home computer can help you understand and experiment with such matters is testimony to the power latent within it.

For Further Information

This article only scratches the surface of the topic of neuron networks and associative memory. If you're interested in learning more, the following article and book are a good way to start.

Kosko, B., “Constructing an Associative Memory”. Byte, September 1987.

Rumelhart, A., and McClelland, J., eds., Parallel Distributed Processing (two volumes), MIT Press, 1986.

Program Listing

10 REM SCREEN CONFIGURATION
20 POKE 53280,13
30 POKE 53281,6
40 PRINT "";
50 OPEN 15,8,15
60 REM VARIABLE DECLARATIONS
70 DIM F1%(42),F2%(42),M%(42,42)
80 DIM V%,J,I
90 REM INITIALISE SCREEN
100 PRINT "";
110 PRINT "    NEURON NETWORK ASSOCIATIVE MEMORY"
120 PRINT
130 PRINT "";
140 PRINT "F1 - TEACH PATTERN     ";
150 PRINT "F2 - DUMP MATRIX"
160 PRINT "F3 - RANDOMIZE PATTERN ";
170 PRINT "F4 - FORGET ALL"
180 PRINT "F5 - RECALL PATTERN    ";
190 PRINT "F6 - QUIT"
200 PRINT "F7 - DISC SAVE         ";
210 PRINT "F8 - DISC LOAD"
220 PRINT
230 PRINT "A-Z, 0-9: LOAD PATTERN"
240 R1 = 4 : C1 = 5 : GOSUB 600
250 R1 = 4 : C1 = 25 : GOSUB 600
260 GOSUB 750
270 GOSUB 860
280 GOSUB 970:PRINT " READY    "
290 GET A$ : IF A$="" GOTO 290
300 GOSUB 970:PRINT "          "
310 K=ASC(A$)
320 IFA$>="0"ANDA$<="9"THENK=K+64:GOTO340
330 IF A$ < "A" OR A$ > "Z" THEN 500
340 GOSUB 970:PRINT "FETCH ";A$
350 L%=0
360 K=(K-64)*8+53248
370 POKE56333,127:POKE 1,PEEK(1)AND251
380 FORI=0TO6:POKE49408+I,PEEK(K+I):NEXT
390 POKE 1,PEEK(1) OR 4:POKE 56333,129
400 FOR I = 0 TO 6
410 J% = PEEK(49408+I)/2
420 FOR K=1 TO 6
430 L%=L%+1
440 F1%(L%) = -1 + (2 * (J% AND 1))
450 J%=J%/2
460 NEXT K
470 NEXT I
480 GOSUB 750 : GOSUB 860 : GOTO 280
490 REM DISPATCH FUNCTION KEY COMMANDS
500 J%=ASC(A$)-132
510 IF J%=1 THEN GOSUB 1000:GOTO 280
520 IF J%=5 THEN GOSUB 1080:GOTO 90
530 IF J%=2 THEN GOSUB 1210:GOTO 280
540 IF J%=6 THEN GOSUB 1680:GOTO 280
550 IF J%=3 THEN GOSUB 1290:GOTO 280
560 IF J%=7 THEN PRINT "";:CLOSE15:END
570 IF J%=4 THEN GOSUB 1800:GOTO 90
580 IF J%=8 THEN GOSUB 1990:GOTO 90
590 GO TO 280
600 REM DRAW BORDERS FOR FIELDS
610 FOR I=0 TO 1
620 V=1024+40*(R1+(I*8))+C1
630 POKE V,112+(-3*I)
640 FOR J=1 TO 8
650 POKE V+J,67
660 NEXT J
670 POKE V+9,110+(15*I)
680 NEXT I
690 FOR I=1 TO 7
700 V=1024+40*(R1+I)+C1
710 POKE V,93
720 POKE V+9,93
730 NEXT I
740 RETURN
750 REM UPDATE FIELD F2% ON SCREEN
760 L%=0
770 FOR I=0 TO 6
780 V% = 1024+40*(I+5)+6
790 FOR J=2 TO 7
800 L%=L%+1
810 IFF1%(L%)=1THENPOKEV%+(8-J),81:GOTO830
820 POKE V%+(8-J),32
830 NEXT J
840 NEXT I
850 RETURN
860 REM UPDATE FIELD F1% ON SCREEN
870 L%=0
880 FOR I = 0 TO 6
890 V%=1024+40*(I+5)+26
900 FOR J=2 TO 7
910 L%=L%+1
920 IF F2%(L%)=1 THEN POKE V%+(8-J),81:GOTO 940
930 POKE V%+(8-J),32
940 NEXT J
950 NEXT I
960 RETURN
970 REM POSITION TO STATUS AREA
980 PRINT "";
990 RETURN
1000 REM TRAIN ON PATTERN IN F1%
1010 GOSUB 970:PRINT "TRAINING"
1020 FOR I = 1 TO 42
1030 FOR J = 1 TO 42
1040 M%(I,J)=M%(I,J)+F1%(I)*F1%(J)
1050 NEXT J
1060 NEXT I
1070 RETURN
1080 REM PRINT PART OF MATRIX
1090 PRINT "";
1100 FOR I=1 TO 24
1110 FOR J=1 TO 39
1120 IFM%(I,J)<0THENPRINT "";:GOTO1140
1130 PRINT "";
1140 PRINT CHR$(ASC("0")+ABS(M%(I,J)));
1150 NEXT J
1160 PRINT
1170 NEXT I
1180 PRINT "PRESS ANY KEY TO CONTINUE:";
1190 GET A$ : IF A$="" GOTO 1190
1200 RETURN
1210 REM RANDOMISE 10 PERCENT OF F1%
1220 GOSUB 970:PRINT "RANDOM"
1230 FOR I=1 TO 42
1240 IF RND(0) > 0.1 THEN 1260
1250 F1%(I)=-F1%(I)
1260 NEXT I
1270 GOSUB 750
1280 RETURN
1290 REM RECALL FROM PATTERN
1300 GOSUB 970:PRINT "RECALL"
1310 P%=1024+40*9+19
1320 REM INITIALLY COPY F1 TO F2
1330 POKE P%+1,ASC("=")
1340 FOR I=1 TO 42
1350 F2%(I)=F1%(I)
1360 NEXT I
1370 GOSUB 860
1380 REM F1 TO F2 PASS
1390 POKE P%,ASC("=")
1400 POKE P%+2,ASC(">")
1410 FOR J=1 TO 42
1420 V%=0
1430 FOR I=1 TO 42
1440 V%=V%+F1%(I)*M%(I,J)
1450 NEXT I
1460 V%=SGN(V%)
1470 IF V%<>0 THEN F2%(J)=V%
1480 NEXT J
1490 GOSUB 860
1500 REM F2 TO F1 PASS
1510 C%=0
1520 POKE P%,ASC("<")
1530 POKE P%+2,ASC("=")
1540 FOR I=1 TO 42
1550 V%=0
1560 FOR J=1 TO 42
1570 V%=V%+F2%(J)*M%(I,J)
1580 NEXT J
1590 V%=SGN(V%)
1600 IFV%<>0ANDV%<>F1%(I)THENF1%(I)=V%:C%=1
1610 NEXT I
1620 GOSUB 750
1630 IF C%<>0 GOTO 1380
1640 POKE P%,ASC(" ")
1650 POKE P%+1,ASC(" ")
1660 POKE P%+2,ASC(" ")
1670 RETURN
1680 REM FORGET ALL - CLEAR MEMORY
1690 GOSUB 970:PRINT "FORGET"
1700 FOR I=1 TO 42
1710 F1%(I)=0
1720 F2%(I)=0
1730 FOR J=1 TO 42
1740 M%(I,J)=0
1750 NEXT J
1760 NEXT I
1770 GOSUB 750
1780 GOSUB 860
1790 RETURN
1800 REM SAVE STATE TO DISC FILE
1810 GOSUB 970:PRINT "SAVE"
1820 PRINT "";
1830 INPUT "FILE NAME: ";A$
1840 A$="@0:"+A$+",S,W"
1850 OPEN 5,8,5,A$
1860 FOR I=1 TO 42:PRINT#5,F1%(I):NEXT
1870 GOSUB 2240
1880 FOR I=1 TO 42:PRINT#5,F2%(I):NEXT
1890 GOSUB 2240
1900 FOR I=1 TO 42
1910 FOR J=1 TO 42
1920 PRINT#5,M%(I,J)
1930 NEXT J
1940 GOSUB 2240
1950 NEXT I
1960 CLOSE 5
1970 PRINT "";
1980 RETURN
1990 REM RESTORE STATE FROM DISC FILE
2000 GOSUB 970:PRINT "RESTORE"
2010 PRINT "";
2020 INPUT "FILE NAME: ";A$
2030 A$="@0:"+A$+",S,R"
2040 P%=ASC("M")
2050 GOSUB 2240
2060 OPEN 5,8,5,A$
2070 FOR I=1 TO 42
2080 INPUT#5,F1%(I)
2090 NEXT I
2100 GOSUB 2240
2110 FOR I=1 TO 42
2120 INPUT#5,F2%(I)
2130 NEXT I
2140 GOSUB 2240
2150 FOR I=1 TO 42
2160 FOR J=1 TO 42
2170 INPUT#5,M%(I,J)
2180 NEXT J
2190 GOSUB 2240
2200 NEXT I
2210 CLOSE 5
2220 RETURN
2230 REM DISC ERROR CHECK
2240 INPUT#15,EN,EM$,ET,ES
2250 IF EN>0THEN PRINT EN,EM$,ET,ES:STOP
2260 RETURN


by John Walker