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

abs_layer_dense_batch.py

1 # file: abs_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 forward and backward absolute value (abs) layer usage
21 # !
22 # !
23 # !*****************************************************************************
24 
25 #
26 
27 
28 #
29 
30 import os
31 import sys
32 
33 from daal.algorithms.neural_networks import layers
34 from daal.data_management import HomogenTensor, TensorIface
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, readTensorFromCSV
40 
41 
42 # Input data set parameters
43 datasetName = os.path.join("..", "data", "batch", "layer.csv")
44 
45 if __name__ == "__main__":
46 
47  # Read datasetFileName from a file and create a tensor to store input data
48  tensorData = readTensorFromCSV(datasetName)
49 
50  # Create an algorithm to compute forward abs layer results using default method
51  absLayerForward = layers.abs.forward.Batch()
52 
53  # Set input objects for the forward abs layer
54  absLayerForward.input.setInput(layers.forward.data, tensorData)
55 
56  # Compute forward abs layer results
57  forwardResult = absLayerForward.compute()
58 
59  # Print the results of the forward abs layer
60  printTensor(forwardResult.getResult(layers.forward.value), "Forward abs layer result (first 5 rows):", 5)
61 
62  # Get the size of forward abs layer output
63  gDims = forwardResult.getResult(layers.forward.value).getDimensions()
64  tensorDataBack = HomogenTensor(gDims, TensorIface.doAllocate, 0.01)
65 
66  # Create an algorithm to compute backward abs layer results using default method
67  absLayerBackward = layers.abs.backward.Batch()
68 
69  # Set input objects for the backward abs layer
70  absLayerBackward.input.setInput(layers.backward.inputGradient, tensorDataBack)
71  absLayerBackward.input.setInputLayerData(layers.backward.inputFromForward, forwardResult.getResultLayerData(layers.forward.resultForBackward))
72 
73  # Compute backward abs layer results
74  backwardResult = absLayerBackward.compute()
75 
76  # Print the results of the backward abs layer
77  printTensor(backwardResult.getResult(layers.backward.gradient), "Backward abs layer result (first 5 rows):", 5)

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