{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE UndecidableSuperClasses #-}
{-# LANGUAGE NoStarIsType #-}
{-# OPTIONS_GHC -fconstraint-solver-iterations=0 #-}
{-# OPTIONS_GHC -fplugin GHC.TypeLits.Extra.Solver #-}
{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver #-}
{-# OPTIONS_GHC -fplugin GHC.TypeLits.Normalise #-}

module Torch.Typed.NN.Recurrent.Cell.LSTM where

import Data.List
  ( foldl',
    scanl',
  )
import GHC.Generics
import GHC.TypeLits
import qualified Torch.DType as D
import qualified Torch.Device as D
import qualified Torch.NN as A
import Torch.Typed.Factories
import Torch.Typed.Functional hiding (linear)
import Torch.Typed.NN.Dropout
import Torch.Typed.Parameter
import Torch.Typed.Tensor

-- | A specification for a long, short-term memory (LSTM) cell.
data
  LSTMCellSpec
    (inputDim :: Nat)
    (hiddenDim :: Nat)
    (dtype :: D.DType)
    (device :: (D.DeviceType, Nat))
  = -- | Weights and biases are drawn from the standard normal distibution (having mean 0 and variance 1)
    LSTMCellSpec
  deriving (Int -> LSTMCellSpec inputDim hiddenDim dtype device -> ShowS
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
Int -> LSTMCellSpec inputDim hiddenDim dtype device -> ShowS
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
[LSTMCellSpec inputDim hiddenDim dtype device] -> ShowS
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LSTMCellSpec inputDim hiddenDim dtype device] -> ShowS
$cshowList :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
[LSTMCellSpec inputDim hiddenDim dtype device] -> ShowS
show :: LSTMCellSpec inputDim hiddenDim dtype device -> String
$cshow :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device -> String
showsPrec :: Int -> LSTMCellSpec inputDim hiddenDim dtype device -> ShowS
$cshowsPrec :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
Int -> LSTMCellSpec inputDim hiddenDim dtype device -> ShowS
Show, LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Bool
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Bool
$c/= :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Bool
== :: LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Bool
$c== :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Bool
Eq, LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Bool
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Ordering
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
Eq (LSTMCellSpec inputDim hiddenDim dtype device)
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Bool
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Ordering
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
$cmin :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
max :: LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
$cmax :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
>= :: LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Bool
$c>= :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Bool
> :: LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Bool
$c> :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Bool
<= :: LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Bool
$c<= :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Bool
< :: LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Bool
$c< :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Bool
compare :: LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Ordering
$ccompare :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device -> Ordering
Ord, forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)) x.
Rep (LSTMCellSpec inputDim hiddenDim dtype device) x
-> LSTMCellSpec inputDim hiddenDim dtype device
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)) x.
LSTMCellSpec inputDim hiddenDim dtype device
-> Rep (LSTMCellSpec inputDim hiddenDim dtype device) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)) x.
Rep (LSTMCellSpec inputDim hiddenDim dtype device) x
-> LSTMCellSpec inputDim hiddenDim dtype device
$cfrom :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)) x.
LSTMCellSpec inputDim hiddenDim dtype device
-> Rep (LSTMCellSpec inputDim hiddenDim dtype device) x
Generic, Int -> LSTMCellSpec inputDim hiddenDim dtype device
LSTMCellSpec inputDim hiddenDim dtype device -> Int
LSTMCellSpec inputDim hiddenDim dtype device
-> [LSTMCellSpec inputDim hiddenDim dtype device]
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> [LSTMCellSpec inputDim hiddenDim dtype device]
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
Int -> LSTMCellSpec inputDim hiddenDim dtype device
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device -> Int
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> [LSTMCellSpec inputDim hiddenDim dtype device]
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> [LSTMCellSpec inputDim hiddenDim dtype device]
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> [LSTMCellSpec inputDim hiddenDim dtype device]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> [LSTMCellSpec inputDim hiddenDim dtype device]
$cenumFromThenTo :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> [LSTMCellSpec inputDim hiddenDim dtype device]
enumFromTo :: LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> [LSTMCellSpec inputDim hiddenDim dtype device]
$cenumFromTo :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> [LSTMCellSpec inputDim hiddenDim dtype device]
enumFromThen :: LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> [LSTMCellSpec inputDim hiddenDim dtype device]
$cenumFromThen :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
-> [LSTMCellSpec inputDim hiddenDim dtype device]
enumFrom :: LSTMCellSpec inputDim hiddenDim dtype device
-> [LSTMCellSpec inputDim hiddenDim dtype device]
$cenumFrom :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> [LSTMCellSpec inputDim hiddenDim dtype device]
fromEnum :: LSTMCellSpec inputDim hiddenDim dtype device -> Int
$cfromEnum :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device -> Int
toEnum :: Int -> LSTMCellSpec inputDim hiddenDim dtype device
$ctoEnum :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
Int -> LSTMCellSpec inputDim hiddenDim dtype device
pred :: LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
$cpred :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
succ :: LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
$csucc :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
-> LSTMCellSpec inputDim hiddenDim dtype device
Enum, LSTMCellSpec inputDim hiddenDim dtype device
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
forall a. a -> a -> Bounded a
maxBound :: LSTMCellSpec inputDim hiddenDim dtype device
$cmaxBound :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
minBound :: LSTMCellSpec inputDim hiddenDim dtype device
$cminBound :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCellSpec inputDim hiddenDim dtype device
Bounded)

