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

loss_logistic_entr_layer_dense_batch.py

1 # file: loss_logistic_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 logistic cross-entropy 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.algorithms.neural_networks.layers import loss
34 from daal.algorithms.neural_networks.layers.loss import logistic_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 datasetName = os.path.join("..", "data", "batch", "logistic_cross_entropy_layer.csv")
43 datasetGroundTruthName = os.path.join("..", "data", "batch", "logistic_cross_entropy_layer_ground_truth.csv")
44 
45 if __name__ == "__main__":
46 
47  # Retrieve the input data
48  tensorData = readTensorFromCSV(datasetName)
49  groundTruth = readTensorFromCSV(datasetGroundTruthName)
50 
51  # Create an algorithm to compute forward logistic cross-entropy layer results using default method
52  logisticCrossLayerForward = loss.logistic_cross.forward.Batch(method=loss.logistic_cross.defaultDense)
53 
54  # Set input objects for the forward logistic_cross layer
55  logisticCrossLayerForward.input.setInput(layers.forward.data, tensorData)
56  logisticCrossLayerForward.input.setInput(loss.forward.groundTruth, groundTruth)
57 
58  # Compute forward logistic_cross layer results
59  forwardResult = logisticCrossLayerForward.compute()
60 
61  # Print the results of the forward logistic_cross layer
62  printTensor(forwardResult.getResult(layers.forward.value), "Forward logistic cross-entropy layer result (first 5 rows):", 5)
63  printTensor(forwardResult.getLayerData(loss.logistic_cross.auxGroundTruth), "Logistic Cross-Entropy layer ground truth (first 5 rows):", 5)
64 
65  # Create an algorithm to compute backward logistic_cross layer results using default method
66  logisticCrossLayerBackward = logistic_cross.backward.Batch(method=loss.logistic_cross.defaultDense)
67 
68  # Set input objects for the backward logistic_cross layer
69  logisticCrossLayerBackward.input.setInputLayerData(layers.backward.inputFromForward, forwardResult.getResultLayerData(layers.forward.resultForBackward))
70 
71  # Compute backward logistic_cross layer results
72  backwardResult = logisticCrossLayerBackward.compute()
73 
74  # Print the results of the backward logistic_cross layer
75  printTensor(backwardResult.getResult(layers.backward.gradient), "Backward logistic cross-entropy layer result (first 5 rows):", 5)

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