1 Main.hs for backprop simulation
3 Date started: 10th November 1992.
5 This main module initialises, runs and gets results from the
6 backpropagation functions and values.
9 > import ReadLists (readWhiteList)
10 > import Numeric(showFFloat)
12 -------------------------------------------------------------------------------
14 -------------------------------------------------------------------------------
15 The following constants set the training problem and parameters:
16 name - the name of the file
17 dimensions - the layered network topology
18 eta - the learning rate
19 accepterr - the level of error acceptable to stop training
20 epochs - the maximum number of epochs in training
24 > dimensions :: Dimensions
25 > dimensions = [2,2,1]
26 > eta,accepterr :: Double
33 -------------------------------------------------------------------------------
34 | IO and Main Program |
35 -------------------------------------------------------------------------------
39 > putStr (program s "")
41 > program :: String -> ShowS
43 > = let egs = readegs s
44 > ws = randweights dimensions
45 > rs = selectegs (length egs)
46 > (ws',res) = trainweights egs ws epochs accepterr eta rs
48 > showString "Examples:\n"
50 > . showString "Classification:\n"
51 > . showresults egs ws
52 > . showString "Training Error:\n"
54 > . showString "Trained Classification:\n"
55 > . showresults egs ws'
58 > program :: String -> String
61 > let egs = _scc_ "readegs" readegs s
62 > ws = _scc_ "randweights" randweights dimensions
63 > rs = _scc_ "selectegs" selectegs (length egs)
64 > (ws',res) = _scc_ "trainweights" trainweights egs ws epochs accepterr eta rs
66 > ++ _scc_ "showegs" showegs egs
67 > ++ "Classification:\n"
68 > ++ _scc_ "showresults" showresults egs ws
69 > ++ "Training Error:\n"
70 > ++ _scc_ "showerr" showerr res
71 > ++ "Trained Classification:\n"
72 > ++ _scc_ "showresults2" showresults egs ws'
76 -------------------------------------------------------------------------------
78 -------------------------------------------------------------------------------
80 > showdouble :: Double -> ShowS
81 > showdouble = showFFloat (Just 4)
83 > showdoubles :: [Double] -> ShowS
84 > showdoubles [] = showString ""
85 > showdoubles (v:vs) = showdouble v . showdoubles vs
87 > showegs :: Egs -> ShowS
88 > showegs [] = showString "\n"
90 > = showdoubles x . showString " " . showdoubles t . showString "\n" . showegs egs
92 > showresults :: Egs -> Weights -> ShowS
93 > showresults [] _ = showString "\n"
94 > showresults ((x,t):egs) ws
95 > = let y = last (classeg ws x)
98 > in shows p . showString " " . showdouble (y!!p) . showString " " .
99 > shows c . showString " " . showdouble (t!!c) . showString "\n" . showresults egs ws
101 > showerr :: [Double] -> ShowS
102 > showerr [] = showString ""
103 > showerr (x:xs) = showerr xs . showdouble x . showString "\n"
105 > showweights :: Weights -> ShowS
106 > showweights [] = showString "\n"
107 > showweights (w:ws) = showweight w . showweights ws
109 > showweight, showl :: Weight -> ShowS
110 > showweight [] = showString "[]\n"
111 > showweight (x:xs) = showString "[" . showdoubles x . showl xs
113 > showl [] = showString "]\n"
114 > showl (x:xs) = showString "\n " . showdoubles x . showl xs
117 > showdouble :: Double -> String
118 > showdouble v = printf "%6.4f " [UDouble v]
120 > showdoubles :: [Double] -> String
121 > showdoubles [] = ""
122 > showdoubles (v:vs) = showdouble v ++ showdoubles vs
124 > showegs :: Egs -> String
126 > showegs ((x,t):egs)
127 > = (showdoubles x) ++ " " ++ (showdoubles t) ++ "\n" ++ showegs egs
129 > showresults :: Egs -> Weights -> String
130 > showresults [] _ = "\n"
131 > showresults ((x,t):egs) ws
132 > = let y = last (classeg ws x)
135 > in show p ++ " " ++ showdouble (y!!p) ++ " " ++
136 > show c ++ " " ++ showdouble (t!!c) ++ "\n" ++ showresults egs ws
138 > showerr :: [Double] -> String
140 > showerr (x:xs) = showerr xs ++ showdouble x ++ "\n"
142 > showweights :: Weights -> String
143 > showweights [] = "\n"
144 > showweights (w:ws) = showweight w ++ showweights ws
145 > showweight, showl :: Weight -> String
146 > showweight [] = "[]\n"
147 > showweight (x:xs) = "[" ++ showdoubles x ++ showl xs
149 > showl (x:xs) = "\n " ++ showdoubles x ++ showl xs
152 -------------------------------------------------------------------------------
153 | Data Reading Functions |
154 -------------------------------------------------------------------------------
156 > readegs :: String -> Egs
157 > readegs s = readData (readWhiteList s)
159 > readData :: [Double] -> Egs
161 > readData bs = let (inp, bs') = splitAt (head dimensions) bs
162 > (out, bs'') = splitAt (last dimensions) bs'
163 > in (inp,out) : (readData bs'')