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

mse_dense_batch.py

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

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