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

lbfgs_cr_entr_loss_dense_batch.py

1 # file: lbfgs_cr_entr_loss_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 with cross entropy loss function
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.cross_entropy_loss
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', 'logreg_train.csv')
48 
49 nFeatures = 6
50 nClasses = 5
51 nIterations = 1000
52 stepLength = 1.0e-4
53 
54 if __name__ == "__main__":
55 
56  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
57  dataSource = FileDataSource(datasetFileName,
58  DataSourceIface.notAllocateNumericTable,
59  DataSourceIface.doDictionaryFromContext)
60 
61  # Create Numeric Tables for input data and dependent variables
62  data = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
63  dependentVariables = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
64  mergedData = MergedNumericTable(data, dependentVariables)
65 
66  # Retrieve the data from input file
67  dataSource.loadDataBlock(mergedData)
68 
69  func = optimization_solver.cross_entropy_loss.Batch(nClasses, data.getNumberOfRows())
70  func.input.set(optimization_solver.cross_entropy_loss.data, data)
71  func.input.set(optimization_solver.cross_entropy_loss.dependentVariables, dependentVariables)
72 
73  # Create objects to compute LBFGS result using the default method
74  algorithm = optimization_solver.lbfgs.Batch(func)
75  algorithm.parameter.nIterations = nIterations
76  algorithm.parameter.stepLengthSequence = HomogenNumericTable(1, 1, NumericTableIface.doAllocate, stepLength)
77 
78  # Set input objects for LBFGS algorithm
79  nParameters = nClasses * (nFeatures + 1)
80  initialPoint = np.full((nParameters, 1), 0.001, dtype=np.float64)
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  expectedPoint = np.array([[-2.277], [2.836], [14.985], [0.511], [7.510], [-2.831], [-5.814], [-0.033], [13.227], [-24.447], [3.730],
88  [10.394], [-10.461], [-0.766], [0.077], [1.558], [-1.133], [2.884], [-3.825], [7.699], [2.421], [-0.135], [-6.996], [1.785], [-2.294], [-9.819], [1.692],
89  [-0.725], [0.069], [-8.41], [1.458], [-3.306], [-4.719], [5.507], [-1.642]], dtype=np.float64)
90  expectedCoefficients = HomogenNumericTable(expectedPoint)
91 
92  # Print computed LBFGS results
93  printNumericTable(expectedCoefficients, "Expected coefficients:")
94  printNumericTable(res.getResult(optimization_solver.iterative_solver.minimum), "Resulting coefficients:")
95  printNumericTable(res.getResult(optimization_solver.iterative_solver.nIterations), "Number of iterations performed:")

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