on master help sign in
Merge pull request #49 from HuwCampbell/topic/exact-hotmap
fe8a5ffHuw Campbell
examples/main/shakespeare.hs+4−1
@@ -41,6 +41,9 @@ import System.IO.Unsafe ( unsafeInterleaveIO )
41-- This network is able to learn and generate simple words in
41-- This network is able to learn and generate simple words in
42-- about an hour.
42-- about an hour.
43--
43--
44+-- Grab the input from
45+-- https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt
46+--
44-- This is a first class recurrent net.
47-- This is a first class recurrent net.
45--
48--
46-- The F and R types are tagging types to ensure that the runner and
49-- The F and R types are tagging types to ensure that the runner and
@@ -69,7 +72,7 @@ loadShakespeare :: FilePath -> ExceptT String IO (Vector Int, M.Map Char Int, Ve
69loadShakespeare path = do
72loadShakespeare path = do
70 contents <- lift $ readFile path
73 contents <- lift $ readFile path
71 let annotated = annotateCapitals contents
74 let annotated = annotateCapitals contents
72− (m,cs) <- ExceptT . return . note "Couldn't fit data in hotMap" $ hotMap (Proxy :: Proxy 40) annotated
75+ (m,cs) <- ExceptT . return $ hotMap (Proxy :: Proxy 40) annotated
73 hot <- ExceptT . return . note "Couldn't generate hot values" $ traverse (`M.lookup` m) annotated
76 hot <- ExceptT . return . note "Couldn't generate hot values" $ traverse (`M.lookup` m) annotated
74 return (V.fromList hot, m, cs)
77 return (V.fromList hot, m, cs)
75
78
grenade.cabal−3
@@ -42,12 +42,9 @@ library
42 , containers >= 0.5 && < 0.6
42 , containers >= 0.5 && < 0.6
43 , cereal >= 0.5 && < 0.6
43 , cereal >= 0.5 && < 0.6
44 , deepseq >= 1.4 && < 1.5
44 , deepseq >= 1.4 && < 1.5
45− , exceptions == 0.8.*
46 , hmatrix == 0.18.*
45 , hmatrix == 0.18.*
47 , MonadRandom >= 0.4 && < 0.6
46 , MonadRandom >= 0.4 && < 0.6
48− , mtl >= 2.2.1 && < 2.3
49 , primitive >= 0.6 && < 0.7
47 , primitive >= 0.6 && < 0.7
50− , text == 1.2.*
51 , singletons >= 2.1 && < 2.4
48 , singletons >= 2.1 && < 2.4
52 , vector >= 0.11 && < 0.13
49 , vector >= 0.11 && < 0.13
53
50
src/Grenade/Utils/OneHot.hs+5−4
@@ -52,15 +52,16 @@ oneHot hot =
52
52
53-- | Create a one hot map from any enumerable.
53-- | Create a one hot map from any enumerable.
54-- Returns a map, and the ordered list for the reverse transformation
54-- Returns a map, and the ordered list for the reverse transformation
55−hotMap :: (Ord a, KnownNat n) => Proxy n -> [a] -> Maybe (Map a Int, Vector a)
55+hotMap :: (Ord a, KnownNat n) => Proxy n -> [a] -> Either String (Map a Int, Vector a)
56hotMap n as =
56hotMap n as =
57 let len = fromIntegral $ natVal n
57 let len = fromIntegral $ natVal n
58 uniq = [ c | (c:_) <- group $ sort as]
58 uniq = [ c | (c:_) <- group $ sort as]
59 hotl = length uniq
59 hotl = length uniq
60− in if hotl <= len
60+ in if hotl == len
61 then
61 then
62− Just (M.fromList $ zip uniq [0..], V.fromList uniq)
62+ Right (M.fromList $ zip uniq [0..], V.fromList uniq)
63− else Nothing
63+ else
64+ Left ("Couldn't create hotMap of size " ++ show len ++ " from vector with " ++ show hotl ++ " unique characters")
64
65
65-- | From a map and value, create a 1D Shape
66-- | From a map and value, create a 1D Shape
66-- with one index hot (1) with the rest 0.
67-- with one index hot (1) with the rest 0.