💣 Machine learning which might blow up in your face 💣

Updated to lts-20.18

authored by iko and committed by Erik de Castro Lopo 0e011753 3b639187

+6 -5
examples/grenade-examples.cabal
··· 26 26 , bytestring 27 27 , cereal 28 28 , either 29 - , optparse-applicative >= 0.13 && < 0.17 29 + , optparse-applicative >= 0.13 && < 0.18 30 30 , text == 1.2.* 31 31 , mtl >= 2.2.1 && < 2.3 32 32 , hmatrix ··· 42 42 , grenade 43 43 , attoparsec 44 44 , either 45 - , optparse-applicative >= 0.13 && < 0.17 45 + , optparse-applicative >= 0.13 && < 0.18 46 46 , text == 1.2.* 47 47 , mtl >= 2.2.1 && < 2.3 48 48 , hmatrix >= 0.18 && < 0.21 ··· 61 61 , bytestring 62 62 , cereal 63 63 , either 64 - , optparse-applicative >= 0.13 && < 0.17 64 + , optparse-applicative >= 0.13 && < 0.18 65 65 , text == 1.2.* 66 66 , mtl >= 2.2.1 && < 2.3 67 67 , hmatrix >= 0.18 && < 0.21 ··· 78 78 , grenade 79 79 , attoparsec 80 80 , either 81 - , optparse-applicative >= 0.13 && < 0.17 81 + , optparse-applicative >= 0.13 && < 0.18 82 82 , text == 1.2.* 83 83 , mtl >= 2.2.1 && < 2.3 84 84 , hmatrix >= 0.18 && < 0.21 ··· 96 96 , bytestring 97 97 , cereal 98 98 , either 99 - , optparse-applicative >= 0.13 && < 0.17 99 + , optparse-applicative >= 0.13 && < 0.18 100 100 , text == 1.2.* 101 101 , mtl >= 2.2.1 && < 2.3 102 102 , hmatrix >= 0.18 && < 0.21 103 103 , transformers 104 104 , semigroups 105 105 , singletons 106 + , singletons-base 106 107 , vector 107 108 , MonadRandom 108 109 , containers
+2 -7
examples/main/shakespeare.hs
··· 14 14 import Data.List ( foldl' ) 15 15 import Data.Maybe ( fromMaybe ) 16 16 17 - #if ! MIN_VERSION_base(4,13,0) 18 - import Data.Semigroup ( (<>) ) 19 - #endif 20 17 21 18 import qualified Data.Vector as V 22 19 import Data.Vector ( Vector ) 23 20 24 21 import qualified Data.Map as M 25 - #if ! MIN_VERSION_base(4,13,0) 26 - import Data.Proxy ( Proxy (..) ) 27 - #endif 28 22 29 23 import qualified Data.ByteString as B 30 24 import Data.Serialize 31 25 32 - import Data.Singletons.Prelude 33 26 import GHC.TypeLits 34 27 35 28 import Numeric.LinearAlgebra.Static ( konst ) ··· 41 34 import Grenade.Utils.OneHot 42 35 43 36 import System.IO.Unsafe ( unsafeInterleaveIO ) 37 + import Data.Proxy 38 + import Prelude.Singletons 44 39 45 40 -- The defininition for our natural language recurrent network. 46 41 -- This network is able to learn and generate simple words in
+2 -1
grenade.cabal
··· 38 38 library 39 39 build-depends: 40 40 base >= 4.8 && < 5 41 - , bytestring == 0.10.* 41 + , bytestring >= 0.10.0 42 42 , containers >= 0.5 && < 0.7 43 43 , cereal >= 0.5 && < 0.6 44 44 , deepseq >= 1.4 && < 1.5 ··· 48 48 -- Versions of singletons are *tightly* coupled with the 49 49 -- GHC version so its fine to drop version bounds. 50 50 , singletons 51 + , singletons-base 51 52 , vector >= 0.11 && < 0.13 52 53 53 54 ghc-options:
+1 -1
src/Grenade/Core/Network.hs
··· 34 34 import Control.Monad.Random ( MonadRandom ) 35 35 36 36 import Data.Singletons 37 - import Data.Singletons.Prelude 38 37 import Data.Serialize 39 38 40 39 #if MIN_VERSION_base(4,9,0) ··· 44 43 import Grenade.Core.Layer 45 44 import Grenade.Core.LearningParameters 46 45 import Grenade.Core.Shape 46 + import Prelude.Singletons 47 47 48 48 -- | Type of a network. 49 49 --
+2 -1
src/Grenade/Core/Runner.hs
··· 13 13 , runNet 14 14 ) where 15 15 16 - import Data.Singletons.Prelude 17 16 18 17 import Grenade.Core.LearningParameters 19 18 import Grenade.Core.Network 20 19 import Grenade.Core.Shape 20 + import Data.Singletons 21 + import Prelude.Singletons 21 22 22 23 -- | Perform reverse automatic differentiation on the network 23 24 -- for the current input and expected output.
+16 -31
src/Grenade/Core/Shape.hs
··· 21 21 module Grenade.Core.Shape ( 22 22 S (..) 23 23 , Shape (..) 24 - #if MIN_VERSION_singletons(2,6,0) 25 24 , SShape (..) 26 - #else 27 - , Sing (..) 28 - #endif 29 - 30 25 , randomOfShape 31 26 , fromStorable 32 27 ) where 33 28 34 29 import Control.DeepSeq (NFData (..)) 35 30 import Control.Monad.Random ( MonadRandom, getRandom ) 36 - 37 - #if MIN_VERSION_base(4,13,0) 38 31 import Data.Kind (Type) 39 - #endif 40 32 import Data.Proxy 41 33 import Data.Serialize 42 34 import Data.Singletons 43 - import Data.Singletons.TypeLits 44 35 import Data.Vector.Storable ( Vector ) 45 36 import qualified Data.Vector.Storable as V 46 - 47 - #if MIN_VERSION_base(4,11,0) 48 - import GHC.TypeLits hiding (natVal) 49 - #else 50 37 import GHC.TypeLits 51 - #endif 52 - 53 38 import qualified Numeric.LinearAlgebra.Static as H 54 39 import Numeric.LinearAlgebra.Static 55 40 import qualified Numeric.LinearAlgebra as NLA ··· 99 84 type instance Sing = SShape 100 85 101 86 data SShape :: Shape -> Type where 102 - D1Sing :: Sing a -> SShape ('D1 a) 103 - D2Sing :: Sing a -> Sing b -> SShape ('D2 a b) 104 - D3Sing :: KnownNat (a * c) => Sing a -> Sing b -> Sing c -> SShape ('D3 a b c) 87 + D1Sing :: KnownNat a => SShape ('D1 a) 88 + D2Sing :: (KnownNat a, KnownNat b) => SShape ('D2 a b) 89 + D3Sing :: (KnownNat (a * c), KnownNat a, KnownNat b, KnownNat c) => SShape ('D3 a b c) 105 90 #else 106 91 data instance Sing (n :: Shape) where 107 92 D1Sing :: Sing a -> Sing ('D1 a) ··· 110 95 #endif 111 96 112 97 instance KnownNat a => SingI ('D1 a) where 113 - sing = D1Sing sing 98 + sing = D1Sing 114 99 instance (KnownNat a, KnownNat b) => SingI ('D2 a b) where 115 - sing = D2Sing sing sing 100 + sing = D2Sing 116 101 instance (KnownNat a, KnownNat b, KnownNat c, KnownNat (a * c)) => SingI ('D3 a b c) where 117 - sing = D3Sing sing sing sing 102 + sing = D3Sing 118 103 119 104 instance SingI x => Num (S x) where 120 105 (+) = n2 (+) ··· 163 148 randomOfShape = do 164 149 seed :: Int <- getRandom 165 150 return $ case (sing :: Sing x) of 166 - D1Sing SNat -> 151 + D1Sing -> 167 152 S1D (randomVector seed Uniform * 2 - 1) 168 153 169 - D2Sing SNat SNat -> 154 + D2Sing -> 170 155 S2D (uniformSample seed (-1) 1) 171 156 172 - D3Sing SNat SNat SNat -> 157 + D3Sing -> 173 158 S3D (uniformSample seed (-1) 1) 174 159 175 160 -- | Generate a shape from a Storable Vector. ··· 177 162 -- Returns Nothing if the vector is of the wrong size. 178 163 fromStorable :: forall x. SingI x => Vector Double -> Maybe (S x) 179 164 fromStorable xs = case sing :: Sing x of 180 - D1Sing SNat -> 165 + D1Sing -> 181 166 S1D <$> H.create xs 182 167 183 - D2Sing SNat SNat -> 168 + D2Sing -> 184 169 S2D <$> mkL xs 185 170 186 - D3Sing SNat SNat SNat -> 171 + D3Sing -> 187 172 S3D <$> mkL xs 188 173 where 189 174 mkL :: forall rows columns. (KnownNat rows, KnownNat columns) ··· 220 205 n2 f (S3D x) (S3D y) = S3D (f x y) 221 206 222 207 -- Helper function for creating the number instances 223 - nk :: forall x. SingI x => Double -> S x 208 + nk :: forall x. (SingI x) => Double -> S x 224 209 nk x = case (sing :: Sing x) of 225 - D1Sing SNat -> 210 + D1Sing -> 226 211 S1D (konst x) 227 212 228 - D2Sing SNat SNat -> 213 + D2Sing -> 229 214 S2D (konst x) 230 215 231 - D3Sing SNat SNat SNat -> 216 + D3Sing -> 232 217 S3D (konst x)
-7
src/Grenade/Layers/Convolution.hs
··· 28 28 import Data.Maybe 29 29 import Data.Proxy 30 30 import Data.Serialize 31 - import Data.Singletons.TypeLits 32 31 33 - #if MIN_VERSION_base(4,11,0) 34 - import GHC.TypeLits hiding (natVal) 35 - #else 36 32 import GHC.TypeLits 37 - #endif 38 - #if MIN_VERSION_base(4,9,0) 39 33 import Data.Kind (Type) 40 - #endif 41 34 42 35 import Numeric.LinearAlgebra hiding ( uniformSample, konst ) 43 36 import qualified Numeric.LinearAlgebra as LA
-7
src/Grenade/Layers/Crop.hs
··· 21 21 import Data.Maybe 22 22 import Data.Proxy 23 23 import Data.Serialize 24 - import Data.Singletons.TypeLits 25 24 26 - #if MIN_VERSION_base(4,11,0) 27 - import GHC.TypeLits hiding (natVal) 28 - #else 29 25 import GHC.TypeLits 30 - #endif 31 - #if MIN_VERSION_base(4,9,0) 32 26 import Data.Kind (Type) 33 - #endif 34 27 35 28 import Grenade.Core 36 29 import Grenade.Layers.Internal.Pad
-7
src/Grenade/Layers/Deconvolution.hs
··· 32 32 import Data.Maybe 33 33 import Data.Proxy 34 34 import Data.Serialize 35 - import Data.Singletons.TypeLits 36 35 37 - #if MIN_VERSION_base(4,11,0) 38 - import GHC.TypeLits hiding (natVal) 39 - #else 40 36 import GHC.TypeLits 41 - #endif 42 - #if MIN_VERSION_base(4,9,0) 43 37 import Data.Kind (Type) 44 - #endif 45 38 46 39 import Numeric.LinearAlgebra hiding ( uniformSample, konst ) 47 40 import qualified Numeric.LinearAlgebra as LA
+1 -1
src/Grenade/Layers/FullyConnected.hs
··· 13 13 14 14 import Data.Proxy 15 15 import Data.Serialize 16 - import Data.Singletons.TypeLits 17 16 18 17 import qualified Numeric.LinearAlgebra as LA 19 18 import Numeric.LinearAlgebra.Static ··· 21 20 import Grenade.Core 22 21 23 22 import Grenade.Layers.Internal.Update 23 + import GHC.TypeLits 24 24 25 25 -- | A basic fully connected (or inner product) neural network layer. 26 26 data FullyConnected i o = FullyConnected
-7
src/Grenade/Layers/Pad.hs
··· 21 21 import Data.Maybe 22 22 import Data.Proxy 23 23 import Data.Serialize 24 - import Data.Singletons.TypeLits 25 24 26 - #if MIN_VERSION_base(4,11,0) 27 - import GHC.TypeLits hiding (natVal) 28 - #else 29 25 import GHC.TypeLits 30 - #endif 31 - #if MIN_VERSION_base(4,9,0) 32 26 import Data.Kind (Type) 33 - #endif 34 27 35 28 import Grenade.Core 36 29 import Grenade.Layers.Internal.Pad
-7
src/Grenade/Layers/Pooling.hs
··· 22 22 import Data.Maybe 23 23 import Data.Proxy 24 24 import Data.Serialize 25 - import Data.Singletons.TypeLits 26 25 27 - #if MIN_VERSION_base(4,11,0) 28 - import GHC.TypeLits hiding (natVal) 29 - #else 30 26 import GHC.TypeLits 31 - #endif 32 - #if MIN_VERSION_base(4,9,0) 33 27 import Data.Kind (Type) 34 - #endif 35 28 36 29 import Grenade.Core 37 30 import Grenade.Layers.Internal.Pooling
-1
src/Grenade/Layers/Reshape.hs
··· 17 17 18 18 import Data.Serialize 19 19 20 - import Data.Singletons.TypeLits 21 20 import GHC.TypeLits 22 21 23 22 import Numeric.LinearAlgebra.Static
+1 -4
src/Grenade/Recurrent/Core/Network.hs
··· 29 29 30 30 31 31 import Control.Monad.Random ( MonadRandom ) 32 - import Data.Singletons ( SingI ) 33 - import Data.Singletons.Prelude ( Head, Last ) 34 32 import Data.Serialize 35 33 36 - #if MIN_VERSION_base(4,9,0) 37 34 import Data.Kind (Type) 38 - #endif 39 35 40 36 import Grenade.Core 41 37 import Grenade.Recurrent.Core.Layer 38 + import Prelude.Singletons 42 39 43 40 -- | Witness type to say indicate we're building up with a normal feed 44 41 -- forward layer.
+1 -1
src/Grenade/Recurrent/Core/Runner.hs
··· 17 17 ) where 18 18 19 19 import Data.List ( foldl' ) 20 - import Data.Singletons.Prelude 21 20 import Grenade.Core 22 21 23 22 import Grenade.Recurrent.Core.Network 23 + import Prelude.Singletons 24 24 25 25 type RecurrentGradients layers = [RecurrentGradient layers] 26 26
-3
src/Grenade/Recurrent/Layers/BasicRecurrent.hs
··· 16 16 17 17 import Control.Monad.Random ( MonadRandom, getRandom ) 18 18 19 - import Data.Singletons.TypeLits 20 19 21 - #if MIN_VERSION_base(4,9,0) 22 20 import Data.Kind (Type) 23 - #endif 24 21 25 22 import Numeric.LinearAlgebra.Static 26 23
+1 -4
src/Grenade/Recurrent/Layers/LSTM.hs
··· 21 21 -- import Data.List ( foldl1' ) 22 22 import Data.Proxy 23 23 import Data.Serialize 24 - import Data.Singletons.TypeLits 25 - 26 - #if MIN_VERSION_base(4,9,0) 27 24 import Data.Kind (Type) 28 - #endif 29 25 30 26 import qualified Numeric.LinearAlgebra as LA 31 27 import Numeric.LinearAlgebra.Static ··· 33 29 import Grenade.Core 34 30 import Grenade.Recurrent.Core 35 31 import Grenade.Layers.Internal.Update 32 + import GHC.TypeLits 36 33 37 34 38 35 -- | Long Short Term Memory Recurrent unit
+1 -1
src/Grenade/Utils/OneHot.hs
··· 22 22 import qualified Data.Map as M 23 23 24 24 import Data.Proxy 25 - import Data.Singletons.TypeLits 26 25 27 26 import Data.Vector ( Vector ) 28 27 import qualified Data.Vector as V ··· 31 30 import Numeric.LinearAlgebra ( maxIndex ) 32 31 import Numeric.LinearAlgebra.Devel 33 32 import Numeric.LinearAlgebra.Static 33 + import GHC.TypeLits 34 34 35 35 import Grenade.Core.Shape 36 36
+7 -1
stack.yaml
··· 20 20 # 21 21 # resolver: ./custom-snapshot.yaml 22 22 # resolver: https://example.com/snapshots/2018-01-01.yaml 23 - resolver: lts-18.28 23 + resolver: lts-20.18 24 24 25 25 # User packages to be built. 26 26 # Various formats can be used as shown in the example below. ··· 70 70 # 71 71 # Allow a newer minor version of GHC than the snapshot specifies 72 72 # compiler-check: newer-minor 73 + 74 + nix: 75 + enable: true 76 + packages: 77 + - blas 78 + - lapack
+5 -5
stack.yaml.lock
··· 7 7 - completed: 8 8 hackage: typelits-witnesses-0.3.0.3@sha256:2d9df4ac6ff3077bfd2bf659e4b495e157723ac5b45c519762853f55df5c16db,2738 9 9 pantry-tree: 10 - size: 469 11 10 sha256: 6a42a462f98e94933b6e9721acd912c6c6b6a4743635efd15cc5871908c816a0 11 + size: 469 12 12 original: 13 13 hackage: typelits-witnesses-0.3.0.3 14 14 snapshots: 15 15 - completed: 16 - size: 590100 17 - url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/18/28.yaml 18 - sha256: 428ec8d5ce932190d3cbe266b9eb3c175cd81e984babf876b64019e2cbe4ea68 19 - original: lts-18.28 16 + sha256: 9fa4bece7acfac1fc7930c5d6e24606004b09e80aa0e52e9f68b148201008db9 17 + size: 649606 18 + url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/18.yaml 19 + original: lts-20.18