Регистрация | Вход
Print("select screen mode");Print("1 640x480");Print("2 800x600");g_mode=Input("> ");If g_mode=1 Then g_width=640:g_height=480If g_mode=2 Then g_width=800:g_height=600Graphics g_width, g_height, 0, 2 ; AAH I REMEMBER TYPING THIS SetFont(LoadFont("system"));Global n_weights=10000Global n_link=100Dim weights%(n_weights,n_link)Global MAX_WORDS=0Dim knowledge$(100000);Global g_text_pos = 0;Global g_text$;Global g_text_word$;; #################################; Pretty bad, so let us train the networkGlobal s_out$=""Global s_dice$=""Global max_res#=0Global last_word$=""f = FileOpen ("loc.txt")Global i_word_$=ReadLine(f); load rus "i"CloseFile(f);AddToKnowledgeWord("test")ReadKnowledge();LoadNet()Global word$;Dim words$(100)Global words_nFunction Weights_add_weight(n,v2)For i=1 To n_linkIf weights(n,i)=v2 Then ReturnNextFor i=1 To n_linkIf weights(n,i)=0 Then weights(n,i)=v2:ReturnNextEnd FunctionWhile (1); Print "Press key to train the network!";WaitKey()ReadKnowledge();Locate(0,GraphicsHeight())user_text$=Input("> ")TextBegin(user_text$)While TextReadWord()AddToKnowledgeWord(g_text_word)WendSeedRnd(MilliSecs())Log_add("> "+user_text)TextBegin(user_text$);last_word$=""words_n=0While TextReadWord()last_word=g_text_word;words_n=words_n+1words(words_n)=g_text_wordIf g_text_word="locate" Then Print location;If g_text_word="dice" Then d=Rand(1,6):Print "dice "+d : Log_add("dice "+d)next_word$=Text_get_next_word$();If next_word$<>"" Then Weights_add_weight(GetVec(g_text_word),GetVec(next_word))Wend; now our loss function should be much smaller; If it isn't try playing around with the parameters; #################################; #################################; The network is trained; Print "Press key for a test run of the trained network!";WaitKey()s_dice$=""s_out$="";r=8;Rand(1,7)dice=0word$=last_word;; Print "max res "+max_resFor i = 1 To r;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;word=if(word1<>""n_attemp=1.a1 n_attemp=n_attemp+1 ;res% = ProcessInputData(GetVec(word)) ; get network results ;If res<0.0 Then res=res*-1; ;If res>max_res Then max_res=res; ;res=res/Float(max_res)*Float(MAX_WORDS); For j=1 To n_link If weights(GetVec(word),j)=0 Exit Next j=j-1 If word=i_word_ Then Print "i_word_ "+j res=weights(GetVec(word),Rand(1,j)) s_$=VecToString(res) If s_="" And n_attemp<100 Then word=words(Rand(1,words_n)):Goto a1; If s_="" Then s_=knowledge(Rand(1,MAX_WORDS)):Print "use Rand": Print res word$=s_$ last_word=word$; Print "res "+res s_out=s_out+s_+" "; If s_="dice" Then s_out=s_out+dice=Rand(1,6)+" "; Next Log_add(s_out) Print s_out If dice Then Print dice; s_out=""; If s_dice Then s_out="AI dice "+s_dice;SaveNet()Wend; Print "Press key to end the program!";WaitKey(); See? much better now!; #################################End; bye!Function Log_add(s$)ls$=""TextBegin(s$)While TextReadWord()ls=ls+g_text_word+" ";WendIf ls="" Or ls=" " Then Returnf=FileOpen("log.txt")SeekFile(f, FileSize("log.txt"));WriteLine(f, ls) CloseFile(f);End Function Function GetVec(s$)For i=1 To MAX_WORDSIf knowledge(i)=s$ Return i; Print "Get_vec "+i:NextReturn 0End FunctionFunction VecToString$(v%)Return knowledge(v)If v<0 Then v=v*-1If v>MAX_WORDS Then v=0 If v>=0 And v<=MAX_WORDS Then Return knowledge(v)Return ""End Function Function LoadNet()f=ReadFile("weights.sav")If f=0 Return;CloseFile (f)file1 = FileOpen("max_res.sav");max_res=ReadFloat(file1)CloseFile(file1)file1 = FileOpen("weights.sav");For i=1 To n_weights;For j=1 To n_link;weights(i,j)=ReadInt(file1)NextNextCloseFile(file1);End FunctionFunction SaveNet()file1 = FileOpen("max_res.sav");WriteFloat(file1,max_res)CloseFile(file1)file1 = FileOpen("weights.sav");For i=1 To n_weights;For j=1 To n_link;WriteInt(file1,weights(i,j))NextNextCloseFile(file1);End FunctionFunction ReadKnowledge()MAX_WORDS=0file1=FileOpen("knowledge.txt");While(Eof(file1)<>1)ReadLine(file1)MAX_WORDS=MAX_WORDS+1WendCloseFile(file1) file1 = FileOpen("knowledge.txt");For i=1 To MAX_WORDSknowledge(i) = ReadLine(file1);NextCloseFile(file1);End FunctionFunction AddToKnowledgeWord(s$)If Word_Qu(s)>0 ReturnIf(s = "") Return f=FileOpen("knowledge.txt")If MAX_WORDS SeekFile(f, FileSize("knowledge.txt")); WriteLine(f, s) MAX_WORDS=MAX_WORDS+1; Print MAX_WORDSCloseFile(f);ReadKnowledge()End FunctionFunction Word_Qu(w$)q=0ReadKnowledge()For i=1 To MAX_WORDSIf w=knowledge(i) q=q+1Next Return qEnd FunctionFunction FileOpen(fn$)file1 = OpenFile(fn$);If(file1=0)file1=WriteFile(fn$);Return file1;End Function;------------------Function TextBegin(text_$)g_text=text_g_text_pos = 1 g_text_prev_word_pos = 1g_text_word=""End Function Function TextReadWord() g_text_word ="" If (g_text_pos > Len(g_text))Return 0;While (Mid(g_text, g_text_pos, 1) = " ") : g_text_pos = g_text_pos + 1 : Wend While (Mid(g_text,g_text_pos,1) <> " " And g_text_pos<=Len(g_text)) g_text_word =g_text_word+ Mid(g_text,g_text_pos,1); g_text_pos=g_text_pos+1 Wend g_text_pos=g_text_pos+1; Return 1;End FunctionFunction Text_get_next_word$()g_text_prev_word_pos=g_text_posprev_word$=g_text_wordTextReadWord()w$=g_text_wordg_text_word=prev_wordg_text_pos=g_text_prev_word_posReturn wEnd Function; simple min functionFunction min#(a#, b#) If a < b Return a Else Return b EndIfEnd Function; simple max functionFunction max#(a#, b#) If a > b Return a Else Return b EndIfEnd Function; to_approximate; INPUT:; x# - the function input value; RETURNS:; the value that the function computes;; This is the function we want our network to approximate; I used a simple x^2, you can try different ones; CHANGEMEFunction to_approximate#(x#) Return x*xEnd Function; ################################# <- I had to insert this or I would have gone mad