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

sgd_log_loss_dense_batch.py

1 # file: sgd_log_loss_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 Stochastic gradient descent algorithm with
19 # ! logistic loss objective function
20 # !*****************************************************************************
21 
22 #
23 ## <a name="DAAL-EXAMPLE-PY-SGD_LOG_LOSS_BATCH"></a>
24 ## \example sgd_log_loss_dense_batch.py
25 #
26 
27 import os
28 import sys
29 
30 import numpy as np
31 
32 import daal.algorithms.optimization_solver as optimization_solver
33 import daal.algorithms.optimization_solver.logistic_loss
34 import daal.algorithms.optimization_solver.sgd
35 import daal.algorithms.optimization_solver.iterative_solver
36 
37 from daal.data_management import (
38  DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
39 )
40 
41 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
42 if utils_folder not in sys.path:
43  sys.path.insert(0, utils_folder)
44 from utils import printNumericTable
45 
46 datasetFileName = os.path.join('..', 'data', 'batch', 'custom.csv')
47 
48 nIterations = 1000
49 nFeatures = 4
50 learningRate = 0.01
51 accuracyThreshold = 0.02
52 
53 initialPoint = np.array([[1], [1], [1], [1], [1]], 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  nVectors = data.getNumberOfRows()
71 
72  logistic_lossObjectiveFunction = optimization_solver.logistic_loss.Batch(nVectors)
73  logistic_lossObjectiveFunction.input.set(optimization_solver.logistic_loss.data, data)
74  logistic_lossObjectiveFunction.input.set(optimization_solver.logistic_loss.dependentVariables, dependentVariables)
75 
76  # Create objects to compute the Stochastic gradient descent result using the default method
77  sgdAlgorithm = optimization_solver.sgd.Batch(logistic_lossObjectiveFunction)
78 
79  # Set input objects for the the Stochastic gradient descent algorithm
80  sgdAlgorithm.input.setInput(optimization_solver.iterative_solver.inputArgument, HomogenNumericTable(initialPoint))
81  sgdAlgorithm.parameter.learningRateSequence = HomogenNumericTable(1, 1, NumericTableIface.doAllocate, learningRate)
82  sgdAlgorithm.parameter.nIterations = nIterations
83  sgdAlgorithm.parameter.accuracyThreshold = accuracyThreshold
84 
85  # Compute the Stochastic gradient descent result
86  # Result class from daal.algorithms.optimization_solver.iterative_solver
87  res = sgdAlgorithm.compute()
88 
89  # Print computed the Stochastic gradient descent result
90  printNumericTable(res.getResult(optimization_solver.iterative_solver.minimum), "Minimum:")
91  printNumericTable(res.getResult(optimization_solver.iterative_solver.nIterations), "Number of iterations performed:")

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