REM CORRECT SO FARR!!!!!!!!!!!1 REM adding noise REM Neural Netprogram (V6 basic> REM Define all functions REM DEF fnactiv (x) = (EXP(x) - EXP(-x)) / (EXP(x) + EXP(-x)) REM DEF fndactiv (x) = 1 / (EXP(x) + EXP(-x)) DEF fnactiv (x) = SGN(x) DEF fndactiv (x) = SGN(x) DIM W(70, 15, 3), Result(15), winp(15, 70), RESP(20), x(69, 2), yhid(15), xinp(69, 20), nam$(20), xin$(20) REM extract xinp() from xin$ REM inputs: f$ REM output: xinp() REM remember to define f$ up top!!!!!! betai = .01: betao = .01 nnodes = 6 GOSUB 4000 ncases = INT(ndpts / 3) FOR fcase = 1 TO ncases f$ = xin$(fcase) GOSUB 5000 NEXT fcase PRINT "initializing W" REM initialize W FOR i = 1 TO 69 FOR j = 1 TO nnodes W(i, j, 0) = 1 / (69 * nnodes) W(i, j, 1) = 1 / (nnodes) NEXT j NEXT i nn = nnodes PRINT "beginning training" FOR nossn = 1 TO 2 FOR pasn = 1 TO 10 FOR fcase = 1 TO ncases IF nossn = 2 THEN f$ = xin$(fcase) GOSUB 5000 GOSUB 6000 END IF REM ************************************************** 13 PRINT "forward pass for "; nam$(fcase) REM forward pass FOR i = 1 TO 69 x(i, 0) = xinp(i, fcase) NEXT i FOR ii = 1 TO 69 FOR jj = 1 TO nn winp(jj, ii) = W(ii, jj, 0) NEXT jj NEXT ii REM input to hidden layer FOR ii = 1 TO nn yhid(ii) = 0! FOR jj = 1 TO 69 yhid(ii) = yhid(ii) + winp(ii, jj) * (x(jj, 0)) NEXT jj NEXT ii PRINT "x(__,1)="; FOR ii = 1 TO nn x(ii, 1) = SGN(fnactiv(yhid(ii))) PRINT x(ii, 1); " "; NEXT ii REM hidden nodes to output node FOR ii = 1 TO nn wo(ii) = W(ii, 1, 1) NEXT ii yo = 0! FOR ii = 1 TO nn yo = (x(ii, 1) * wo(ii)) + yo NEXT ii yo = SGN(fnactiv(yo)) RESPi = yo REM condition check PRINT nam$(fcase), "current="; RESPi; "correct="; RESP(fcase) IF RESPi = RESP(fcase) THEN 100 REM weight mods sum1 = 0! FOR ii = 1 TO 69 FOR jj = 1 TO nn W(ii, jj, 0) = W(ii, jj, 0) + betai * fndactiv(RESP(fcase) - x(jj, 1)) * x(ii, 0) sum1 = sum1 + W(ii, jj, 0) NEXT jj NEXT ii FOR ii = 1 TO 69 FOR jj = 1 TO nn W(ii, jj, 0) = W(ii, jj, 0) / sum1 NEXT jj NEXT ii sum2 = 0! FOR ii = 1 TO nn W(ii, 1, 1) = W(ii, 1, 1) + betao * fndactiv(RESP(fcase) - yo) * x(ii, 1) sum2 = sum2 + W(ii, 1, 1) NEXT ii FOR ii = 1 TO nn W(ii, 1, 1) = W(ii, 1, 1) / sum2 NEXT ii GOTO 13 REM overal loop finish REM ************************************************8 100 NEXT fcase REM close training pass loop NEXT pasn NEXT nossn REM &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& PRINT "Beginning Testing!!" REM TEST LOOP FOR fcase = 1 TO ncases PRINT nam$(fcase); TAB(20); FOR tloop = 1 TO 10 f$ = xin$(fcase) GOSUB 5000 GOSUB 6000 REM forward pass FOR i = 1 TO 69 x(i, 0) = xinp(i, fcase) NEXT i FOR ii = 1 TO 69 FOR jj = 1 TO nn winp(jj, ii) = W(ii, jj, 0) NEXT jj NEXT ii REM input to hidden layer FOR ii = 1 TO nn yhid(ii) = 0! FOR jj = 1 TO 69 yhid(ii) = yhid(ii) + winp(ii, jj) * (x(jj, 0)) NEXT jj NEXT ii FOR ii = 1 TO nn x(ii, 1) = SGN(fnactiv(yhid(ii))) NEXT ii REM hidden nodes to output node FOR ii = 1 TO nn wo(ii) = W(ii, 1, 1) NEXT ii yo = 0! FOR ii = 1 TO nn yo = (x(ii, 1) * wo(ii)) + yo NEXT ii yo = SGN(fnactiv(yo)) RESPi = yo IF RESPi = RESP(fcase) THEN Result(fcase) = Result(fcase) + 1 NEXT tloop PRINT "Test result="; Result(fcase) * 10; " %" ovrres = ovrres + Result(fcase) / 10 / ncases NEXT fcase PRINT "Testing Complete" IF ovrres >= .9 THEN GOSUB 9000 PRINT "Overall Result="; ovrres * 100; " %" END REM ************************************************************************** 4000 REM read in data file REM inputs: fil$, REM outputs: nam$,xin$,bitavd,ndpts for all objects fil$ = "c:\softeng\cap6990\nnproj\filbdt1" PRINT "directory+name of data file"; fil$ OPEN fil$ FOR INPUT AS #1 ndpts = 0 WHILE NOT EOF(1) LINE INPUT #1, nnnn$ ndpts = ndpts + 1 WEND CLOSE #1 PRINT "there are "; ndpts / 3; "data objects" OPEN fil$ FOR INPUT AS #1 FOR i = 1 TO ndpts / 3 LINE INPUT #1, nam$(i) LINE INPUT #1, xin$(i) INPUT #1, RESP(i) NEXT i CLOSE #1 PRINT "data objects:" FOR i = 1 TO ndpts / 3 PRINT nam$(i); "--"; xin$(i) PRINT "food/no food"; RESP(i) NEXT i RETURN REM*********************************************************** REM *********************************************************** 5000 REM extract xinp() from xin$ REM inputs: f$ REM output: xinp() REM remember to define f$ up top!!!!!! FOR i = 1 TO LEN(f$) xx = ASC(MID$(f$, i, 1)) - ASC("0") IF xx = 1 THEN xinp(i, fcase) = 1 IF xx = 0 THEN xinp(i, fcase) = -1 REM PRINT "xinp("; i; ") = "; xinp(i, fcase) NEXT i RETURN REM******************************************************** REM******************************************************** 6000 REM noise adder REM add visual noise due to llvl LLvl = 9 - 8 * RND(10) PRINT "Light Level="; LLvl LLaa = (9 / LLvl) * .1 LLvl = INT(LLvl) FOR i = 1 TO 49 IF i / LLvl = INT(i / LLvl) AND 3 * RND(3) > 2.1 THEN xinp(i, fcase) = SGN(xinp(i, fcase) - 2) END IF NEXT i REM add Temperature noise Temp = 95 - 40 * RND(10) PRINT "Temp="; Temp; " F" TTaa = 1 + 10 / ABS(75 - Temp) TTsn = SGN(75 - Temp) FOR i = 50 TO 60 IF (60 - i) / INT(TTaa) = INT((60 - i) / INT(TTaa)) AND 10 * RND(10) > 6.5 THEN xinp(i, fcase) = SGN(xinp(i, fcase) + TTsn * 2) END IF NEXT i RETURN REM******************************************************** REM ********************************************************** 9000 REM Ouput Successful results OPEN "c:\softeng\cap6990\nnproj\filb.out" FOR OUTPUT AS #1 PRINT #1, "FILB Success file" PRINT #1, "Betai=", betai PRINT #1, "Betao=", betao PRINT #1, "Input layer Weights" FOR i = 1 TO 69 PRINT #1, W(i, 1, 0), W(i, 2, 0), W(i, 3, 0), W(i, 4, 0), W(i, 5, 0), W(i, 6, 0) NEXT i PRINT #1, " " PRINT #1, "Hidden layer Weights" FOR i = 1 TO 6 PRINT #1, W(i, 1, 1) NEXT i PRINT #1, " " PRINT #1, "Test Results" FOR ii = 1 TO ncases PRINT nam$(ii), Result(ii) * 10 PRINT #1, nam$(ii), Result(ii) * 10 NEXT ii PRINT #1, "TOTAL--------", ovrres * 100, "%" CLOSE #1 RETURN REM **********************************************