Python* API Reference for Intel® Data Analytics Acceleration Library 2018 Update 3

batch_norm_layer_dense_batch.py

1 # file: batch_norm_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 batch normalization layer usage
19 # !
20 # !*****************************************************************************
21 
22 #
23 ## <a name="DAAL-EXAMPLE-PY-BATCH_NORMALIZATION_LAYER_BATCH"></a>
24 ## \example batch_norm_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, readTensorFromCSV
37 
38 # Input data set name
39 datasetFileName = os.path.join("..", "data", "batch", "layer.csv")
40 dimension = 1
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 
47  printTensor(data, "Forward batch normalization layer input (first 5 rows):", 5)
48 
49  # Get collection of dimension sizes of the input data tensor
50  dataDims = data.getDimensions()
51  dimensionSize = dataDims[dimension]
52 
53  # Create a collection of dimension sizes of input weights, biases, population mean and variance tensors
54  dimensionSizes = [dimensionSize]
55 
56  # Create input weights, biases, population mean and population variance tensors
57  weights = HomogenTensor(dimensionSizes, TensorIface.doAllocate, 1.0)
58  biases = HomogenTensor(dimensionSizes, TensorIface.doAllocate, 2.0)
59  populationMean = HomogenTensor(dimensionSizes, TensorIface.doAllocate, 0.0)
60  populationVariance = HomogenTensor(dimensionSizes, TensorIface.doAllocate, 0.0)
61 
62  # Create an algorithm to compute forward batch normalization layer results using default method
63  forwardLayer = layers.batch_normalization.forward.Batch()
64  forwardLayer.parameter.dimension = dimension
65  forwardLayer.input.setInput(layers.forward.data, data)
66  forwardLayer.input.setInput(layers.forward.weights, weights)
67  forwardLayer.input.setInput(layers.forward.biases, biases)
68  forwardLayer.input.setInputLayerData(layers.batch_normalization.forward.populationMean, populationMean)
69  forwardLayer.input.setInputLayerData(layers.batch_normalization.forward.populationVariance, populationVariance)
70 
71  # Compute forward batch normalization layer results
72  forwardResult = forwardLayer.compute()
73 
74  printTensor(forwardResult.getResult(layers.forward.value), "Forward batch normalization layer result (first 5 rows):", 5)
75  printTensor(forwardResult.getLayerData(layers.batch_normalization.auxMean), "Mini-batch mean (first 5 values):", 5)
76  printTensor(forwardResult.getLayerData(layers.batch_normalization.auxStandardDeviation), "Mini-batch standard deviation (first 5 values):", 5)
77  printTensor(forwardResult.getLayerData(layers.batch_normalization.auxPopulationMean), "Population mean (first 5 values):", 5)
78  printTensor(forwardResult.getLayerData(layers.batch_normalization.auxPopulationVariance), "Population variance (first 5 values):", 5)
79 
80  # Create input gradient tensor for backward batch normalization layer
81  inputGradientTensor = HomogenTensor(dataDims, TensorIface.doAllocate, 10.0)
82 
83  # Create an algorithm to compute backward batch normalization layer results using default method
84  backwardLayer = layers.batch_normalization.backward.Batch()
85  backwardLayer.parameter.dimension = dimension
86  backwardLayer.input.setInput(layers.backward.inputGradient, inputGradientTensor)
87  backwardLayer.input.setInputLayerData(layers.backward.inputFromForward, forwardResult.getResultLayerData(layers.forward.resultForBackward))
88 
89  # Compute backward batch normalization layer results
90  backwardResult = backwardLayer.compute()
91 
92  printTensor(backwardResult.getResult(layers.backward.gradient), "Backward batch normalization layer result (first 5 rows):", 5)
93  printTensor(backwardResult.getResult(layers.backward.weightDerivatives), "Weight derivatives (first 5 values):", 5)
94  printTensor(backwardResult.getResult(layers.backward.biasDerivatives), "Bias derivatives (first 5 values):", 5)

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