mirror of
https://github.com/tensorflow/haskell.git
synced 2024-06-02 11:03:34 +02:00
Add gradient for slice function
This commit is contained in:
parent
9c165e8a3e
commit
b8d1bf89f8
|
@ -710,6 +710,27 @@ opGrad "Pad" _ [toT -> x, toT -> padPattern] [dz] =
|
|||
gradientSliceBegin = CoreOps.reshape padPatternSliced rankx
|
||||
gradientSliceSize = shape (x :: Tensor Build Float)
|
||||
|
||||
-- Gradient for Slice
|
||||
-- Create an Nx2 padding where N ist the rank of (grad of) Slice and the first
|
||||
-- column represents how many zeros are to be prepended for each dimension, and the second
|
||||
-- column indicates how many zeros are appended.
|
||||
-- The number of zeros to prepend is the shape of the beginvec.
|
||||
-- The number of zeros to append is the shape of the inputvec
|
||||
-- elementwise-subtracted by both the beginvec and sizevec.
|
||||
-- Some more reshaping is needed to assemble this tensor with the
|
||||
-- right dimensions.
|
||||
opGrad "Slice" _ [toT -> inputvec, toT -> beginvec, _] [dz] =
|
||||
[Just $ CoreOps.pad dz paddings, Nothing, Nothing]
|
||||
where
|
||||
v1 = vector [1 :: Int32]
|
||||
input_rank' = CoreOps.rank (inputvec :: Tensor Build Float)
|
||||
-- For some reason input_rank' has an empty shape
|
||||
input_rank = CoreOps.reshape input_rank' v1
|
||||
pad_shape = CoreOps.concat 0 [input_rank, v1]
|
||||
beforepad = CoreOps.reshape beginvec pad_shape
|
||||
afterpad = CoreOps.reshape (shape inputvec - shape dz - beginvec) pad_shape
|
||||
paddings = CoreOps.concat 1 [beforepad, afterpad]
|
||||
|
||||
-- TODO: This could be either Int32 or Int64.
|
||||
opGrad "BatchToSpaceND" _ [_, toT @Int32 -> blockShape, toT @Int32 -> crops] [dz] =
|
||||
[Just $ CoreOps.spaceToBatchND dz blockShape crops, Nothing, Nothing]
|
||||
|
@ -858,6 +879,7 @@ numOutputs o =
|
|||
"Reshape" -> 1
|
||||
"Select" -> 1
|
||||
"Size" -> 1
|
||||
"Slice" -> 1
|
||||
"SoftmaxCrossEntropyWithLogits" -> 2
|
||||
"SpaceToBatchND" -> 1
|
||||
"SparseSegmentSum" -> 1
|
||||
|
|
|
@ -32,9 +32,9 @@ import Control.Monad(forM_, replicateM, zipWithM)
|
|||
import Control.Monad.IO.Class (liftIO)
|
||||
|
||||
import qualified TensorFlow.Core as TF
|
||||
import qualified TensorFlow.GenOps.Core as TF (conv2DBackpropInput', max, maximum, tile, pad, batchToSpaceND, spaceToBatchND, squeeze)
|
||||
import qualified TensorFlow.GenOps.Core as TF (conv2DBackpropInput', max, maximum, tile, pad, batchToSpaceND, spaceToBatchND, squeeze, slice, shape)
|
||||
import qualified TensorFlow.Gradient as TF
|
||||
import qualified TensorFlow.Ops as TF hiding (zeroInitializedVariable)
|
||||
import qualified TensorFlow.Ops as TF hiding (zeroInitializedVariable, shape)
|
||||
import qualified TensorFlow.Output as TF
|
||||
import qualified TensorFlow.Types as TF
|
||||
import qualified TensorFlow.Variable as TF
|
||||
|
@ -324,6 +324,18 @@ testPad =
|
|||
V.fromList [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] @=? dx
|
||||
V.fromList [2, 2, 3] @=? s
|
||||
|
||||
testSlice :: Test
|
||||
testSlice =
|
||||
testCase "testSlice" $ do
|
||||
([dx], [s]) <-
|
||||
TF.runSession $ do
|
||||
(x :: TF.Tensor TF.Value Float) <- TF.render $ TF.zeros $ TF.Shape [2, 3, 4 :: Int64]
|
||||
(z :: TF.Tensor TF.Value Float) <- TF.render $ TF.zeros $ TF.Shape [1, 2, 2 :: Int64]
|
||||
let y = TF.slice x (TF.constant (TF.Shape [3]) [1, 1, 1 :: Int32]) (TF.shape z)
|
||||
calculateGradWithShape y x
|
||||
V.fromList [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0] @=? dx
|
||||
V.fromList [2, 3, 4] @=? s
|
||||
|
||||
testBatchToSpaceND :: Test
|
||||
testBatchToSpaceND =
|
||||
testCase "testBatchToSpaceND" $ do
|
||||
|
@ -517,6 +529,7 @@ main = defaultMain
|
|||
, testExpandDims
|
||||
, testReshape
|
||||
, testPad
|
||||
, testSlice
|
||||
, testBatchToSpaceND
|
||||
, testSpaceToBatchND
|
||||
, testSqueeze
|
||||
|
|
Loading…
Reference in New Issue
Block a user