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

locallycon2d_layer_dense_batch.py

1 # file: locallycon2d_layer_dense_batch.py
2 #===============================================================================
3 # Copyright 2014-2019 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 2D locally connected layer usage
19 # !
20 # !*****************************************************************************
21 
22 #
23 
24 
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, 2, 6, 8]
45  tensorData = HomogenTensor(inDims, TensorIface.doAllocate, 1.0)
46 
47  # Create an algorithm to compute forward 2D locally connected layer results using default method
48  locallyconnected2dLayerForward = layers.locallyconnected2d.forward.Batch()
49  locallyconnected2dLayerForward.input.setInput(layers.forward.data, tensorData)
50 
51  # Compute forward 2D locally connected layer results
52  forwardResult = locallyconnected2dLayerForward.compute()
53 
54  printTensor(forwardResult.getResult(layers.forward.value), "Forward 2D locally connected layer result (first 5 rows):", 5, 15)
55  printTensor(forwardResult.getLayerData(layers.locallyconnected2d.auxWeights), "2D locally connected layer weights (first 5 rows):", 5, 15)
56 
57  gDims = forwardResult.getResult(layers.forward.value).getDimensions()
58 
59  # Create input gradient tensor for backward 2D locally connected layer
60  tensorDataBack = HomogenTensor(gDims, TensorIface.doAllocate, 0.01)
61 
62  # Create an algorithm to compute backward 2D locally connected layer results using default method
63  locallyconnected2dLayerBackward = layers.locallyconnected2d.backward.Batch()
64  locallyconnected2dLayerBackward.input.setInput(layers.backward.inputGradient, tensorDataBack)
65  locallyconnected2dLayerBackward.input.setInputLayerData(layers.backward.inputFromForward, forwardResult.getResultLayerData(layers.forward.resultForBackward))
66 
67  # Compute backward 2D locally connected layer results
68  backwardResult = locallyconnected2dLayerBackward.compute()
69 
70  printTensor(backwardResult.getResult(layers.backward.gradient),
71  "2D locally connected layer backpropagation gradient result (first 5 rows):", 5, 15)
72  printTensor(backwardResult.getResult(layers.backward.weightDerivatives),
73  "2D locally connected layer backpropagation weightDerivative result (first 5 rows):", 5, 15)
74  printTensor(backwardResult.getResult(layers.backward.biasDerivatives),
75  "2D locally connected layer backpropagation biasDerivative result (first 5 rows):", 5, 15)

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