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

softmax_layer_dense_batch.py

1 # file: softmax_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 softmax layer usage
19 # !
20 # !*****************************************************************************
21 
22 #
23 ## <a name="DAAL-EXAMPLE-PY-SOFTMAX_LAYER_BATCH"></a>
24 ## \example softmax_layer_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.layers import softmax
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, readTensorFromCSV
38 
39 # Input data set parameters
40 datasetName = os.path.join("..", "data", "batch", "layer.csv")
41 dimension = 1
42 
43 if __name__ == "__main__":
44 
45  # Read datasetFileName from a file and create a tensor to store input data
46  tensorData = readTensorFromCSV(datasetName)
47 
48  # Create an algorithm to compute forward softmax layer results using default method
49  softmaxLayerForward = softmax.forward.Batch()
50  softmaxLayerForward.parameter.dimension = dimension
51 
52  # Set input objects for the forward softmax layer
53  softmaxLayerForward.input.setInput(layers.forward.data, tensorData)
54 
55  # Compute forward softmax layer results
56  forwardResult = softmaxLayerForward.compute()
57 
58  # Print the results of the forward softmax layer
59  printTensor(forwardResult.getResult(layers.forward.value), "Forward softmax layer result (first 5 rows):", 5)
60 
61  # Get the size of forward softmax layer output
62  gDims = forwardResult.getResult(layers.forward.value).getDimensions()
63  tensorDataBack = HomogenTensor(gDims, TensorIface.doAllocate, 0.01)
64 
65  # Create an algorithm to compute backward softmax layer results using default method
66  softmaxLayerBackward = softmax.backward.Batch()
67  softmaxLayerBackward.parameter.dimension = dimension
68 
69  # Set input objects for the backward softmax layer
70  softmaxLayerBackward.input.setInput(layers.backward.inputGradient, tensorDataBack)
71  softmaxLayerBackward.input.setInputLayerData(layers.backward.inputFromForward, forwardResult.getResultLayerData(layers.forward.resultForBackward))
72 
73  # Compute backward softmax layer results
74  backwardResult = softmaxLayerBackward.compute()
75 
76  # Print the results of the backward softmax layer
77  printTensor(backwardResult.getResult(layers.backward.gradient), "Backward softmax layer result (first 5 rows):", 5)

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