@@ -32,7 +32,7 @@ import Control.Monad(forM_, replicateM, zipWithM)
32
32
import Control.Monad.IO.Class (liftIO )
33
33
34
34
import qualified TensorFlow.Core as TF
35
- import qualified TensorFlow.GenOps.Core as TF (conv2DBackpropInput' , max , maximum , tile )
35
+ import qualified TensorFlow.GenOps.Core as TF (conv2DBackpropInput' , max , maximum , tile , pad , batchToSpaceND , spaceToBatchND , squeeze )
36
36
import qualified TensorFlow.Gradient as TF
37
37
import qualified TensorFlow.Ops as TF hiding (zeroInitializedVariable )
38
38
import qualified TensorFlow.Output as TF
@@ -313,6 +313,54 @@ testReshape =
313
313
V. fromList [1 , 1 , 1 , 1 ] @=? dx
314
314
V. fromList [2 , 2 ] @=? s
315
315
316
+ testPad :: Test
317
+ testPad =
318
+ testCase " testPad" $ do
319
+ ([dx], [s]) <-
320
+ TF. runSession $ do
321
+ (x :: TF. Tensor TF. Value Float ) <- TF. render $ TF. zeros $ TF. Shape [2 , 2 , 3 :: Int64 ]
322
+ let y = TF. pad x $ TF. constant (TF. Shape [3 , 2 ]) [1 , 4 , 1 , 1 , 2 , 3 :: Int32 ]
323
+ calculateGradWithShape y x
324
+ V. fromList [1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] @=? dx
325
+ V. fromList [2 , 2 , 3 ] @=? s
326
+
327
+ testBatchToSpaceND :: Test
328
+ testBatchToSpaceND =
329
+ testCase " testBatchToSpaceND" $ do
330
+ ([dx], [s]) <-
331
+ TF. runSession $ do
332
+ (x :: TF. Tensor TF. Value Float ) <- TF. render $ TF. constant (TF. Shape [4 , 1 , 1 , 1 :: Int64 ]) [1 , 2 , 3 , 4 ]
333
+ shape <- TF. render $ TF. vector [2 , 2 :: Int32 ]
334
+ crops <- TF. render $ TF. constant (TF. Shape [2 , 2 ]) [0 , 0 , 0 , 0 :: Int32 ]
335
+ let y = TF. batchToSpaceND x shape crops
336
+ calculateGradWithShape y x
337
+ V. fromList [1 , 1 , 1 , 1 ] @=? dx
338
+ V. fromList [4 , 1 , 1 , 1 ] @=? s
339
+
340
+ testSpaceToBatchND :: Test
341
+ testSpaceToBatchND =
342
+ testCase " testSpaceToBatchND" $ do
343
+ ([dx], [s]) <-
344
+ TF. runSession $ do
345
+ (x :: TF. Tensor TF. Value Float ) <- TF. render $ TF. constant (TF. Shape [1 , 2 , 2 , 1 :: Int64 ]) [1 , 2 , 3 , 4 ]
346
+ shape <- TF. render $ TF. vector [2 , 2 :: Int32 ]
347
+ paddings <- TF. render $ TF. constant (TF. Shape [2 , 2 ]) [0 , 0 , 0 , 0 :: Int32 ]
348
+ let y = TF. spaceToBatchND x shape paddings
349
+ calculateGradWithShape y x
350
+ V. fromList [1 , 1 , 1 , 1 ] @=? dx
351
+ V. fromList [1 , 2 , 2 , 1 ] @=? s
352
+
353
+ testSqueeze :: Test
354
+ testSqueeze =
355
+ testCase " testSqueeze" $ do
356
+ ([dx], [s]) <-
357
+ TF. runSession $ do
358
+ (x :: TF. Tensor TF. Value Float ) <- TF. render $ TF. zeros $ TF. Shape [1 , 2 , 3 :: Int64 ]
359
+ let y = TF. squeeze x
360
+ calculateGradWithShape y x
361
+ V. fromList [1 , 1 , 1 , 1 , 1 , 1 ] @=? dx
362
+ V. fromList [1 , 2 , 3 ] @=? s
363
+
316
364
calculateGradWithShape :: TF. Tensor TF. Build Float -> TF. Tensor TF. Value Float -> SessionT IO ([V. Vector Float ], [V. Vector Int32 ])
317
365
calculateGradWithShape y x = do
318
366
gs <- TF. gradients y [x]
@@ -468,6 +516,10 @@ main = defaultMain
468
516
, testTanhGrad
469
517
, testExpandDims
470
518
, testReshape
519
+ , testPad
520
+ , testBatchToSpaceND
521
+ , testSpaceToBatchND
522
+ , testSqueeze
471
523
, testFillGrad
472
524
, testTileGrad
473
525
, testTile2DGrad
@@ -478,4 +530,4 @@ main = defaultMain
478
530
, matMulTransposeGradient (True , False )
479
531
, matMulTransposeGradient (True , True )
480
532
, testConv2DBackpropInputGrad
481
- ]
533
+ ]
0 commit comments