loadNet ("SNNS_TD_NET.net")
#loadNet ("best.net")
loadPattern ("/home/reczko/snns/foukas/dat/tra4000-win37.pat")
loadPattern ("/home/reczko/snns/foukas/dat/val500-win37.pat")
resfile := "test.res"
ep := 0
done := 0
nonBest := 0

setSeed(4711)
setInitFunc( "Randomize_Weights", 0.05, -0.05)
initNet ()
#setLearnFunc( "TimeDelayBackprop", 0.0000005, 0.0 )
# setLearnFunc( "TimeDelayBackprop", 0.00000025, 0.0 )
#setLearnFunc( "TimeDelayBackprop", 0.0000003, 0.4 )
#setLearnFunc( "TimeDelayBackprop", 0.0002, 0.0 )
#setLearnFunc( "TimeDelayBackprop", 0.002, 0.0 )
setLearnFunc( "TimeDelayBatchBackprop", 0.001, 0.05,0.9 )
#setLearnFunc( "TimeDelayBatchBackprop", 0.0005, 0.05,0.99 )
#setLearnFunc( "TimeDelayBatchBackprop", 0.0002, 0.0,0.1 )
#setLearnFunc( "TimeDelayBatchBackprop", 0.00008, 0.05,0.75 )
nonbest := 0
havebest := 0
phase := 1 # adjust learning parameters after 1st phase

setShuffle(TRUE)
repeat

 setPattern ("/home/reczko/snns/foukas/dat/tra4000-win37.pat")
 for i := 1 to 2 do
  trainNet ()
  print(SSE)
 endfor
 saveNet("test.net")
# loadNet("test.net")

 saveResult (resfile, 1, PAT, FALSE, TRUE, "create")
 command := "/home/reczko/snns/SNNSoS-0.1/SNNSv4.3/tools/sources/analyze -s -e WTA -i " + resfile + " | awk -f /home/reczko/snns/SNNSoS-0.1/SNNSv4.3/tools/sources/analyze.gawk"
 execute(command, w, r, u, e)
 print("tra-set wrong: ",w, " right: ",r, " unknown: ",u, " error: ",e)

 setPattern ("/home/reczko/snns/foukas/dat/val500-win37.pat")
#testNet ()
 saveResult (resfile, 1, PAT, FALSE, TRUE, "create")
 command := "/home/reczko/snns/SNNSoS-0.1/SNNSv4.3/tools/sources/analyze -s -e WTA -i " + resfile + " | awk -f /home/reczko/snns/SNNSoS-0.1/SNNSv4.3/tools/sources/analyze.gawk"
 execute(command, w, r, u, e)
 print("ep: ",ep, " wrong: ",w, " right: ",r, " unknown: ",u, " error: ",e)

 if (ep == 0) then
  bestr := r
 else
  if (r > bestr) then
    bestr := r
    saveNet("best.net")
    execute("cp test.res best.res")
    nonBest := 0
    havebest := 1
  else
    nonBest := nonBest +1
    if (havebest == 0) then maxeep := 250 else maxeep := 90 endif
    if (nonBest > maxeep) then
       if (phase == 1) then
           execute("cp best.net best-phase1.net")
	   setLearnFunc( "TimeDelayBatchBackprop", 0.0005, 0.05,0.99 )
	   phase := 2
	   nonBest := 0
       else	   
           done := 1
       endif
    endif
  endif
 endif
 ep := ep + 1

until ((done == 1) or (SIGNAL == 2))
saveNet("last.net")
