Python* API Reference for Intel® Data Analytics Acceleration Library 2020 Update 1

spat_ave_pool2d_layer_dense_batch.py

1 # file: spat_ave_pool2d_layer_dense_batch.py
2 #===============================================================================
3 # Copyright 2014-2020 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #===============================================================================
17 
18 #
19 # ! Content:
20 # ! Python example of neural network forward and backward two-dimensional spatial pyramid average pooling layers usage
21 # !
22 # !*****************************************************************************
23 
24 #
25 
26 
27 #
28 
29 import os
30 import sys
31 
32 import numpy as np
33 
34 from daal.algorithms.neural_networks import layers
35 from daal.data_management import HomogenTensor
36 
37 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
38 if utils_folder not in sys.path:
39  sys.path.insert(0, utils_folder)
40 from utils import printTensor, printNumericTable
41 
42 nDim = 4
43 dims = [2, 3, 2, 4]
44 dataArray = np.array([[[[2, 4, 6, 8],
45  [10, 12, 14, 16]],
46  [[18, 20, 22, 24],
47  [26, 28, 30, 32]],
48  [[34, 36, 38, 40],
49  [42, 44, 46, 48]]],
50  [[[-2, -4, -6, -8],
51  [-10, -12, -14, -16]],
52  [[-18, -20, -22, -24],
53  [-26, -28, -30, -32]],
54  [[-34, -36, -38, -40],
55  [-42, -44, -46, -48]]]],
56  dtype=np.float64)
57 
58 if __name__ == "__main__":
59  data = HomogenTensor(dataArray)
60 
61  # Read datasetFileName from a file and create a tensor to store input data
62  printTensor(data, "Forward two-dimensional spatial pyramid average pooling layer input (first 10 rows):", 10)
63 
64  # Create an algorithm to compute forward two-dimensional maximum pooling layer results using default method
65  forwardLayer = layers.spatial_average_pooling2d.forward.Batch(2, nDim)
66  forwardLayer.input.setInput(layers.forward.data, data)
67 
68  # Compute forward two-dimensional spatial pyramid average pooling layer results and return them
69  # Result class from layers.spatial_average_pooling2d.forward
70  forwardResult = forwardLayer.compute()
71 
72  printTensor(forwardResult.getResult(layers.forward.value),
73  "Forward two-dimensional spatial pyramid average pooling layer result (first 5 rows):",
74  5)
75  printNumericTable(forwardResult.getLayerData(layers.spatial_average_pooling2d.auxInputDimensions),
76  "Forward two-dimensional spatial pyramid average pooling layer input dimensions:")
77 
78  # Create an algorithm to compute backward two-dimensional spatial pyramid average pooling layer results using default method
79  backwardLayer = layers.spatial_average_pooling2d.backward.Batch(2, nDim)
80  backwardLayer.input.setInput(layers.backward.inputGradient, forwardResult.getResult(layers.forward.value))
81  backwardLayer.input.setInputLayerData(layers.backward.inputFromForward, forwardResult.getResultLayerData(layers.forward.resultForBackward))
82 
83  # Compute backward two-dimensional spatial pyramid average pooling layer results
84  # Result class from layers.spatial_average_pooling2d.backward
85  backwardResult = backwardLayer.compute()
86 
87  printTensor(backwardResult.getResult(layers.backward.gradient),
88  "Backward two-dimensional spatial pyramid average pooling layer result (first 10 rows):",
89  10)

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