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

max_pool1d_layer_dense_batch.py

1 # file: max_pool1d_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 one-dimensional maximum pooling layers usage
21 # !
22 # !*****************************************************************************
23 
24 #
25 
26 
27 #
28 
29 import os
30 import sys
31 
32 from daal.algorithms.neural_networks import layers
33 
34 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
35 if utils_folder not in sys.path:
36  sys.path.insert(0, utils_folder)
37 from utils import printTensor, readTensorFromCSV
38 
39 # Input data set name
40 datasetFileName = os.path.join("..", "data", "batch", "layer.csv")
41 
42 if __name__ == "__main__":
43 
44  # Read datasetFileName from a file and create a tensor to store input data
45  data = readTensorFromCSV(datasetFileName)
46  nDim = data.getNumberOfDimensions()
47 
48  printTensor(data, "Forward one-dimensional maximum pooling layer input (first 10 rows):", 10)
49 
50  # Create an algorithm to compute forward one-dimensional pooling layer results using maximum method
51  forwardLayer = layers.maximum_pooling1d.forward.Batch(nDim)
52  forwardLayer.input.setInput(layers.forward.data, data)
53 
54  # Compute forward one-dimensional maximum pooling layer results
55  forwardResult = forwardLayer.compute()
56 
57  # Print the results of the forward one-dimensional maximum pooling layer
58  printTensor(forwardResult.getResult(layers.forward.value), "Forward one-dimensional maximum pooling layer result (first 5 rows):", 5)
59  printTensor(forwardResult.getLayerData(layers.maximum_pooling1d.auxSelectedIndices),
60  "Forward one-dimensional maximum pooling layer selected indices (first 5 rows):", 5)
61 
62  # Create an algorithm to compute backward one-dimensional maximum pooling layer results using default method
63  backwardLayer = layers.maximum_pooling1d.backward.Batch(nDim)
64 
65  # Set input objects for the backward one-dimensional maximum pooling layer
66  backwardLayer.input.setInput(layers.backward.inputGradient, forwardResult.getResult(layers.forward.value))
67  backwardLayer.input.setInputLayerData(layers.backward.inputFromForward, forwardResult.getResultLayerData(layers.forward.resultForBackward))
68 
69  # Compute backward one-dimensional maximum pooling layer results
70  backwardResult = backwardLayer.compute()
71 
72  # Print the results of the backward one-dimensional maximum pooling layer
73  printTensor(backwardResult.getResult(layers.backward.gradient),
74  "Backward one-dimensional maximum pooling layer result (first 10 rows):", 10)

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