@@ -8,6 +8,9 @@ import Control.Monad
88import Control.Monad.Random
99import Data.List ( foldl' )
1010
11+import qualified Data.ByteString as B
12+import Data.Serialize
13+
1114import GHC.TypeLits
1215
1316import qualified Numeric.LinearAlgebra.Static as SA
@@ -16,13 +19,12 @@ import Options.Applicative
1619
1720import Grenade
1821
19−-- The defininition for our simple feed forward network.
20−-- The type level list represents the shapes passed through the layers. One can see that for this demonstration
21−-- we are using relu, tanh and logit non-linear units, which can be easily subsituted for each other in and out.
22−
23−-- It's important to keep the type signatures, as there's many layers which can "squeeze" into the gaps
24−-- between the shapes, so inference can't do it all for us.
2522
23+-- The defininition for our simple feed forward network.
24+-- The type level lists represents the layers and the shapes passed through the layers.
25+-- One can see that for this demonstration we are using relu, tanh and logit non-linear
26+-- units, which can be easily subsituted for each other in and out.
27+--
2628-- With around 100000 examples, this should show two clear circles which have been learned by the network.
2729type FFNet = Network '[ FullyConnected 2 40, Tanh, FullyConnected 40 10, Relu, FullyConnected 10 1, Logit ]
2830 '[ 'D1 2, 'D1 40, 'D1 40, 'D1 10, 'D1 10, 'D1 1, 'D1 1]
@@ -30,8 +32,8 @@ type FFNet = Network '[ FullyConnected 2 40, Tanh, FullyConnected 40 10, Relu, F
3032randomNet :: MonadRandom m => m FFNet
3133randomNet = randomNetwork
3234
33−netTest :: MonadRandom m => LearningParameters -> Int -> m String
34−netTest rate n = do
35+netTrain :: FFNet -> LearningParameters -> Int -> IO FFNet
36+netTrain net0 rate n = do
3537 inps <- replicateM n $ do
3638 s <- getRandom
3739 return $ S1D $ SA.randomVector s SA.Uniform * 2 - 1
@@ -39,20 +41,28 @@ netTest rate n = do
3941 if v `inCircle` (fromRational 0.33, 0.33) || v `inCircle` (fromRational (-0.33), 0.33)
4042 then S1D $ fromRational 1
4143 else S1D $ fromRational 0
42− net0 <- randomNet
4344
4445 let trained = foldl' trainEach net0 (zip inps outs)
45− let testIns = [ [ (x,y) | x <- [0..50] ]
46− | y <- [0..20] ]
47−
48− let outMat = fmap (fmap (\(x,y) -> (render . normx) $ runNet trained (S1D $ SA.vector [x / 25 - 1,y / 10 - 1]))) testIns
49− return $ unlines outMat
46+ return trained
5047
5148 where
5249 inCircle :: KnownNat n => SA.R n -> (SA.R n, Double) -> Bool
5350 v `inCircle` (o, r) = SA.norm_2 (v - o) <= r
5451 trainEach !network (i,o) = train rate network i o
5552
53+netLoad :: FilePath -> IO FFNet
54+netLoad modelPath = do
55+ modelData <- B.readFile modelPath
56+ either fail return $ runGet (get :: Get FFNet) modelData
57+
58+netScore :: FFNet -> IO ()
59+netScore network = do
60+ let testIns = [ [ (x,y) | x <- [0..50] ]
61+ | y <- [0..20] ]
62+ outMat = fmap (fmap (\(x,y) -> (render . normx) $ runNet network (S1D $ SA.vector [x / 25 - 1,y / 10 - 1]))) testIns
63+ putStrLn $ unlines outMat
64+
65+ where
5666 render n' | n' <= 0.2 = ' '
5767 | n' <= 0.4 = '.'
5868 | n' <= 0.6 = '-'
@@ -62,7 +72,7 @@ netTest rate n = do
6272 normx :: S ('D1 1) -> Double
6373 normx (S1D r) = SA.mean r
6474
65−data FeedForwardOpts = FeedForwardOpts Int LearningParameters
75+data FeedForwardOpts = FeedForwardOpts Int LearningParameters (Maybe FilePath) (Maybe FilePath)
6676
6777feedForward' :: Parser FeedForwardOpts
6878feedForward' =
@@ -72,9 +82,19 @@ feedForward' =
7282 <*> option auto (long "momentum" <> value 0.9)
7383 <*> option auto (long "l2" <> value 0.0005)
7484 )
85+ <*> optional (strOption (long "load"))
86+ <*> optional (strOption (long "save"))
7587
7688main :: IO ()
7789main = do
78− FeedForwardOpts examples rate <- execParser (info (feedForward' <**> helper) idm)
79− putStrLn "Training network..."
80− putStrLn =<< evalRandIO (netTest rate examples)
90+ FeedForwardOpts examples rate load save <- execParser (info (feedForward' <**> helper) idm)
91+ net0 <- case load of
92+ Just loadFile -> netLoad loadFile
93+ Nothing -> randomNet
94+
95+ net <- netTrain net0 rate examples
96+ netScore net
97+
98+ case save of
99+ Just saveFile -> B.writeFile saveFile $ runPut (put net)
100+ Nothing -> return ()