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

sgd_moment_opt_res_dense_batch.py

1 # file: sgd_moment_opt_res_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 SGD 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 nFeatures = 3
50 accuracyThreshold = 0.0000001
51 halfNIterations = 200
52 nIterations = halfNIterations * 2
53 batchSize = 4
54 learningRate = 0.5
55 
56 startPoint = np.array([[8], [2], [1], [4]], dtype=np.float64)
57 
58 if __name__ == "__main__":
59 
60  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
61  dataSource = FileDataSource(datasetFileName,
62  DataSourceIface.notAllocateNumericTable,
63  DataSourceIface.doDictionaryFromContext)
64 
65  # Create Numeric Tables for data and values for dependent variable
66  data = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
67  dependentVariables = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
68  mergedData = MergedNumericTable(data, dependentVariables)
69 
70  # Retrieve the data from the input file
71  dataSource.loadDataBlock(mergedData)
72 
73  nVectors = data.getNumberOfRows()
74 
75  mseObjectiveFunction = optimization_solver.mse.Batch(nVectors)
76  mseObjectiveFunction.input.set(optimization_solver.mse.data, data)
77  mseObjectiveFunction.input.set(optimization_solver.mse.dependentVariables, dependentVariables)
78 
79  # Create objects to compute the SGD result using the default method
80  sgdAlgorithm = optimization_solver.sgd.Batch(mseObjectiveFunction, method=optimization_solver.sgd.momentum)
81 
82  # Set input objects for the the SGD algorithm
83  sgdAlgorithm.input.setInput(optimization_solver.iterative_solver.inputArgument, HomogenNumericTable(startPoint))
84  sgdAlgorithm.parameter.learningRate = HomogenNumericTable(1, 1, NumericTableIface.doAllocate, learningRate)
85  sgdAlgorithm.parameter.nIterations = halfNIterations
86  sgdAlgorithm.parameter.accuracyThreshold = accuracyThreshold
87  sgdAlgorithm.parameter.batchSize = batchSize
88  sgdAlgorithm.parameter.optionalResultRequired = True
89 
90  # Compute the SGD result
91  # Result class from daal.algorithms.optimization_solver.iterative_solver
92  res = sgdAlgorithm.compute()
93 
94  # Print computed the SGD result
95  printNumericTable(res.getResult(optimization_solver.iterative_solver.minimum), "Minimum after first compute():")
96  printNumericTable(res.getResult(optimization_solver.iterative_solver.nIterations), "Number of iterations performed:")
97 
98  sgdAlgorithm.input.setInput(optimization_solver.iterative_solver.inputArgument, res.getResult(optimization_solver.iterative_solver.minimum))
99  sgdAlgorithm.input.setInput(optimization_solver.iterative_solver.optionalArgument, res.getResult(optimization_solver.iterative_solver.optionalResult))
100 
101  res = sgdAlgorithm.compute()
102 
103  printNumericTable(res.getResult(optimization_solver.iterative_solver.minimum), "Minimum after second compute():")
104  printNumericTable(res.getResult(optimization_solver.iterative_solver.nIterations), "Number of iterations performed:")

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