on master help sign in
Merge pull request #49 from HuwCampbell/topic/exact-hotmap

Topic/exact hotmap

commitfe8a5ff37f920069748e2c8f8ac0be70ff431f3d authorHuw Campbell <huw.campbell@gmail.com> dateThu 4 Jan 2018, 21:35 (9 years ago) parent96a45069d9dfac
3 files changed+9−8
examples/main/shakespeare.hs+4−1 grenade.cabal−3 src/Grenade/Utils/OneHot.hs+5−4
examples/main/shakespeare.hs+4−1
@@ -41,6 +41,9 @@ import System.IO.Unsafe ( unsafeInterleaveIO )
4141-- This network is able to learn and generate simple words in
4242-- about an hour.
4343--
44+-- Grab the input from
45+-- https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt
46+--
4447-- This is a first class recurrent net.
4548--
4649-- 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
6972loadShakespeare path = do
7073 contents <- lift $ readFile path
7174 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
7376 hot <- ExceptT . return . note "Couldn't generate hot values" $ traverse (`M.lookup` m) annotated
7477 return (V.fromList hot, m, cs)
7578
grenade.cabal−3
@@ -42,12 +42,9 @@ library
4242 , containers >= 0.5 && < 0.6
4343 , cereal >= 0.5 && < 0.6
4444 , deepseq >= 1.4 && < 1.5
45− , exceptions == 0.8.*
4645 , hmatrix == 0.18.*
4746 , MonadRandom >= 0.4 && < 0.6
48− , mtl >= 2.2.1 && < 2.3
4947 , primitive >= 0.6 && < 0.7
50− , text == 1.2.*
5148 , singletons >= 2.1 && < 2.4
5249 , vector >= 0.11 && < 0.13
5350
src/Grenade/Utils/OneHot.hs+5−4
@@ -52,15 +52,16 @@ oneHot hot =
5252
5353-- | Create a one hot map from any enumerable.
5454-- 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)
5656hotMap n as =
5757 let len = fromIntegral $ natVal n
5858 uniq = [ c | (c:_) <- group $ sort as]
5959 hotl = length uniq
60− in if hotl <= len
60+ in if hotl == len
6161 then
62− Just (M.fromList $ zip uniq [0..], V.fromList uniq)
63− else Nothing
62+ Right (M.fromList $ zip uniq [0..], V.fromList uniq)
63+ else
64+ Left ("Couldn't create hotMap of size " ++ show len ++ " from vector with " ++ show hotl ++ " unique characters")
6465
6566-- | From a map and value, create a 1D Shape
6667-- with one index hot (1) with the rest 0.