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.
31 from daal.algorithms.neural_networks
import initializers
32 from daal.algorithms.neural_networks
import layers
33 from daal.algorithms
import optimization_solver
34 from daal.algorithms.neural_networks
import training, prediction
35 from daal.data_management
import NumericTable, HomogenNumericTable
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
43 trainDatasetFile = os.path.join(
"..",
"data",
"batch",
"neural_network_train.csv")
44 trainGroundTruthFile = os.path.join(
"..",
"data",
"batch",
"neural_network_train_ground_truth.csv")
45 testDatasetFile = os.path.join(
"..",
"data",
"batch",
"neural_network_test.csv")
46 testGroundTruthFile = os.path.join(
"..",
"data",
"batch",
"neural_network_test_ground_truth.csv")
57 fullyConnectedLayer1 = layers.fullyconnected.Batch(5)
58 fullyConnectedLayer1.parameter.weightsInitializer = initializers.uniform.Batch(-0.001, 0.001)
59 fullyConnectedLayer1.parameter.biasesInitializer = initializers.uniform.Batch(0, 0.5)
62 fullyConnectedLayer2 = layers.fullyconnected.Batch(2)
63 fullyConnectedLayer2.parameter.weightsInitializer = initializers.uniform.Batch(0.5, 1)
64 fullyConnectedLayer2.parameter.biasesInitializer = initializers.uniform.Batch(0.5, 1)
67 softmaxCrossEntropyLayer = layers.loss.softmax_cross.Batch()
70 topology = training.Topology()
73 topology.push_back(fullyConnectedLayer1)
74 topology.push_back(fullyConnectedLayer2)
75 topology.push_back(softmaxCrossEntropyLayer)
76 topology.get(fc1).addNext(fc2)
77 topology.get(fc2).addNext(sm1)
83 trainingData = readTensorFromCSV(trainDatasetFile)
84 trainingGroundTruth = readTensorFromCSV(trainGroundTruthFile,
True)
86 sgdAlgorithm = optimization_solver.sgd.Batch(fptype=np.float32)
90 sgdAlgorithm.parameter.learningRateSequence = HomogenNumericTable(1, 1, NumericTable.doAllocate, learningRate)
92 sgdAlgorithm.parameter.batchSize = batchSize
93 sgdAlgorithm.parameter.nIterations = int(trainingData.getDimensionSize(0) / sgdAlgorithm.parameter.batchSize)
96 net = training.Batch(sgdAlgorithm)
98 sampleSize = trainingData.getDimensions()
99 sampleSize[0] = batchSize
102 topology = configureNet()
103 net.initialize(sampleSize, topology)
106 net.input.setInput(training.data, trainingData)
107 net.input.setInput(training.groundTruth, trainingGroundTruth)
110 trainingModel = net.compute().get(training.model)
112 return trainingModel.getPredictionModel_Float32()
115 def testModel(predictionModel):
117 predictionData = readTensorFromCSV(testDatasetFile)
120 net = prediction.Batch()
122 net.parameter.batchSize = predictionData.getDimensionSize(0)
125 net.input.setModelInput(prediction.model, predictionModel)
126 net.input.setTensorInput(prediction.data, predictionData)
133 def printResults(predictionResult):
135 predictionGroundTruth = readTensorFromCSV(testGroundTruthFile)
137 printTensors(predictionGroundTruth, predictionResult.getResult(prediction.prediction),
138 "Ground truth",
"Neural network predictions: each class probability",
139 "Neural network classification results (first 20 observations):", 20)
143 if __name__ ==
"__main__":
145 predictionModel = trainModel()
147 predictionResult = testModel(predictionModel)
149 printResults(predictionResult)