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

neural_net_predict_dense_batch.py

1 # file: neural_net_predict_dense_batch.py
2 #===============================================================================
3 # Copyright 2014-2017 Intel Corporation
4 # All Rights Reserved.
5 #
6 # If this software was obtained under the Intel Simplified Software License,
7 # the following terms apply:
8 #
9 # The source code, information and material ("Material") contained herein is
10 # owned by Intel Corporation or its suppliers or licensors, and title to such
11 # Material remains with Intel Corporation or its suppliers or licensors. The
12 # Material contains proprietary information of Intel or its suppliers and
13 # licensors. The Material is protected by worldwide copyright laws and treaty
14 # provisions. No part of the Material may be used, copied, reproduced,
15 # modified, published, uploaded, posted, transmitted, distributed or disclosed
16 # in any way without Intel's prior express written permission. No license under
17 # any patent, copyright or other intellectual property rights in the Material
18 # is granted to or conferred upon you, either expressly, by implication,
19 # inducement, estoppel or otherwise. Any license under such intellectual
20 # property rights must be express and approved by Intel in writing.
21 #
22 # Unless otherwise agreed by Intel in writing, you may not remove or alter this
23 # notice or any other notice embedded in Materials by Intel or Intel's
24 # suppliers or licensors in any way.
25 #
26 #
27 # If this software was obtained under the Apache License, Version 2.0 (the
28 # "License"), the following terms apply:
29 #
30 # You may not use this file except in compliance with the License. You may
31 # obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
32 #
33 #
34 # Unless required by applicable law or agreed to in writing, software
35 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 #
38 # See the License for the specific language governing permissions and
39 # limitations under the License.
40 #===============================================================================
41 
42 #
43 # ! Content:
44 # ! Python example of neural network scoring
45 # !*****************************************************************************
46 
47 #
48 ## <a name="DAAL-EXAMPLE-PY-NEURAL_NET_PREDICTION_DENSE_BATCH"></a>
49 ## \example neural_net_predict_dense_batch.py
50 #
51 
52 import os
53 import sys
54 
55 from daal.algorithms.neural_networks import layers
56 from daal.algorithms.neural_networks import prediction
57 
58 import daal.algorithms.neural_networks.layers.fullyconnected.forward
59 import daal.algorithms.neural_networks.layers.softmax.forward
60 
61 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
62 if utils_folder not in sys.path:
63  sys.path.insert(0, utils_folder)
64 from utils import printTensors, readTensorFromCSV
65 
66 # Input data set parameters
67 testDatasetFile = os.path.join("..", "data", "batch", "neural_network_test.csv")
68 testGroundTruthFile = os.path.join("..", "data", "batch", "neural_network_test_ground_truth.csv")
69 
70 # Weights and biases obtained on the training stage
71 fc1WeightsFile = os.path.join("..", "data", "batch", "fc1_weights.csv")
72 fc1BiasesFile = os.path.join("..", "data", "batch", "fc1_biases.csv")
73 fc2WeightsFile = os.path.join("..", "data", "batch", "fc2_weights.csv")
74 fc2BiasesFile = os.path.join("..", "data", "batch", "fc2_biases.csv")
75 
76 fc1 = 0
77 fc2 = 1
78 sm1 = 2
79 
80 
81 def configureNet():
82  # Create layers of the neural network
83  # Create first fully-connected layer
84  fullyConnectedLayer1 = layers.fullyconnected.forward.Batch(5)
85 
86  # Create second fully-connected layer
87  fullyConnectedLayer2 = layers.fullyconnected.forward.Batch(2)
88 
89  # Create softmax layer
90  softmaxLayer = layers.softmax.forward.Batch()
91 
92  # Create topology of the neural network
93  topology = prediction.Topology()
94 
95  # Add layers to the topology of the neural network
96  topology.push_back(fullyConnectedLayer1)
97  topology.push_back(fullyConnectedLayer2)
98  topology.push_back(softmaxLayer)
99  topology.get(fc1).addNext(fc2)
100  topology.get(fc2).addNext(sm1)
101  return topology
102 
103 
104 def createModel():
105  # Read testing data set from a .csv file and create a tensor to store input data
106  predictionData = readTensorFromCSV(testDatasetFile)
107 
108  # Configure the neural network
109  topology = configureNet()
110 
111  # Create prediction model of the neural network
112  predictionModel = prediction.Model(topology)
113 
114  # Read 1st fully-connected layer weights and biases from CSV file
115  # 1st fully-connected layer weights are a 2D tensor of size 5 x 20
116  fc1Weights = readTensorFromCSV(fc1WeightsFile)
117  # 1st fully-connected layer biases are a 1D tensor of size 5
118  fc1Biases = readTensorFromCSV(fc1BiasesFile)
119 
120  # Set weights and biases of the 1st fully-connected layer
121  fc1Input = predictionModel.getLayer(fc1).getLayerInput()
122  fc1Input.setInput(layers.forward.weights, fc1Weights)
123  fc1Input.setInput(layers.forward.biases, fc1Biases)
124 
125  # Set flag that specifies that weights and biases of the 1st fully-connected layer are initialized
126  fc1Parameter = predictionModel.getLayer(fc1).getLayerParameter()
127  fc1Parameter.weightsAndBiasesInitialized = True
128 
129  # Read 2nd fully-connected layer weights and biases from CSV file
130  # 2nd fully-connected layer weights are a 2D tensor of size 2 x 5
131  fc2Weights = readTensorFromCSV(fc2WeightsFile)
132  # 2nd fully-connected layer biases are a 1D tensor of size 2
133  fc2Biases = readTensorFromCSV(fc2BiasesFile)
134 
135  # Set weights and biases of the 2nd fully-connected layer
136  fc2Input = predictionModel.getLayer(fc2).getLayerInput()
137  fc2Input.setInput(layers.forward.weights, fc2Weights)
138  fc2Input.setInput(layers.forward.biases, fc2Biases)
139 
140  # Set flag that specifies that weights and biases of the 2nd fully-connected layer are initialized
141  fc2Parameter = predictionModel.getLayer(fc2).getLayerParameter()
142  fc2Parameter.weightsAndBiasesInitialized = True
143 
144  return (predictionData, predictionModel)
145 
146 
147 def testModel(predictionData, predictionModel):
148  # Create an algorithm to compute the neural network predictions
149  net = prediction.Batch()
150 
151  net.parameter.batchSize = predictionData.getDimensionSize(0)
152 
153  # Set input objects for the prediction neural network
154  net.input.setModelInput(prediction.model, predictionModel)
155  net.input.setTensorInput(prediction.data, predictionData)
156 
157  # Run the neural network prediction and
158  # get results of the neural network prediction
159  return net.compute()
160 
161 
162 def printResults(predictionResult):
163  # Read testing ground truth from a .csv file and create a tensor to store the data
164  predictionGroundTruth = readTensorFromCSV(testGroundTruthFile)
165  printTensors(predictionGroundTruth, predictionResult.getResult(prediction.prediction),
166  "Ground truth", "Neural network predictions: each class probability",
167  "Neural network classification results (first 20 observations):", 20)
168 
169 
170 if __name__ == "__main__":
171  (predictionData, predictionModel) = createModel()
172 
173  predictionResult = testModel(predictionData, predictionModel)
174 
175  printResults(predictionResult)

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