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

conv2d_layer_dense_batch.py

1 # file: conv2d_layer_dense_batch.py
2 #===============================================================================
3 # Copyright 2014-2019 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 two-dimensional convolution layer usage
19 # !
20 # !*****************************************************************************
21 
22 #
23 
24 
25 #
26 
27 import os
28 import sys
29 
30 from daal.algorithms.neural_networks import layers
31 from daal.data_management import HomogenTensor, TensorIface
32 
33 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
34 if utils_folder not in sys.path:
35  sys.path.insert(0, utils_folder)
36 from utils import printTensor
37 
38 # Input data set name
39 datasetFileName = os.path.join("..", "data", "batch", "layer.csv")
40 
41 if __name__ == "__main__":
42 
43  # Create collection of dimension sizes of the input data tensor
44  inDims = [2, 1, 16, 16]
45  tensorData = HomogenTensor(inDims, TensorIface.doAllocate, 1.0)
46 
47  # Create an algorithm to compute forward two-dimensional convolution layer results using default method
48  convolution2dLayerForward = layers.convolution2d.forward.Batch()
49  convolution2dLayerForward.input.setInput(layers.forward.data, tensorData)
50 
51  # Compute forward two-dimensional convolution layer results
52  forwardResult = convolution2dLayerForward.compute()
53 
54  printTensor(forwardResult.getResult(layers.forward.value), "Two-dimensional convolution layer result (first 5 rows):", 5, 15)
55  printTensor(forwardResult.getLayerData(layers.convolution2d.auxWeights),
56  "Two-dimensional convolution layer weights (first 5 rows):", 5, 15)
57 
58  gDims = forwardResult.getResult(layers.forward.value).getDimensions()
59 
60  # Create input gradient tensor for backward two-dimensional convolution layer
61  tensorDataBack = HomogenTensor(gDims, TensorIface.doAllocate, 0.01)
62 
63  # Create an algorithm to compute backward two-dimensional convolution layer results using default method
64  convolution2dLayerBackward = layers.convolution2d.backward.Batch()
65  convolution2dLayerBackward.input.setInput(layers.backward.inputGradient, tensorDataBack)
66  convolution2dLayerBackward.input.setInputLayerData(layers.backward.inputFromForward, forwardResult.getResultLayerData(layers.forward.resultForBackward))
67 
68  # Compute backward two-dimensional convolution layer results
69  backwardResult = convolution2dLayerBackward.compute()
70 
71  printTensor(backwardResult.getResult(layers.backward.gradient),
72  "Two-dimensional convolution layer backpropagation gradient result (first 5 rows):", 5, 15)
73  printTensor(backwardResult.getResult(layers.backward.weightDerivatives),
74  "Two-dimensional convolution layer backpropagation weightDerivative result (first 5 rows):", 5, 15)
75  printTensor(backwardResult.getResult(layers.backward.biasDerivatives),
76  "Two-dimensional convolution layer backpropagation biasDerivative result (first 5 rows):", 5, 15)

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