-- | A long, short-term memory cell.
data
  LSTMCell
    (inputDim :: Nat)
    (hiddenDim :: Nat)
    (dtype :: D.DType)
    (device :: (D.DeviceType, Nat)) = LSTMCell
  { -- | input-to-hidden weights
    forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCell inputDim hiddenDim dtype device
-> Parameter device dtype '[4 * hiddenDim, inputDim]
lstmCell_w_ih :: Parameter device dtype '[4 * hiddenDim, inputDim],
    -- | hidden-to-hidden weights
    forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCell inputDim hiddenDim dtype device
-> Parameter device dtype '[4 * hiddenDim, hiddenDim]
lstmCell_w_hh :: Parameter device dtype '[4 * hiddenDim, hiddenDim],
    -- | input-to-hidden bias
    forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCell inputDim hiddenDim dtype device
-> Parameter device dtype '[4 * hiddenDim]
lstmCell_b_ih :: Parameter device dtype '[4 * hiddenDim],
    -- | hidden-to-hidden bias
    forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCell inputDim hiddenDim dtype device
-> Parameter device dtype '[4 * hiddenDim]
lstmCell_b_hh :: Parameter device dtype '[4 * hiddenDim]
  }
  deriving (Int -> LSTMCell inputDim hiddenDim dtype device -> ShowS
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
Int -> LSTMCell inputDim hiddenDim dtype device -> ShowS
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
[LSTMCell inputDim hiddenDim dtype device] -> ShowS
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCell inputDim hiddenDim dtype device -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LSTMCell inputDim hiddenDim dtype device] -> ShowS
$cshowList :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
[LSTMCell inputDim hiddenDim dtype device] -> ShowS
show :: LSTMCell inputDim hiddenDim dtype device -> String
$cshow :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCell inputDim hiddenDim dtype device -> String
showsPrec :: Int -> LSTMCell inputDim hiddenDim dtype device -> ShowS
$cshowsPrec :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
Int -> LSTMCell inputDim hiddenDim dtype device -> ShowS
Show, forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)) x.
Rep (LSTMCell inputDim hiddenDim dtype device) x
-> LSTMCell inputDim hiddenDim dtype device
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)) x.
LSTMCell inputDim hiddenDim dtype device
-> Rep (LSTMCell inputDim hiddenDim dtype device) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)) x.
Rep (LSTMCell inputDim hiddenDim dtype device) x
-> LSTMCell inputDim hiddenDim dtype device
$cfrom :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)) x.
LSTMCell inputDim hiddenDim dtype device
-> Rep (LSTMCell inputDim hiddenDim dtype device) x
Generic, forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCell inputDim hiddenDim dtype device
-> HList (Parameters (LSTMCell inputDim hiddenDim dtype device))
forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCell inputDim hiddenDim dtype device
-> HList (Parameters (LSTMCell inputDim hiddenDim dtype device))
-> LSTMCell inputDim hiddenDim dtype device
forall f.
(f -> HList (Parameters f))
-> (f -> HList (Parameters f) -> f) -> Parameterized f
replaceParameters :: LSTMCell inputDim hiddenDim dtype device
-> HList (Parameters (LSTMCell inputDim hiddenDim dtype device))
-> LSTMCell inputDim hiddenDim dtype device
$creplaceParameters :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCell inputDim hiddenDim dtype device
-> HList (Parameters (LSTMCell inputDim hiddenDim dtype device))
-> LSTMCell inputDim hiddenDim dtype device
flattenParameters :: LSTMCell inputDim hiddenDim dtype device
-> HList (Parameters (LSTMCell inputDim hiddenDim dtype device))
$cflattenParameters :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCell inputDim hiddenDim dtype device
-> HList (Parameters (LSTMCell inputDim hiddenDim dtype device))
Parameterized)

