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

sgd_moment_opt_res_dense_batch.py

1 # file: sgd_moment_opt_res_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 SGD algorithm
19 # !*****************************************************************************
20 
21 #
22 
23 
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.mse
33 import daal.algorithms.optimization_solver.sgd
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', 'mse.csv')
46 
47 nFeatures = 3
48 accuracyThreshold = 0.0000001
49 halfNIterations = 200
50 nIterations = halfNIterations * 2
51 batchSize = 4
52 learningRate = 0.5
53 
54 startPoint = 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 SGD result using the default method
78  sgdAlgorithm = optimization_solver.sgd.Batch(mseObjectiveFunction, method=optimization_solver.sgd.momentum)
79 
80  # Set input objects for the the SGD algorithm
81  sgdAlgorithm.input.setInput(optimization_solver.iterative_solver.inputArgument, HomogenNumericTable(startPoint))
82  sgdAlgorithm.parameter.learningRate = HomogenNumericTable(1, 1, NumericTableIface.doAllocate, learningRate)
83  sgdAlgorithm.parameter.nIterations = halfNIterations
84  sgdAlgorithm.parameter.accuracyThreshold = accuracyThreshold
85  sgdAlgorithm.parameter.batchSize = batchSize
86  sgdAlgorithm.parameter.optionalResultRequired = True
87 
88  # Compute the SGD result
89  # Result class from daal.algorithms.optimization_solver.iterative_solver
90  res = sgdAlgorithm.compute()
91 
92  # Print computed the SGD result
93  printNumericTable(res.getResult(optimization_solver.iterative_solver.minimum), "Minimum after first compute():")
94  printNumericTable(res.getResult(optimization_solver.iterative_solver.nIterations), "Number of iterations performed:")
95 
96  sgdAlgorithm.input.setInput(optimization_solver.iterative_solver.inputArgument, res.getResult(optimization_solver.iterative_solver.minimum))
97  sgdAlgorithm.input.setInput(optimization_solver.iterative_solver.optionalArgument, res.getResult(optimization_solver.iterative_solver.optionalResult))
98 
99  res = sgdAlgorithm.compute()
100 
101  printNumericTable(res.getResult(optimization_solver.iterative_solver.minimum), "Minimum after second compute():")
102  printNumericTable(res.getResult(optimization_solver.iterative_solver.nIterations), "Number of iterations performed:")

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