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

mse_dense_batch.py

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.

1 # file: mse_dense_batch.py
2 #===============================================================================
3 # Copyright 2014-2019 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 the mean squared error objective function
19 # !*****************************************************************************
20 
21 
22 #
23 ## <a name="DAAL-EXAMPLE-PY-MSE_BATCH"></a>
24 ## \example mse_dense_batch.py
25 #
26 
27 import os
28 import sys
29 
30 import numpy as np
31 
32 import daal.algorithms.optimization_solver as optimization_solver
33 import daal.algorithms.optimization_solver.mse
34 
35 from daal.data_management import (
36  DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
37 )
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 printNumericTable
43 
44 datasetFileName = os.path.join('..', 'data', 'batch', 'mse.csv')
45 nFeatures = 3
46 
47 argumentValue = np.array([[-1], [0.1], [0.15], [-0.5]], dtype=np.float64)
48 
49 if __name__ == "__main__":
50 
51  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
52  dataSource = FileDataSource(datasetFileName,
53  DataSourceIface.notAllocateNumericTable,
54  DataSourceIface.doDictionaryFromContext)
55 
56  # Create Numeric Tables for data and values for dependent variable
57  data = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
58  dependentVariables = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
59  mergedData = MergedNumericTable(data, dependentVariables)
60 
61  # Retrieve the data from the input file
62  dataSource.loadDataBlock(mergedData)
63 
64  nVectors = data.getNumberOfRows()
65 
66  # Create the MSE objective function objects to compute the MSE objective function result using the default method
67  mseObjectiveFunction = optimization_solver.mse.Batch(nVectors)
68 
69  # Set input objects for the MSE objective function
70  mseObjectiveFunction.input.set(optimization_solver.mse.data, data)
71  mseObjectiveFunction.input.set(optimization_solver.mse.dependentVariables, dependentVariables)
72  mseObjectiveFunction.input.set(optimization_solver.mse.argument, HomogenNumericTable(argumentValue))
73  mseObjectiveFunction.parameter().resultsToCompute = (
74  optimization_solver.objective_function.gradient |
75  optimization_solver.objective_function.value |
76  optimization_solver.objective_function.hessian
77  )
78 
79  # Compute the MSE objective function result
80  # Result class from optimization_solver.objective_function
81  res = mseObjectiveFunction.compute()
82 
83  # Print computed the MSE objective function result
84  printNumericTable(res.get(optimization_solver.objective_function.valueIdx),
85  "Value")
86  printNumericTable(res.get(optimization_solver.objective_function.gradientIdx),
87  "Gradient")
88  printNumericTable(res.get(optimization_solver.objective_function.hessianIdx),
89  "Hessian")

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