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

locallycon2d_layer_dense_batch.py

1 # file: locallycon2d_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 2D locally connected layer 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 from daal.data_management import HomogenTensor, TensorIface
34 
35 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
36 if utils_folder not in sys.path:
37  sys.path.insert(0, utils_folder)
38 from utils import printTensor
39 
40 # Input data set name
41 datasetFileName = os.path.join("..", "data", "batch", "layer.csv")
42 
43 if __name__ == "__main__":
44 
45  # Create collection of dimension sizes of the input data tensor
46  inDims = [2, 2, 6, 8]
47  tensorData = HomogenTensor(inDims, TensorIface.doAllocate, 1.0)
48 
49  # Create an algorithm to compute forward 2D locally connected layer results using default method
50  locallyconnected2dLayerForward = layers.locallyconnected2d.forward.Batch()
51  locallyconnected2dLayerForward.input.setInput(layers.forward.data, tensorData)
52 
53  # Compute forward 2D locally connected layer results
54  forwardResult = locallyconnected2dLayerForward.compute()
55 
56  printTensor(forwardResult.getResult(layers.forward.value), "Forward 2D locally connected layer result (first 5 rows):", 5, 15)
57  printTensor(forwardResult.getLayerData(layers.locallyconnected2d.auxWeights), "2D locally connected layer weights (first 5 rows):", 5, 15)
58 
59  gDims = forwardResult.getResult(layers.forward.value).getDimensions()
60 
61  # Create input gradient tensor for backward 2D locally connected layer
62  tensorDataBack = HomogenTensor(gDims, TensorIface.doAllocate, 0.01)
63 
64  # Create an algorithm to compute backward 2D locally connected layer results using default method
65  locallyconnected2dLayerBackward = layers.locallyconnected2d.backward.Batch()
66  locallyconnected2dLayerBackward.input.setInput(layers.backward.inputGradient, tensorDataBack)
67  locallyconnected2dLayerBackward.input.setInputLayerData(layers.backward.inputFromForward, forwardResult.getResultLayerData(layers.forward.resultForBackward))
68 
69  # Compute backward 2D locally connected layer results
70  backwardResult = locallyconnected2dLayerBackward.compute()
71 
72  printTensor(backwardResult.getResult(layers.backward.gradient),
73  "2D locally connected layer backpropagation gradient result (first 5 rows):", 5, 15)
74  printTensor(backwardResult.getResult(layers.backward.weightDerivatives),
75  "2D locally connected layer backpropagation weightDerivative result (first 5 rows):", 5, 15)
76  printTensor(backwardResult.getResult(layers.backward.biasDerivatives),
77  "2D locally connected layer backpropagation biasDerivative result (first 5 rows):", 5, 15)

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