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

split_layer_dense_batch.py

1 # file: split_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 split 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 split
34 
35 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
36 if utils_folder not in sys.path:
37  sys.path.insert(0, utils_folder)
38 from utils import printTensor, readTensorFromCSV
39 
40 # Input data set parameters
41 datasetName = os.path.join("..", "data", "batch", "layer.csv")
42 nOutputs = 3
43 nInputs = 3
44 
45 if __name__ == "__main__":
46 
47  # Read datasetFileName from a file and create a tensor to store input data
48  tensorData = readTensorFromCSV(datasetName)
49 
50  # Create an algorithm to compute forward split layer results using default method
51  splitLayerForward = split.forward.Batch()
52 
53  # Set parameters for the forward split layer
54  splitLayerForward.parameter.nOutputs = nOutputs
55  splitLayerForward.parameter.nInputs = nInputs
56 
57  # Set input objects for the forward split layer
58  splitLayerForward.input.setInput(layers.forward.data, tensorData)
59 
60  printTensor(tensorData, "Split layer input (first 5 rows):", 5)
61 
62  # Compute forward split layer results
63  forwardResult = splitLayerForward.compute()
64 
65  # Print the results of the forward split layer
66  for i in range(nOutputs):
67  printTensor(forwardResult.getResultLayerData(split.forward.valueCollection, i),
68  "Forward split layer result (first 5 rows):", 5)
69 
70  # Create an algorithm to compute backward split layer results using default method
71  splitLayerBackward = split.backward.Batch()
72 
73  # Set parameters for the backward split layer
74  splitLayerBackward.parameter.nOutputs = nOutputs
75  splitLayerBackward.parameter.nInputs = nInputs
76 
77  # Set input objects for the backward split layer
78  splitLayerBackward.input.setInputLayerData(split.backward.inputGradientCollection,
79  forwardResult.getResultLayerData(split.forward.valueCollection))
80 
81  # Compute backward split layer results
82  backwardResult = splitLayerBackward.compute()
83 
84  # Print the results of the backward split layer
85  printTensor(backwardResult.getResult(layers.backward.gradient), "Backward split layer result (first 5 rows):", 5)

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