@@ -1,14 +1,6 @@
1module Test.Grenade.Layers.Internal.Reference where
1module Test.Grenade.Layers.Internal.Reference where
4−import Control.Monad.ST ( ST )
6−import Data.Foldable ( forM_ )
7−import Data.Function ( on )
8−import Data.List ( maximumBy )
10import Numeric.LinearAlgebra
3import Numeric.LinearAlgebra
11−import qualified Numeric.LinearAlgebra.Devel as U
13im2col :: Int -> Int -> Int -> Int -> Matrix Double -> Matrix Double
5im2col :: Int -> Int -> Int -> Int -> Matrix Double -> Matrix Double
14im2col nrows ncols srows scols m =
6im2col nrows ncols srows scols m =
@@ -47,7 +39,6 @@ col2imfit starts krows kcols drows dcols m =
47 accums = concatMap (\(conv',(stx',sty')) -> fmap (\((ix,iy), val) -> ((ix + stx', iy + sty'), val)) conv') pairs
39 accums = concatMap (\(conv',(stx',sty')) -> fmap (\((ix,iy), val) -> ((ix + stx', iy + sty'), val)) conv') pairs
48 in accum (konst 0 (drows, dcols)) (+) accums
40 in accum (konst 0 (drows, dcols)) (+) accums
51poolForward :: Int -> Int -> Int -> Int -> Int -> Int -> Matrix Double -> Matrix Double
42poolForward :: Int -> Int -> Int -> Int -> Int -> Int -> Matrix Double -> Matrix Double
52poolForward nrows ncols srows scols outputRows outputCols m =
43poolForward nrows ncols srows scols outputRows outputCols m =
53 let starts = fittingStarts (rows m) nrows srows (cols m) ncols scols
44 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 =
61poolForwardFit :: [(Int,Int)] -> Int -> Int -> Int -> Int -> Matrix Double -> Matrix Double
52poolForwardFit :: [(Int,Int)] -> Int -> Int -> Int -> Int -> Matrix Double -> Matrix Double
62poolForwardFit starts nrows ncols _ outputCols m =
53poolForwardFit 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
64 in matrix outputCols els
55 in matrix outputCols els
66poolBackward :: Int -> Int -> Int -> Int -> Matrix Double -> Matrix Double -> Matrix Double
57poolBackward :: Int -> Int -> Int -> Int -> Matrix Double -> Matrix Double -> Matrix Double
@@ -76,34 +67,14 @@ poolBackwardList krows kcols srows scols inRows inCols inputMatrices =
76 in uncurry (poolBackwardFit starts krows kcols) <$> inputMatrices
67 in uncurry (poolBackwardFit starts krows kcols) <$> inputMatrices
78poolBackwardFit :: [(Int,Int)] -> Int -> Int -> Matrix Double -> Matrix Double -> Matrix Double
69poolBackwardFit :: [(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 =
80 let inRows = rows inputMatrix
71 let inRows = rows inputMatrix
81 inCols = cols inputMatrix
72 inCols = cols inputMatrix
82− gradCol = cols gradientMatrix
73+ inds = fmap (\start -> maxIndex $ subMatrix start (krows, kcols) inputMatrix) starts
83− extent = (krows, kcols)
74+ grads = toList $ flatten gradientMatrix
84−
75+ grads' = zip3 starts grads inds
85− retM <- U.newMatrix 0 inRows inCols
76+ accums = fmap (\((stx',sty'),grad,(inx, iny)) -> ((stx' + inx, sty' + iny), grad)) grads'
86−
77+ in accum (konst 0 (inRows, inCols)) (+) accums
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)
93−unsafeMaxElementSubmatrix :: (Int,Int) -> (Int,Int) -> Matrix Double -> Double
94−unsafeMaxElementSubmatrix starts extent m = uncurry (U.atM' m) $ unsafeMaxIndexSubMatrix starts extent m
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
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
108-- | These functions are not even remotely safe, but it's only called from the statically typed
79-- | These functions are not even remotely safe, but it's only called from the statically typed
109-- commands, so we should be good ?!?!?
80-- commands, so we should be good ?!?!?