instance
  ( KnownDevice device,
    KnownDType dtype,
    KnownNat inputDim,
    KnownNat hiddenDim,
    RandDTypeIsValid device dtype
  ) =>
  A.Randomizable
    (LSTMCellSpec inputDim hiddenDim dtype device)
    (LSTMCell inputDim hiddenDim dtype device)
  where
  sample :: LSTMCellSpec inputDim hiddenDim dtype device
-> IO (LSTMCell inputDim hiddenDim dtype device)
sample LSTMCellSpec inputDim hiddenDim dtype device
LSTMCellSpec =
    forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
Parameter device dtype '[4 * hiddenDim, inputDim]
-> Parameter device dtype '[4 * hiddenDim, hiddenDim]
-> Parameter device dtype '[4 * hiddenDim]
-> Parameter device dtype '[4 * hiddenDim]
-> LSTMCell inputDim hiddenDim dtype device
LSTMCell
      forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall (shape :: [Natural]) (dtype :: DType)
       (device :: (DeviceType, Natural)).
Tensor device dtype shape -> IO (Parameter device dtype shape)
makeIndependent forall (m :: Type -> Type) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (shape :: [Natural]) (dtype :: DType)
       (device :: (DeviceType, Natural)).
(TensorOptions shape dtype device,
 RandDTypeIsValid device dtype) =>
IO (Tensor device dtype shape)
randn)
      forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> (forall (shape :: [Natural]) (dtype :: DType)
       (device :: (DeviceType, Natural)).
Tensor device dtype shape -> IO (Parameter device dtype shape)
makeIndependent forall (m :: Type -> Type) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (shape :: [Natural]) (dtype :: DType)
       (device :: (DeviceType, Natural)).
(TensorOptions shape dtype device,
 RandDTypeIsValid device dtype) =>
IO (Tensor device dtype shape)
randn)
      forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> (forall (shape :: [Natural]) (dtype :: DType)
       (device :: (DeviceType, Natural)).
Tensor device dtype shape -> IO (Parameter device dtype shape)
makeIndependent forall (m :: Type -> Type) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (shape :: [Natural]) (dtype :: DType)
       (device :: (DeviceType, Natural)).
(TensorOptions shape dtype device,
 RandDTypeIsValid device dtype) =>
IO (Tensor device dtype shape)
randn)
      forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> (forall (shape :: [Natural]) (dtype :: DType)
       (device :: (DeviceType, Natural)).
Tensor device dtype shape -> IO (Parameter device dtype shape)
makeIndependent forall (m :: Type -> Type) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (shape :: [Natural]) (dtype :: DType)
       (device :: (DeviceType, Natural)).
(TensorOptions shape dtype device,
 RandDTypeIsValid device dtype) =>
IO (Tensor device dtype shape)
randn)

-- | A single recurrent step of an `LSTMCell`
lstmCellForward ::
  forall inputDim hiddenDim batchSize dtype device.
  ( KnownDType dtype,
    KnownNat inputDim,
    KnownNat hiddenDim,
    KnownNat batchSize
  ) =>
  -- | The cell
  LSTMCell inputDim hiddenDim dtype device ->
  -- | The current (Hidden, Cell) state
  ( Tensor device dtype '[batchSize, hiddenDim],
    Tensor device dtype '[batchSize, hiddenDim]
  ) ->
  -- | The input
  Tensor device dtype '[batchSize, inputDim] ->
  -- | The subsequent (Hidden, Cell) state
  ( Tensor device dtype '[batchSize, hiddenDim],
    Tensor device dtype '[batchSize, hiddenDim]
  )
