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

loss_softmax_entr_layer_dense_batch.py

1 # file: loss_softmax_entr_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 softmax cross-entropy layer usage
21 # !
22 # !*****************************************************************************
23 
24 #
25 
26 
27 #
28 
29 import os
30 import sys
31 from daal.data_management import HomogenTensor
32 from daal.algorithms.neural_networks import layers
33 from daal.algorithms.neural_networks.layers import loss
34 from daal.algorithms.neural_networks.layers.loss import softmax_cross
35 
36 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
37 if utils_folder not in sys.path:
38  sys.path.insert(0, utils_folder)
39 from utils import printTensor, readTensorFromCSV
40 
41 # Input data set parameters
42 datasetGroundTruth = [[[1, 0, 0, 1]],[[0, 0, 1, 1]],[[1, 0, 0, 1]]];
43 dataset = [[[ 1, 2, 3, 4],[ 5, 6, 7, 8]],[[9, 10, 11, 12],[13, 14, 15, 16]],[[17, 18, 19, 20],[21, 22, 23, 24]]];
44 
45 
46 if __name__ == "__main__":
47 
48  # Retrieve the input data
49  groundTruth = HomogenTensor(datasetGroundTruth)
50  tensorData = HomogenTensor(dataset)
51 
52  printTensor(tensorData, "Forward softmax cross-entropy layer input data:");
53  printTensor(groundTruth, "Forward softmax cross-entropy layer input ground truth:");
54 
55  # Create an algorithm to compute forward softmax cross-entropy layer results using default method
56  softmaxCrossLayerForward = loss.softmax_cross.forward.Batch(method=loss.softmax_cross.defaultDense)
57 
58  # Set input objects for the forward softmax_cross layer
59  softmaxCrossLayerForward.input.setInput(layers.forward.data, tensorData)
60  softmaxCrossLayerForward.input.setInput(loss.forward.groundTruth, groundTruth)
61 
62  # Compute forward softmax_cross layer results
63  forwardResult = softmaxCrossLayerForward.compute()
64 
65  # Print the results of the forward softmax_cross layer
66  printTensor(forwardResult.getResult(layers.forward.value), "Forward softmax cross-entropy layer result (first 5 rows):", 5)
67  printTensor(forwardResult.getLayerData(loss.softmax_cross.auxProbabilities), "Softmax Cross-Entropy layer probabilities estimations (first 5 rows):", 5)
68  printTensor(forwardResult.getLayerData(loss.softmax_cross.auxGroundTruth), "Softmax Cross-Entropy layer ground truth (first 5 rows):", 5)
69 
70  # Create an algorithm to compute backward softmax_cross layer results using default method
71  softmaxCrossLayerBackward = softmax_cross.backward.Batch(method=loss.softmax_cross.defaultDense)
72 
73  # Set input objects for the backward softmax_cross layer
74  softmaxCrossLayerBackward.input.setInputLayerData(layers.backward.inputFromForward, forwardResult.getResultLayerData(layers.forward.resultForBackward))
75 
76  # Compute backward softmax_cross layer results
77  backwardResult = softmaxCrossLayerBackward.compute()
78 
79  # Print the results of the backward softmax_cross layer
80  printTensor(backwardResult.getResult(layers.backward.gradient), "Backward softmax cross-entropy layer result (first 5 rows):", 5)

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