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

lcn_layer_dense_batch.py

1 # file: lcn_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 forward and backward local contrast normalization layer usage
19 # !
20 # !*****************************************************************************
21 
22 #
23 ## <a name="DAAL-EXAMPLE-PY-LCN_LAYER_BATCH"></a>
24 ## \example lcn_layer_dense_batch.py
25 #
26 
27 import os
28 import sys
29 
30 from daal.algorithms.neural_networks import layers
31 from daal.data_management import HomogenTensor, TensorIface
32 
33 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
34 if utils_folder not in sys.path:
35  sys.path.insert(0, utils_folder)
36 from utils import printTensor
37 
38 # Input data set name
39 datasetFileName = os.path.join("..", "data", "batch", "layer.csv")
40 
41 if __name__ == "__main__":
42 
43  # Create collection of dimension sizes of the input data tensor
44  inDims = [2, 1, 3, 4]
45  tensorData = HomogenTensor(inDims, TensorIface.doAllocate, 1.0)
46 
47  # Create an algorithm to compute forward two-dimensional convolution layer results using default method
48  lcnLayerForward = layers.lcn.forward.Batch()
49  lcnLayerForward.input.setInput(layers.forward.data, tensorData)
50 
51  # Compute forward two-dimensional convolution layer results
52  forwardResult = lcnLayerForward.compute()
53 
54  printTensor(forwardResult.getResult(layers.forward.value), "Forward local contrast normalization layer result:")
55  printTensor(forwardResult.getLayerData(layers.lcn.auxCenteredData), "Centered data tensor:")
56  printTensor(forwardResult.getLayerData(layers.lcn.auxSigma), "Sigma tensor:")
57  printTensor(forwardResult.getLayerData(layers.lcn.auxC), "C tensor:")
58  printTensor(forwardResult.getLayerData(layers.lcn.auxInvMax), "Inverted max(sigma, C):")
59 
60  # Create input gradient tensor for backward two-dimensional convolution layer
61  tensorDataBack = HomogenTensor(inDims, TensorIface.doAllocate, 0.01)
62 
63  # Create an algorithm to compute backward two-dimensional convolution layer results using default method
64  lcnLayerBackward = layers.lcn.backward.Batch()
65  lcnLayerBackward.input.setInput(layers.backward.inputGradient, tensorDataBack)
66  lcnLayerBackward.input.setInputLayerData(layers.backward.inputFromForward, forwardResult.getResultLayerData(layers.forward.resultForBackward))
67 
68  # Compute backward two-dimensional convolution layer results
69  backwardResult = lcnLayerBackward.compute()
70 
71  printTensor(backwardResult.getResult(layers.backward.gradient),
72  "Local contrast normalization layer backpropagation gradient result:")

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