@@ -1,14 +1,6 @@
11module Test.Grenade.Layers.Internal.Reference where
22
3−
4−import Control.Monad.ST ( ST )
5−
6−import Data.Foldable ( forM_ )
7−import Data.Function ( on )
8−import Data.List ( maximumBy )
9−
103import Numeric.LinearAlgebra
11−import qualified Numeric.LinearAlgebra.Devel as U
124
135im2col :: Int -> Int -> Int -> Int -> Matrix Double -> Matrix Double
146im2col nrows ncols srows scols m =
@@ -47,7 +39,6 @@ col2imfit starts krows kcols drows dcols m =
4739 accums = concatMap (\(conv',(stx',sty')) -> fmap (\((ix,iy), val) -> ((ix + stx', iy + sty'), val)) conv') pairs
4840 in accum (konst 0 (drows, dcols)) (+) accums
4941
50−
5142poolForward :: Int -> Int -> Int -> Int -> Int -> Int -> Matrix Double -> Matrix Double
5243poolForward nrows ncols srows scols outputRows outputCols m =
5344 let starts = fittingStarts (rows m) nrows srows (cols m) ncols scols
@@ -60,7 +51,7 @@ poolForwardList nrows ncols srows scols inRows inCols outputRows outputCols ms =
6051
6152poolForwardFit :: [(Int,Int)] -> Int -> Int -> Int -> Int -> Matrix Double -> Matrix Double
6253poolForwardFit starts nrows ncols _ outputCols m =
63− let els = fmap (\start -> unsafeMaxElementSubmatrix start (nrows, ncols) m) starts
54+ let els = fmap (\start -> maxElement $ subMatrix start (nrows, ncols) m) starts
6455 in matrix outputCols els
6556
6657poolBackward :: Int -> Int -> Int -> Int -> Matrix Double -> Matrix Double -> Matrix Double
@@ -76,34 +67,14 @@ poolBackwardList krows kcols srows scols inRows inCols inputMatrices =
7667 in uncurry (poolBackwardFit starts krows kcols) <$> inputMatrices
7768
7869poolBackwardFit :: [(Int,Int)] -> Int -> Int -> Matrix Double -> Matrix Double -> Matrix Double
79−poolBackwardFit starts krows kcols inputMatrix gradientMatrix = U.runSTMatrix $ do
70+poolBackwardFit starts krows kcols inputMatrix gradientMatrix =
8071 let inRows = rows inputMatrix
8172 inCols = cols inputMatrix
82− gradCol = cols gradientMatrix
83− extent = (krows, kcols)
84−
85− retM <- U.newMatrix 0 inRows inCols
86−
87− forM_ (zip [0..] starts) $ \(ix, start) -> do
88− let loc = unsafeMaxIndexSubMatrix start extent inputMatrix
89− uncurry (unsafeModifyMatrix retM) loc ((+) $ uncurry (U.atM' gradientMatrix) $ divMod ix gradCol)
90−
91− return retM
92−
93−unsafeMaxElementSubmatrix :: (Int,Int) -> (Int,Int) -> Matrix Double -> Double
94−unsafeMaxElementSubmatrix starts extent m = uncurry (U.atM' m) $ unsafeMaxIndexSubMatrix starts extent m
95−
96−unsafeMaxIndexSubMatrix :: (Int,Int) -> (Int,Int) -> Matrix Double -> (Int, Int)
97−unsafeMaxIndexSubMatrix (startRow, startCol) (extentRow, extentCold) m =
98− let mrows = [startRow .. startRow + extentRow - 1]
99− mcols = [startCol .. startCol + extentCold - 1]
100− pairs = concatMap ( \r -> fmap (\c -> (r , c)) mcols ) mrows
101− in maximumBy (compare `on` uncurry (U.atM' m)) pairs
102−
103−
104−unsafeModifyMatrix :: U.STMatrix s Double -> Int -> Int -> (Double -> Double) -> ST s ()
105−unsafeModifyMatrix x r c f = U.unsafeReadMatrix x r c >>= U.unsafeWriteMatrix x r c . f
106−
73+ inds = fmap (\start -> maxIndex $ subMatrix start (krows, kcols) inputMatrix) starts
74+ grads = toList $ flatten gradientMatrix
75+ grads' = zip3 starts grads inds
76+ accums = fmap (\((stx',sty'),grad,(inx, iny)) -> ((stx' + inx, sty' + iny), grad)) grads'
77+ in accum (konst 0 (inRows, inCols)) (+) accums
10778
10879-- | These functions are not even remotely safe, but it's only called from the statically typed
10980-- commands, so we should be good ?!?!?