Deprecation Notice: With the introduction of
daal4py, a package that supersedes PyDAAL, Intel is deprecating PyDAAL and will discontinue support starting with Intel® DAAL 2021 and Intel® Distribution for Python 2021. Until then Intel will continue to provide compatible pyDAAL
pip and
conda packages for newer releases of Intel DAAL and make it available in open source. However, Intel will not add the new features of Intel DAAL to pyDAAL. Intel recommends developers switch to and use daal4py.
Note: To find daal4py examples, refer to daal4py documentation
or browse github
repository.
29 from daal.algorithms.neural_networks
import layers
30 from daal.algorithms.neural_networks
import prediction
32 import daal.algorithms.neural_networks.layers.fullyconnected.forward
33 import daal.algorithms.neural_networks.layers.softmax.forward
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
41 testDatasetFile = os.path.join(
"..",
"data",
"batch",
"neural_network_test.csv")
42 testGroundTruthFile = os.path.join(
"..",
"data",
"batch",
"neural_network_test_ground_truth.csv")
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")
58 fullyConnectedLayer1 = layers.fullyconnected.forward.Batch(5)
61 fullyConnectedLayer2 = layers.fullyconnected.forward.Batch(2)
64 softmaxLayer = layers.softmax.forward.Batch()
67 topology = prediction.Topology()
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)
80 predictionData = readTensorFromCSV(testDatasetFile)
83 topology = configureNet()
86 predictionModel = prediction.Model(topology)
90 fc1Weights = readTensorFromCSV(fc1WeightsFile)
92 fc1Biases = readTensorFromCSV(fc1BiasesFile)
95 fc1Input = predictionModel.getLayer(fc1).getLayerInput()
96 fc1Input.setInput(layers.forward.weights, fc1Weights)
97 fc1Input.setInput(layers.forward.biases, fc1Biases)
100 fc1Parameter = predictionModel.getLayer(fc1).getLayerParameter()
101 fc1Parameter.weightsAndBiasesInitialized =
True
105 fc2Weights = readTensorFromCSV(fc2WeightsFile)
107 fc2Biases = readTensorFromCSV(fc2BiasesFile)
110 fc2Input = predictionModel.getLayer(fc2).getLayerInput()
111 fc2Input.setInput(layers.forward.weights, fc2Weights)
112 fc2Input.setInput(layers.forward.biases, fc2Biases)
115 fc2Parameter = predictionModel.getLayer(fc2).getLayerParameter()
116 fc2Parameter.weightsAndBiasesInitialized =
True
118 return (predictionData, predictionModel)
121 def testModel(predictionData, predictionModel):
123 net = prediction.Batch()
125 net.parameter.batchSize = predictionData.getDimensionSize(0)
128 net.input.setModelInput(prediction.model, predictionModel)
129 net.input.setTensorInput(prediction.data, predictionData)
136 def printResults(predictionResult):
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)
144 if __name__ ==
"__main__":
145 (predictionData, predictionModel) = createModel()
147 predictionResult = testModel(predictionData, predictionModel)
149 printResults(predictionResult)