lstmCellForward :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (batchSize :: Natural) (dtype :: DType)
       (device :: (DeviceType, Natural)).
(KnownDType dtype, KnownNat inputDim, KnownNat hiddenDim,
 KnownNat batchSize) =>
LSTMCell inputDim hiddenDim dtype device
-> (Tensor device dtype '[batchSize, hiddenDim],
    Tensor device dtype '[batchSize, hiddenDim])
-> Tensor device dtype '[batchSize, inputDim]
-> (Tensor device dtype '[batchSize, hiddenDim],
    Tensor device dtype '[batchSize, hiddenDim])
lstmCellForward LSTMCell {Parameter device dtype '[4 * hiddenDim, inputDim]
Parameter device dtype '[4 * hiddenDim, hiddenDim]
Parameter device dtype '[4 * hiddenDim]
lstmCell_b_hh :: Parameter device dtype '[4 * hiddenDim]
lstmCell_b_ih :: Parameter device dtype '[4 * hiddenDim]
lstmCell_w_hh :: Parameter device dtype '[4 * hiddenDim, hiddenDim]
lstmCell_w_ih :: Parameter device dtype '[4 * hiddenDim, inputDim]
lstmCell_b_hh :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCell inputDim hiddenDim dtype device
-> Parameter device dtype '[4 * hiddenDim]
lstmCell_b_ih :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCell inputDim hiddenDim dtype device
-> Parameter device dtype '[4 * hiddenDim]
lstmCell_w_hh :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCell inputDim hiddenDim dtype device
-> Parameter device dtype '[4 * hiddenDim, hiddenDim]
lstmCell_w_ih :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (dtype :: DType) (device :: (DeviceType, Natural)).
LSTMCell inputDim hiddenDim dtype device
-> Parameter device dtype '[4 * hiddenDim, inputDim]
..} =
  forall (inputSize :: Natural) (hiddenSize :: Natural)
       (batchSize :: Natural) (dtype :: DType)
       (device :: (DeviceType, Natural)).
Tensor device dtype '[4 * hiddenSize, inputSize]
-> Tensor device dtype '[4 * hiddenSize, hiddenSize]
-> Tensor device dtype '[4 * hiddenSize]
-> Tensor device dtype '[4 * hiddenSize]
-> (Tensor device dtype '[batchSize, hiddenSize],
    Tensor device dtype '[batchSize, hiddenSize])
-> Tensor device dtype '[batchSize, inputSize]
-> (Tensor device dtype '[batchSize, hiddenSize],
    Tensor device dtype '[batchSize, hiddenSize])
lstmCell
    (forall (shape :: [Natural]) (dtype :: DType)
       (device :: (DeviceType, Natural)).
Parameter device dtype shape -> Tensor device dtype shape
toDependent Parameter device dtype '[4 * hiddenDim, inputDim]
lstmCell_w_ih)
    (forall (shape :: [Natural]) (dtype :: DType)
       (device :: (DeviceType, Natural)).
Parameter device dtype shape -> Tensor device dtype shape
toDependent Parameter device dtype '[4 * hiddenDim, hiddenDim]
lstmCell_w_hh)
    (forall (shape :: [Natural]) (dtype :: DType)
       (device :: (DeviceType, Natural)).
Parameter device dtype shape -> Tensor device dtype shape
toDependent Parameter device dtype '[4 * hiddenDim]
lstmCell_b_ih)
    (forall (shape :: [Natural]) (dtype :: DType)
       (device :: (DeviceType, Natural)).
Parameter device dtype shape -> Tensor device dtype shape
toDependent Parameter device dtype '[4 * hiddenDim]
lstmCell_b_hh)

-- | foldl' for lists of tensors unsing an `LSTMCell`
lstmCellFold ::
  forall inputDim hiddenDim batchSize dtype device.
  ( KnownDType dtype,
    KnownNat inputDim,
    KnownNat hiddenDim,
    KnownNat batchSize
  ) =>
  LSTMCell inputDim hiddenDim dtype device ->
  -- | The initial (Hidden, Cell) state
  ( Tensor device dtype '[batchSize, hiddenDim],
    Tensor device dtype '[batchSize, hiddenDim]
  ) ->
  -- | The list of inputs
  [Tensor device dtype '[batchSize, inputDim]] ->
  -- | The final (Hidden, Cell) state
  ( Tensor device dtype '[batchSize, hiddenDim],
    Tensor device dtype '[batchSize, hiddenDim]
  )
lstmCellFold :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (batchSize :: Natural) (dtype :: DType)
       (device :: (DeviceType, Natural)).
(KnownDType dtype, KnownNat inputDim, KnownNat hiddenDim,
 KnownNat batchSize) =>
LSTMCell inputDim hiddenDim dtype device
-> (Tensor device dtype '[batchSize, hiddenDim],
    Tensor device dtype '[batchSize, hiddenDim])
-> [Tensor device dtype '[batchSize, inputDim]]
-> (Tensor device dtype '[batchSize, hiddenDim],
    Tensor device dtype '[batchSize, hiddenDim])
lstmCellFold LSTMCell inputDim hiddenDim dtype device
cell = forall (t :: Type -> Type) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (forall (inputDim :: Natural) (hiddenDim :: Natural)
       (batchSize :: Natural) (dtype :: DType)
       (device :: (DeviceType, Natural)).
(KnownDType dtype, KnownNat inputDim, KnownNat hiddenDim,
 KnownNat batchSize) =>
LSTMCell inputDim hiddenDim dtype device
-> (Tensor device dtype '[batchSize, hiddenDim],
    Tensor device dtype '[batchSize, hiddenDim])
-> Tensor device dtype '[batchSize, inputDim]
-> (Tensor device dtype '[batchSize, hiddenDim],
    Tensor device dtype '[batchSize, hiddenDim])
lstmCellForward LSTMCell inputDim hiddenDim dtype device
cell)

-- | scanl' for lists of tensors unsing an `LSTMCell`
lstmCellScan ::
  forall inputDim hiddenDim batchSize dtype device.
  ( KnownDType dtype,
    KnownNat inputDim,
    KnownNat hiddenDim,
    KnownNat batchSize
  ) =>
  LSTMCell inputDim hiddenDim dtype device ->
  -- | The initial (Hidden, Cell) state
  ( Tensor device dtype '[batchSize, hiddenDim],
    Tensor device dtype '[batchSize, hiddenDim]
  ) ->
  -- | The list of inputs
  [Tensor device dtype '[batchSize, inputDim]] ->
  -- | All subsequent (Hidden, Cell) states
  [ ( Tensor device dtype '[batchSize, hiddenDim],
      Tensor device dtype '[batchSize, hiddenDim]
    )
  ]
lstmCellScan :: forall (inputDim :: Natural) (hiddenDim :: Natural)
       (batchSize :: Natural) (dtype :: DType)
       (device :: (DeviceType, Natural)).
(KnownDType dtype, KnownNat inputDim, KnownNat hiddenDim,
 KnownNat batchSize) =>
LSTMCell inputDim hiddenDim dtype device
-> (Tensor device dtype '[batchSize, hiddenDim],
    Tensor device dtype '[batchSize, hiddenDim])
-> [Tensor device dtype '[batchSize, inputDim]]
-> [(Tensor device dtype '[batchSize, hiddenDim],
     Tensor device dtype '[batchSize, hiddenDim])]
lstmCellScan LSTMCell inputDim hiddenDim dtype device
cell = forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl' (forall (inputDim :: Natural) (hiddenDim :: Natural)
       (batchSize :: Natural) (dtype :: DType)
       (device :: (DeviceType, Natural)).
(KnownDType dtype, KnownNat inputDim, KnownNat hiddenDim,
 KnownNat batchSize) =>
LSTMCell inputDim hiddenDim dtype device
-> (Tensor device dtype '[batchSize, hiddenDim],
    Tensor device dtype '[batchSize, hiddenDim])
-> Tensor device dtype '[batchSize, inputDim]
-> (Tensor device dtype '[batchSize, hiddenDim],
    Tensor device dtype '[batchSize, hiddenDim])
lstmCellForward LSTMCell inputDim hiddenDim dtype device
cell)