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

lbfgs_opt_res_dense_batch.py

1 # file: lbfgs_opt_res_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 LBFGS algorithm
21 # !*****************************************************************************
22 
23 #
24 
25 
26 #
27 
28 import os
29 import sys
30 
31 import numpy as np
32 
33 import daal.algorithms.optimization_solver as optimization_solver
34 import daal.algorithms.optimization_solver.mse
35 import daal.algorithms.optimization_solver.lbfgs
36 import daal.algorithms.optimization_solver.iterative_solver
37 
38 from daal.data_management import (
39  DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
40 )
41 
42 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
43 if utils_folder not in sys.path:
44  sys.path.insert(0, utils_folder)
45 from utils import printNumericTable
46 
47 datasetFileName = os.path.join('..', 'data', 'batch', 'lbfgs.csv')
48 
49 nFeatures = 10
50 halfNIterations = 500
51 nIterations = halfNIterations * 2
52 stepLength = 1.0e-4
53 
54 initialPoint = np.array([[100], [100], [100], [100], [100], [100], [100], [100], [100], [100], [100]], dtype=np.float64)
55 expectedPoint = np.array([[11], [ 1], [ 2], [ 3], [ 4], [ 5], [ 6], [ 7], [ 8], [ 9], [ 10]], dtype=np.float64)
56 
57 if __name__ == "__main__":
58 
59  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
60  dataSource = FileDataSource(datasetFileName,
61  DataSourceIface.notAllocateNumericTable,
62  DataSourceIface.doDictionaryFromContext)
63 
64  # Create Numeric Tables for data and values for dependent variable
65  data = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
66  dependentVariables = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
67  mergedData = MergedNumericTable(data, dependentVariables)
68 
69  # Retrieve the data from the input file
70  dataSource.loadDataBlock(mergedData)
71 
72  mseObjectiveFunction = optimization_solver.mse.Batch(data.getNumberOfRows())
73  mseObjectiveFunction.input.set(optimization_solver.mse.data, data)
74  mseObjectiveFunction.input.set(optimization_solver.mse.dependentVariables, dependentVariables)
75 
76  # Create objects to compute the lbfgs result using the default method
77  lbfgsAlgorithm = optimization_solver.lbfgs.Batch(mseObjectiveFunction)
78  lbfgsAlgorithm.parameter.nIterations = halfNIterations
79  lbfgsAlgorithm.parameter.stepLengthSequence = HomogenNumericTable(1, 1, NumericTableIface.doAllocate, stepLength)
80  lbfgsAlgorithm.parameter.optionalResultRequired = True
81 
82  # Set input objects for LBFGS algorithm
83  lbfgsAlgorithm.input.setInput(optimization_solver.iterative_solver.inputArgument, HomogenNumericTable(initialPoint))
84 
85  # Compute the lbfgs result
86  # Result class from daal.algorithms.optimization_solver.iterative_solver
87  res = lbfgsAlgorithm.compute()
88 
89  # Print computed the lbfgs result
90  printNumericTable(res.getResult(optimization_solver.iterative_solver.minimum), "Resulting coefficients after first compute():")
91  printNumericTable(res.getResult(optimization_solver.iterative_solver.nIterations), "Number of iterations performed:")
92 
93  # Set optional result as an optional input
94  lbfgsAlgorithm.input.setInput(optimization_solver.iterative_solver.inputArgument, res.getResult(optimization_solver.iterative_solver.minimum))
95  lbfgsAlgorithm.input.setInput(optimization_solver.iterative_solver.optionalArgument, res.getResult(optimization_solver.iterative_solver.optionalResult))
96 
97  # Print computed the lbfgs result
98  res = lbfgsAlgorithm.compute()
99 
100  expectedCoefficients = HomogenNumericTable(expectedPoint)
101 
102  # Print computed the Adaptive gradient descent result
103  printNumericTable(expectedCoefficients, "Expected coefficients:")
104  printNumericTable(res.getResult(optimization_solver.iterative_solver.minimum), "Resulting coefficients after second compute():")
105  printNumericTable(res.getResult(optimization_solver.iterative_solver.nIterations), "Number of iterations performed:")

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