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

initializers_dense_batch.py

1 # file: initializers_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 initializers
19 # !
20 # !*****************************************************************************
21 
22 #
23 ## <a name="DAAL-EXAMPLE-PY-INITIALIZERS_DENSE_BATCH"></a>
24 ## \example initializers_dense_batch.py
25 #
26 
27 import os
28 import sys
29 
30 from daal.algorithms.neural_networks import layers
31 from daal.algorithms.neural_networks import initializers
32 from daal.data_management import HomogenTensor, TensorIface
33 
34 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
35 if utils_folder not in sys.path:
36  sys.path.insert(0, utils_folder)
37 from utils import printTensor
38 
39 if __name__ == "__main__":
40  # Create collection of dimension sizes of the input data tensor
41  inDims = [2, 1, 3, 4]
42  tensorData = HomogenTensor(inDims, TensorIface.doAllocate)
43 
44  # Fill tensor data using truncated gaussian initializer
45  # Create an algorithm to initialize data using default method
46  truncatedGaussInitializer = initializers.truncated_gaussian.Batch(0.0, 1.0)
47 
48  # Set input object and parameters for the truncated gaussian initializer
49  truncatedGaussInitializer.input.set(initializers.data, tensorData)
50 
51  # Compute truncated gaussian initializer
52  truncatedGaussInitializer.compute()
53 
54  # Print the results of the truncated gaussian initializer
55  printTensor(tensorData, "Data with truncated gaussian distribution:")
56 
57 
58  # Fill tensor data using gaussian initializer
59  # Create an algorithm to initialize data using default method
60  gaussInitializer = initializers.gaussian.Batch(1.0, 0.5)
61 
62  # Set input object and parameters for the gaussian initializer
63  gaussInitializer.input.set(initializers.data, tensorData)
64 
65  # Compute gaussian initializer
66  gaussInitializer.compute()
67 
68  # Print the results of the gaussian initializer
69  printTensor(tensorData, "Data with gaussian distribution:")
70 
71 
72  # Fill tensor data using uniform initializer
73  # Create an algorithm to initialize data using default method
74  uniformInitializer = initializers.uniform.Batch(-5.0, 5.0)
75 
76  # Set input object and parameters for the uniform initializer
77  uniformInitializer.input.set(initializers.data, tensorData)
78 
79  # Compute uniform initializer
80  uniformInitializer.compute()
81 
82  # Print the results of the uniform initializer
83  printTensor(tensorData, "Data with uniform distribution:")
84 
85 
86  # Fill layer weights using xavier initializer
87  # Create an algorithm to compute forward fully-connected layer results using default method
88  fullyconnectedLayerForward = layers.fullyconnected.forward.Batch(5)
89 
90  # Set input objects and parameter for the forward fully-connected layer
91  fullyconnectedLayerForward.input.setInput(layers.forward.data, tensorData)
92  fullyconnectedLayerForward.parameter.weightsInitializer = initializers.xavier.Batch()
93 
94  # Compute forward fully-connected layer results
95  fullyconnectedLayerForward.compute()
96 
97  # Print the results of the xavier initializer
98  printTensor(fullyconnectedLayerForward.input.getInput(layers.forward.weights), "Weights filled by xavier initializer:")

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