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

neural_net_predict_dense_batch.py

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

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