on master help sign in
Optimise poolForwardFit
commit267ebc20804478dc8e07b33dd60f3e3c3f1397db authorHuw Campbell <huw.campbell@ambiata.com> dateFri 9 Dec 2016, 20:51 (10 years ago) parentb914b91
1 file changed+3−21
src/Grenade/Layers/Internal/Pooling.hs+3−21
src/Grenade/Layers/Internal/Pooling.hs+3−21
@@ -27,7 +27,7 @@ poolForwardList nrows ncols srows scols inRows inCols outputRows outputCols ms =
2727
2828poolForwardFit :: [(Int,Int)] -> Int -> Int -> Int -> Int -> Matrix Double -> Matrix Double
2929poolForwardFit starts nrows ncols _ outputCols m =
30− let els = fmap (\start -> unsafeMaxElement $ subMatrix start (nrows, ncols) m) starts
30+ let els = fmap (\start -> unsafeMaxElementSubmatrix start (nrows, ncols) m) starts
3131 in LA.matrix outputCols els
3232
3333poolBackward :: Int -> Int -> Int -> Int -> Matrix Double -> Matrix Double -> Matrix Double
@@ -42,16 +42,6 @@ poolBackwardList krows kcols srows scols inRows inCols inputMatrices =
4242 let starts = fittingStarts inRows krows srows inCols kcols scols
4343 in uncurry (poolBackwardFit starts krows kcols) <$> inputMatrices
4444
45−-- poolBackwardFit :: [(Int,Int)] -> Int -> Int -> Matrix Double -> Matrix Double -> Matrix Double
46−-- poolBackwardFit starts krows kcols inputMatrix gradientMatrix =
47−-- let inRows = rows inputMatrix
48−-- inCols = cols inputMatrix
49−-- inds = fmap (\start -> unsafeMaxIndex $ subMatrix start (krows, kcols) inputMatrix) starts
50−-- grads = toList $ flatten gradientMatrix
51−-- grads' = zip3 starts grads inds
52−-- accums = fmap (\((stx',sty'),grad,(inx, iny)) -> ((stx' + inx, sty' + iny), grad)) grads'
53−-- in accum (LA.konst 0 (inRows, inCols)) (+) accums
54−
5545poolBackwardFit :: [(Int,Int)] -> Int -> Int -> Matrix Double -> Matrix Double -> Matrix Double
5646poolBackwardFit starts krows kcols inputMatrix gradientMatrix = U.runSTMatrix $ do
5747 let inRows = rows inputMatrix
@@ -67,16 +57,8 @@ poolBackwardFit starts krows kcols inputMatrix gradientMatrix = U.runSTMatrix $
6757
6858 return retM
6959
70−unsafeMaxElement :: Matrix Double -> Double
71−unsafeMaxElement m = uncurry (U.atM' m) $ unsafeMaxIndex m
72−
73−unsafeMaxIndex :: Matrix Double -> (Int, Int)
74−unsafeMaxIndex m =
75− let mrows = [0 .. rows m - 1]
76− mcols = [0 .. cols m - 1]
77− pairs = concatMap ( \r -> fmap (\c -> (r , c)) mcols ) mrows
78− in maximumBy (compare `on` uncurry (U.atM' m)) pairs
79−
60+unsafeMaxElementSubmatrix :: (Int,Int) -> (Int,Int) -> Matrix Double -> Double
61+unsafeMaxElementSubmatrix starts extent m = uncurry (U.atM' m) $ unsafeMaxIndexSubMatrix starts extent m
8062
8163unsafeMaxIndexSubMatrix :: (Int,Int) -> (Int,Int) -> Matrix Double -> (Int, Int)
8264unsafeMaxIndexSubMatrix (startRow, startCol) (extentRow, extentCold) m =