horde-ad-0.2.0.0: Higher Order Reverse Derivatives Efficiently - Automatic Differentiation
Safe HaskellNone
LanguageGHC2024

MnistCnnShaped2

Description

Shaped tensor-based implementation of Convolutional Neural Network for classification of MNIST digits. Sports 2 hidden layers.

With the current CPU backend it's slow enough that it's hard to see if it trains.

Synopsis

Documentation

type ADCnnMnistParametersShaped (target :: Target) (h :: Natural) (w :: Natural) (kh :: Natural) (kw :: Natural) (c_out :: Natural) (n_hidden :: Natural) r = ((target (TKS '[c_out, 1, kh + 1, kw + 1] r), target (TKS '[c_out] r)), (target (TKS '[c_out, c_out, kh + 1, kw + 1] r), target (TKS '[c_out] r)), (target (TKS '[n_hidden, (c_out * Div h 4) * Div w 4] r), target (TKS '[n_hidden] r)), (target (TKS '[SizeMnistLabel, n_hidden] r), target (TKS '[SizeMnistLabel] r))) Source #

The differentiable type of all trainable parameters of this nn. Shaped version, statically checking all dimension widths.

Due to subtraction complicating posititive number type inference, kh denotes kernel height minus one and analogously kw is kernel width minus one.

convMnistLayerS :: forall (kh :: Natural) (kw :: Natural) (h :: Nat) (w :: Nat) (c_in :: Nat) (c_out :: Nat) (batch_size :: Nat) target r. (1 <= kh, 1 <= kw, ADReady target, GoodScalar r, Differentiable r) => SNat kh -> SNat kw -> SNat h -> SNat w -> SNat c_in -> SNat c_out -> SNat batch_size -> target (TKS '[c_out, c_in, kh + 1, kw + 1] r) -> target (TKS '[batch_size, c_in, h, w] r) -> target (TKS '[c_out] r) -> target (TKS '[batch_size, c_out, Div h 2, Div w 2] r) Source #

A single convolutional layer with relu and maxPool. The c_in type parameter is going to be alwayst 1, meaning grayscale, but this function works for any c_in.

convMnistTwoS Source #

Arguments

:: forall (kh :: Natural) (kw :: Natural) (h :: Nat) (w :: Nat) (c_out :: Nat) (n_hidden :: Nat) (batch_size :: Nat) target r. (1 <= kh, 1 <= kw, ADReady target, GoodScalar r, Differentiable r) 
=> SNat kh 
-> SNat kw 
-> SNat h 
-> SNat w 
-> SNat c_out 
-> SNat n_hidden 
-> SNat batch_size

these boilerplate lines tie type parameters to the corresponding SNat value parameters denoting basic dimensions

-> PrimalOf target (TKS '[batch_size, 1, h, w] r)

input images

-> ADCnnMnistParametersShaped target h w kh kw c_out n_hidden r

parameters

-> target (TKS '[SizeMnistLabel, batch_size] r)

output classification

Composition of two convolutional layers.

convMnistLossFusedS :: forall (kh :: Natural) (kw :: Natural) (h :: Nat) (w :: Nat) (c_out :: Nat) (n_hidden :: Nat) (batch_size :: Nat) target r. (h ~ SizeMnistHeight, w ~ SizeMnistWidth, 1 <= kh, 1 <= kw, ADReady target, ADReady (PrimalOf target), GoodScalar r, Differentiable r) => SNat kh -> SNat kw -> SNat c_out -> SNat n_hidden -> SNat batch_size -> (PrimalOf target (TKS '[batch_size, h, w] r), PrimalOf target (TKS '[batch_size, SizeMnistLabel] r)) -> ADCnnMnistParametersShaped target h w kh kw c_out n_hidden r -> target ('TKScalar r) Source #

The neural network composed with the SoftMax-CrossEntropy loss function.

convMnistTestS :: forall (kh :: Natural) (kw :: Natural) (h :: Nat) (w :: Nat) (c_out :: Nat) (n_hidden :: Nat) (batch_size :: Nat) target r. (h ~ SizeMnistHeight, w ~ SizeMnistWidth, 1 <= kh, 1 <= kw, target ~ Concrete, GoodScalar r, Differentiable r) => SNat kh -> SNat kw -> SNat c_out -> SNat n_hidden -> SNat batch_size -> MnistDataBatchS batch_size r -> ADCnnMnistParametersShaped target h w kh kw c_out n_hidden r -> r Source #

A function testing the neural network given testing set of inputs and the trained parameters.