Python* API Reference for Intel® Data Analytics Acceleration Library 2018 Update 3

lbfgs_opt_res_dense_batch.py

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

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