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

neural_net_predict_dense_batch.py

1 # file: neural_net_predict_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 neural network scoring
21 # !*****************************************************************************
22 
23 #
24 
25 
26 #
27 
28 import os
29 import sys
30 
31 from daal.algorithms.neural_networks import layers
32 from daal.algorithms.neural_networks import prediction
33 
34 import daal.algorithms.neural_networks.layers.fullyconnected.forward
35 import daal.algorithms.neural_networks.layers.softmax.forward
36 
37 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
38 if utils_folder not in sys.path:
39  sys.path.insert(0, utils_folder)
40 from utils import printTensors, readTensorFromCSV
41 
42 # Input data set parameters
43 testDatasetFile = os.path.join("..", "data", "batch", "neural_network_test.csv")
44 testGroundTruthFile = os.path.join("..", "data", "batch", "neural_network_test_ground_truth.csv")
45 
46 # Weights and biases obtained on the training stage
47 fc1WeightsFile = os.path.join("..", "data", "batch", "fc1_weights.csv")
48 fc1BiasesFile = os.path.join("..", "data", "batch", "fc1_biases.csv")
49 fc2WeightsFile = os.path.join("..", "data", "batch", "fc2_weights.csv")
50 fc2BiasesFile = os.path.join("..", "data", "batch", "fc2_biases.csv")
51 
52 fc1 = 0
53 fc2 = 1
54 sm1 = 2
55 
56 
57 def configureNet():
58  # Create layers of the neural network
59  # Create first fully-connected layer
60  fullyConnectedLayer1 = layers.fullyconnected.forward.Batch(5)
61 
62  # Create second fully-connected layer
63  fullyConnectedLayer2 = layers.fullyconnected.forward.Batch(2)
64 
65  # Create softmax layer
66  softmaxLayer = layers.softmax.forward.Batch()
67 
68  # Create topology of the neural network
69  topology = prediction.Topology()
70 
71  # Add layers to the topology of the neural network
72  topology.push_back(fullyConnectedLayer1)
73  topology.push_back(fullyConnectedLayer2)
74  topology.push_back(softmaxLayer)
75  topology.get(fc1).addNext(fc2)
76  topology.get(fc2).addNext(sm1)
77  return topology
78 
79 
80 def createModel():
81  # Read testing data set from a .csv file and create a tensor to store input data
82  predictionData = readTensorFromCSV(testDatasetFile)
83 
84  # Configure the neural network
85  topology = configureNet()
86 
87  # Create prediction model of the neural network
88  predictionModel = prediction.Model(topology)
89 
90  # Read 1st fully-connected layer weights and biases from CSV file
91  # 1st fully-connected layer weights are a 2D tensor of size 5 x 20
92  fc1Weights = readTensorFromCSV(fc1WeightsFile)
93  # 1st fully-connected layer biases are a 1D tensor of size 5
94  fc1Biases = readTensorFromCSV(fc1BiasesFile)
95 
96  # Set weights and biases of the 1st fully-connected layer
97  fc1Input = predictionModel.getLayer(fc1).getLayerInput()
98  fc1Input.setInput(layers.forward.weights, fc1Weights)
99  fc1Input.setInput(layers.forward.biases, fc1Biases)
100 
101  # Set flag that specifies that weights and biases of the 1st fully-connected layer are initialized
102  fc1Parameter = predictionModel.getLayer(fc1).getLayerParameter()
103  fc1Parameter.weightsAndBiasesInitialized = True
104 
105  # Read 2nd fully-connected layer weights and biases from CSV file
106  # 2nd fully-connected layer weights are a 2D tensor of size 2 x 5
107  fc2Weights = readTensorFromCSV(fc2WeightsFile)
108  # 2nd fully-connected layer biases are a 1D tensor of size 2
109  fc2Biases = readTensorFromCSV(fc2BiasesFile)
110 
111  # Set weights and biases of the 2nd fully-connected layer
112  fc2Input = predictionModel.getLayer(fc2).getLayerInput()
113  fc2Input.setInput(layers.forward.weights, fc2Weights)
114  fc2Input.setInput(layers.forward.biases, fc2Biases)
115 
116  # Set flag that specifies that weights and biases of the 2nd fully-connected layer are initialized
117  fc2Parameter = predictionModel.getLayer(fc2).getLayerParameter()
118  fc2Parameter.weightsAndBiasesInitialized = True
119 
120  return (predictionData, predictionModel)
121 
122 
123 def testModel(predictionData, predictionModel):
124  # Create an algorithm to compute the neural network predictions
125  net = prediction.Batch()
126 
127  net.parameter.batchSize = predictionData.getDimensionSize(0)
128 
129  # Set input objects for the prediction neural network
130  net.input.setModelInput(prediction.model, predictionModel)
131  net.input.setTensorInput(prediction.data, predictionData)
132 
133  # Run the neural network prediction and
134  # get results of the neural network prediction
135  return net.compute()
136 
137 
138 def printResults(predictionResult):
139  # Read testing ground truth from a .csv file and create a tensor to store the data
140  predictionGroundTruth = readTensorFromCSV(testGroundTruthFile)
141  printTensors(predictionGroundTruth, predictionResult.getResult(prediction.prediction),
142  "Ground truth", "Neural network predictions: each class probability",
143  "Neural network classification results (first 20 observations):", 20)
144 
145 
146 if __name__ == "__main__":
147  (predictionData, predictionModel) = createModel()
148 
149  predictionResult = testModel(predictionData, predictionModel)
150 
151  printResults(predictionResult)

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