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

lbfgs_dense_batch.py

1 # file: lbfgs_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 # ! Content:
19 # ! Python example of the limited memory Broyden-Fletcher-Goldfarb-Shanno
20 # ! 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 nIterations = 1000
51 stepLength = 1.0e-4
52 
53 initialPoint = np.array([[100], [100], [100], [100], [100], [100], [100], [100], [100], [100], [100]], dtype=np.float64)
54 expectedPoint = np.array([[11], [ 1], [ 2], [ 3], [ 4], [ 5], [ 6], [ 7], [ 8], [ 9], [ 10]], dtype=np.float64)
55 
56 if __name__ == "__main__":
57 
58  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
59  dataSource = FileDataSource(datasetFileName,
60  DataSourceIface.notAllocateNumericTable,
61  DataSourceIface.doDictionaryFromContext)
62 
63  # Create Numeric Tables for input data and dependent variables
64  data = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
65  dependentVariables = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
66  mergedData = MergedNumericTable(data, dependentVariables)
67 
68  # Retrieve the data from input file
69  dataSource.loadDataBlock(mergedData)
70 
71  mseObjectiveFunction = optimization_solver.mse.Batch(data.getNumberOfRows())
72  mseObjectiveFunction.input.set(optimization_solver.mse.data, data)
73  mseObjectiveFunction.input.set(optimization_solver.mse.dependentVariables, dependentVariables)
74 
75  # Create objects to compute LBFGS result using the default method
76  algorithm = optimization_solver.lbfgs.Batch(mseObjectiveFunction)
77  algorithm.parameter.nIterations = nIterations
78  algorithm.parameter.stepLengthSequence = HomogenNumericTable(1, 1, NumericTableIface.doAllocate, stepLength)
79 
80  # Set input objects for LBFGS algorithm
81  algorithm.input.setInput(optimization_solver.iterative_solver.inputArgument, HomogenNumericTable(initialPoint))
82 
83  # Compute LBFGS result
84  # Result class from daal.algorithms.optimization_solver.iterative_solver
85  res = algorithm.compute()
86 
87  expectedCoefficients = HomogenNumericTable(expectedPoint)
88 
89  # Print computed LBFGS results
90  printNumericTable(expectedCoefficients, "Expected coefficients:")
91  printNumericTable(res.getResult(optimization_solver.iterative_solver.minimum), "Resulting coefficients:")
92  printNumericTable(res.getResult(optimization_solver.iterative_solver.nIterations), "Number of iterations performed:")

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