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

sgd_dense_batch.py

1 # file: sgd_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 #
19 # ! Content:
20 # ! Python example of the Stochastic gradient descent 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.sgd
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', 'mse.csv')
48 
49 nIterations = 1000
50 nFeatures = 3
51 learningRate = 1.0
52 accuracyThreshold = 0.0000001
53 
54 initialPoint = np.array([[8], [2], [1], [4]], 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 data and values for dependent variable
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 the input file
69  dataSource.loadDataBlock(mergedData)
70 
71  nVectors = data.getNumberOfRows()
72 
73  mseObjectiveFunction = optimization_solver.mse.Batch(nVectors)
74  mseObjectiveFunction.input.set(optimization_solver.mse.data, data)
75  mseObjectiveFunction.input.set(optimization_solver.mse.dependentVariables, dependentVariables)
76 
77  # Create objects to compute the Stochastic gradient descent result using the default method
78  sgdAlgorithm = optimization_solver.sgd.Batch(mseObjectiveFunction)
79 
80  # Set input objects for the the Stochastic gradient descent algorithm
81  sgdAlgorithm.input.setInput(optimization_solver.iterative_solver.inputArgument, HomogenNumericTable(initialPoint))
82  sgdAlgorithm.parameter.learningRateSequence = HomogenNumericTable(1, 1, NumericTableIface.doAllocate, learningRate)
83  sgdAlgorithm.parameter.nIterations = nIterations
84  sgdAlgorithm.parameter.accuracyThreshold = accuracyThreshold
85 
86  # Compute the Stochastic gradient descent result
87  # Result class from daal.algorithms.optimization_solver.iterative_solver
88  res = sgdAlgorithm.compute()
89 
90  # Print computed the Stochastic gradient descent result
91  printNumericTable(res.getResult(optimization_solver.iterative_solver.minimum), "Minimum:")
92  printNumericTable(res.getResult(optimization_solver.iterative_solver.nIterations), "Number of iterations performed:")

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