Python* API Reference for Intel® Data Analytics Acceleration Library 2019 Update 4

lbfgs_cr_entr_loss_dense_batch.py

Deprecation Notice: With the introduction of daal4py, a package that supersedes PyDAAL, Intel is deprecating PyDAAL and will discontinue support starting with Intel® DAAL 2021 and Intel® Distribution for Python 2021. Until then Intel will continue to provide compatible pyDAAL pip and conda packages for newer releases of Intel DAAL and make it available in open source. However, Intel will not add the new features of Intel DAAL to pyDAAL. Intel recommends developers switch to and use daal4py.

Note: To find daal4py examples, refer to daal4py documentation or browse github repository.

1 # file: lbfgs_cr_entr_loss_dense_batch.py
2 #===============================================================================
3 # Copyright 2014-2019 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 # ! Content:
17 # ! Python example of the limited memory Broyden-Fletcher-Goldfarb-Shanno
18 # ! algorithm with cross entropy loss function
19 # !*****************************************************************************
20 
21 #
22 ## <a name="DAAL-EXAMPLE-PY-LBFGS_CR_ENTR_LOSS_BATCH"></a>
23 ## \example lbfgs_cr_entr_loss_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.cross_entropy_loss
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', 'logreg_train.csv')
46 
47 nFeatures = 6
48 nClasses = 5
49 nIterations = 1000
50 stepLength = 1.0e-4
51 
52 if __name__ == "__main__":
53 
54  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
55  dataSource = FileDataSource(datasetFileName,
56  DataSourceIface.notAllocateNumericTable,
57  DataSourceIface.doDictionaryFromContext)
58 
59  # Create Numeric Tables for input data and dependent variables
60  data = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
61  dependentVariables = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
62  mergedData = MergedNumericTable(data, dependentVariables)
63 
64  # Retrieve the data from input file
65  dataSource.loadDataBlock(mergedData)
66 
67  func = optimization_solver.cross_entropy_loss.Batch(nClasses, data.getNumberOfRows())
68  func.input.set(optimization_solver.cross_entropy_loss.data, data)
69  func.input.set(optimization_solver.cross_entropy_loss.dependentVariables, dependentVariables)
70 
71  # Create objects to compute LBFGS result using the default method
72  algorithm = optimization_solver.lbfgs.Batch(func)
73  algorithm.parameter.nIterations = nIterations
74  algorithm.parameter.stepLengthSequence = HomogenNumericTable(1, 1, NumericTableIface.doAllocate, stepLength)
75 
76  # Set input objects for LBFGS algorithm
77  nParameters = nClasses * (nFeatures + 1)
78  initialPoint = np.full((nParameters, 1), 0.001, dtype=np.float64)
79  algorithm.input.setInput(optimization_solver.iterative_solver.inputArgument, HomogenNumericTable(initialPoint))
80 
81  # Compute LBFGS result
82  # Result class from daal.algorithms.optimization_solver.iterative_solver
83  res = algorithm.compute()
84 
85  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],
86  [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],
87  [-0.725], [0.069], [-8.41], [1.458], [-3.306], [-4.719], [5.507], [-1.642]], dtype=np.float64)
88  expectedCoefficients = HomogenNumericTable(expectedPoint)
89 
90  # Print computed LBFGS results
91  printNumericTable(expectedCoefficients, "Expected coefficients:")
92  printNumericTable(res.getResult(optimization_solver.iterative_solver.minimum), "Resulting coefficients:")
93  printNumericTable(res.getResult(optimization_solver.iterative_solver.nIterations), "Number of iterations performed:")

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