Python* API Reference for Intel® Data Analytics Acceleration Library 2019

spat_max_pool2d_layer_dense_batch.py

1 # file: spat_max_pool2d_layer_dense_batch.py
2 #===============================================================================
3 # Copyright 2014-2018 Intel Corporation.
4 #
5 # This software and the related documents are Intel copyrighted materials, and
6 # your use of them is governed by the express license under which they were
7 # provided to you (License). Unless the License provides otherwise, you may not
8 # use, modify, copy, publish, distribute, disclose or transmit this software or
9 # the related documents without Intel's prior written permission.
10 #
11 # This software and the related documents are provided as is, with no express
12 # or implied warranties, other than those that are expressly stated in the
13 # License.
14 #===============================================================================
15 
16 #
17 # ! Content:
18 # ! Python example of neural network forward and backward two-dimensional spatial pyramid maximum pooling layers usage
19 # !
20 # !*****************************************************************************
21 
22 #
23 ## <a name="DAAL-EXAMPLE-PY-SPAT_MAX_POOL2D_LAYER_DENSE_BATCH"></a>
24 ## \example spat_max_pool2d_layer_dense_batch.py
25 #
26 
27 import os
28 import sys
29 
30 import numpy as np
31 
32 from daal.algorithms.neural_networks import layers
33 from daal.algorithms.neural_networks.layers import spatial_maximum_pooling2d
34 from daal.data_management import HomogenTensor
35 
36 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
37 if utils_folder not in sys.path:
38  sys.path.insert(0, utils_folder)
39 from utils import printTensor
40 
41 nDim = 4
42 dims = [2, 3, 2, 4]
43 dataArray = np.array([[[[1, 2, 3, 4],
44  [5, 6, 7, 8]],
45  [[9, 10, 11, 12],
46  [13, 14, 15, 16]],
47  [[17, 18, 19, 20],
48  [21, 22, 23, 24]]],
49  [[[-1, -2, -3, -4],
50  [-5, -6, -7, -8]],
51  [[-9, -10, -11, -12],
52  [-13, -14, -15, -16]],
53  [[-17, -18, -19, -20],
54  [-21, -22, -23, -24]]]],
55  dtype=np.float64)
56 
57 if __name__ == "__main__":
58  data = HomogenTensor(dataArray)
59 
60 
61  printTensor(data, "Forward two-dimensional spatial pyramid maximum pooling layer input (first 10 rows):", 10)
62 
63  # Create an algorithm to compute forward two-dimensional spatial pyramid maximum pooling layer results using default method
64  forwardLayer = spatial_maximum_pooling2d.forward.Batch(2, nDim)
65  forwardLayer.input.setInput(layers.forward.data, data)
66 
67  # Compute forward two-dimensional spatial pyramid maximum pooling layer results
68  forwardLayer.compute()
69 
70  # Get the computed forward two-dimensional spatial pyramid maximum pooling layer results
71  forwardResult = forwardLayer.getResult()
72 
73  printTensor(forwardResult.getResult(layers.forward.value), "Forward two-dimensional spatial pyramid maximum pooling layer result (first 5 rows):", 5)
74  printTensor(forwardResult.getLayerData(layers.spatial_maximum_pooling2d.auxSelectedIndices),
75  "Forward two-dimensional spatial pyramid maximum pooling layer selected indices (first 10 rows):", 10)
76 
77  # Create an algorithm to compute backward two-dimensional spatial pyramid maximum pooling layer results using default method
78  backwardLayer = layers.spatial_maximum_pooling2d.backward.Batch(2, nDim)
79  backwardLayer.input.setInput(layers.backward.inputGradient, forwardResult.getResult(layers.forward.value))
80  backwardLayer.input.setInputLayerData(layers.backward.inputFromForward, forwardResult.getResultLayerData(layers.forward.resultForBackward))
81 
82  # Compute backward two-dimensional spatial pyramid maximum pooling layer results
83  backwardLayer.compute()
84 
85  # Get the computed backward two-dimensional spatial pyramid maximum pooling layer results
86  backwardResult = backwardLayer.getResult()
87 
88  printTensor(backwardResult.getResult(layers.backward.gradient),
89  "Backward two-dimensional spatial pyramid maximum pooling layer result (first 10 rows):", 10)

For more complete information about compiler optimizations, see our Optimization Notice.