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

neural_net_dense_batch.py

1 # file: neural_net_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 training and scoring
21 # !*****************************************************************************
22 
23 #
24 
25 
26 #
27 
28 import os
29 import sys
30 
31 import numpy as np
32 
33 from daal.algorithms.neural_networks import initializers
34 from daal.algorithms.neural_networks import layers
35 from daal.algorithms import optimization_solver
36 from daal.algorithms.neural_networks import training, prediction
37 from daal.data_management import NumericTable, HomogenNumericTable
38 
39 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
40 if utils_folder not in sys.path:
41  sys.path.insert(0, utils_folder)
42 from utils import printTensors, readTensorFromCSV
43 
44 # Input data set parameters
45 trainDatasetFile = os.path.join("..", "data", "batch", "neural_network_train.csv")
46 trainGroundTruthFile = os.path.join("..", "data", "batch", "neural_network_train_ground_truth.csv")
47 testDatasetFile = os.path.join("..", "data", "batch", "neural_network_test.csv")
48 testGroundTruthFile = os.path.join("..", "data", "batch", "neural_network_test_ground_truth.csv")
49 
50 fc1 = 0
51 fc2 = 1
52 sm1 = 2
53 
54 batchSize = 10
55 
56 def configureNet():
57  # Create layers of the neural network
58  # Create fully-connected layer and initialize layer parameters
59  fullyConnectedLayer1 = layers.fullyconnected.Batch(5)
60  fullyConnectedLayer1.parameter.weightsInitializer = initializers.uniform.Batch(-0.001, 0.001)
61  fullyConnectedLayer1.parameter.biasesInitializer = initializers.uniform.Batch(0, 0.5)
62 
63  # Create fully-connected layer and initialize layer parameters
64  fullyConnectedLayer2 = layers.fullyconnected.Batch(2)
65  fullyConnectedLayer2.parameter.weightsInitializer = initializers.uniform.Batch(0.5, 1)
66  fullyConnectedLayer2.parameter.biasesInitializer = initializers.uniform.Batch(0.5, 1)
67 
68  # Create softmax layer and initialize layer parameters
69  softmaxCrossEntropyLayer = layers.loss.softmax_cross.Batch()
70 
71  # Create configuration of the neural network with layers
72  topology = training.Topology()
73 
74  # Add layers to the topology of the neural network
75  topology.push_back(fullyConnectedLayer1)
76  topology.push_back(fullyConnectedLayer2)
77  topology.push_back(softmaxCrossEntropyLayer)
78  topology.get(fc1).addNext(fc2)
79  topology.get(fc2).addNext(sm1)
80  return topology
81 
82 
83 def trainModel():
84  # Read training data set from a .csv file and create a tensor to store input data
85  trainingData = readTensorFromCSV(trainDatasetFile)
86  trainingGroundTruth = readTensorFromCSV(trainGroundTruthFile, True)
87 
88  sgdAlgorithm = optimization_solver.sgd.Batch(fptype=np.float32)
89 
90  # Set learning rate for the optimization solver used in the neural network
91  learningRate = 0.001
92  sgdAlgorithm.parameter.learningRateSequence = HomogenNumericTable(1, 1, NumericTable.doAllocate, learningRate)
93  # Set the batch size for the neural network training
94  sgdAlgorithm.parameter.batchSize = batchSize
95  sgdAlgorithm.parameter.nIterations = int(trainingData.getDimensionSize(0) / sgdAlgorithm.parameter.batchSize)
96 
97  # Create an algorithm to train neural network
98  net = training.Batch(sgdAlgorithm)
99 
100  sampleSize = trainingData.getDimensions()
101  sampleSize[0] = batchSize
102 
103  # Configure the neural network
104  topology = configureNet()
105  net.initialize(sampleSize, topology)
106 
107  # Pass a training data set and dependent values to the algorithm
108  net.input.setInput(training.data, trainingData)
109  net.input.setInput(training.groundTruth, trainingGroundTruth)
110 
111  # Run the neural network training and retrieve training model
112  trainingModel = net.compute().get(training.model)
113  # return prediction model
114  return trainingModel.getPredictionModel_Float32()
115 
116 
117 def testModel(predictionModel):
118  # Read testing data set from a .csv file and create a tensor to store input data
119  predictionData = readTensorFromCSV(testDatasetFile)
120 
121  # Create an algorithm to compute the neural network predictions
122  net = prediction.Batch()
123 
124  net.parameter.batchSize = predictionData.getDimensionSize(0)
125 
126  # Set input objects for the prediction neural network
127  net.input.setModelInput(prediction.model, predictionModel)
128  net.input.setTensorInput(prediction.data, predictionData)
129 
130  # Run the neural network prediction
131  # and return results of the neural network prediction
132  return net.compute()
133 
134 
135 def printResults(predictionResult):
136  # Read testing ground truth from a .csv file and create a tensor to store the data
137  predictionGroundTruth = readTensorFromCSV(testGroundTruthFile)
138 
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 topology = ""
145 if __name__ == "__main__":
146 
147  predictionModel = trainModel()
148 
149  predictionResult = testModel(predictionModel)
150 
151  printResults(predictionResult)

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