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

max_pool3d_layer_dense_batch.py

1 # file: max_pool3d_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 three-dimensional maximum 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 printTensor3d
41 
42 nDim = 3
43 dims = [3, 2, 4]
44 dataArray = np.array([[[1, 2, 3, 4],
45  [5, 6, 7, 8]],
46  [[9, 10, 11, 12],
47  [13, 14, 15, 16]],
48  [[17, 18, 19, 20],
49  [21, 22, 23, 24]]],
50  dtype=np.float64)
51 
52 if __name__ == "__main__":
53 
54  dataTensor = HomogenTensor(dataArray)
55 
56  printTensor3d(dataTensor, "Forward maximum pooling layer input:")
57 
58  # Create an algorithm to compute forward pooling layer results using maximum method
59  forwardLayer = layers.maximum_pooling3d.forward.Batch(nDim)
60  forwardLayer.input.setInput(layers.forward.data, dataTensor)
61 
62  # Compute forward pooling layer results
63  forwardResult = forwardLayer.compute()
64 
65  printTensor3d(forwardResult.getResult(layers.forward.value), "Forward maximum pooling layer result:")
66  printTensor3d(forwardResult.getLayerData(layers.maximum_pooling3d.auxSelectedIndices),
67  "Forward maximum pooling layer selected indices:")
68 
69  # Create an algorithm to compute backward pooling layer results using maximum method
70  backwardLayer = layers.maximum_pooling3d.backward.Batch(nDim)
71  backwardLayer.input.setInput(layers.backward.inputGradient, forwardResult.getResult(layers.forward.value))
72  backwardLayer.input.setInputLayerData(layers.backward.inputFromForward, forwardResult.getResultLayerData(layers.forward.resultForBackward))
73 
74  # Compute backward pooling layer results
75  backwardResult = backwardLayer.compute()
76 
77  # Print the computed backward pooling layer results
78  printTensor3d(backwardResult.getResult(layers.backward.gradient), "Backward maximum pooling layer result:")

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