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.
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
36 from daal.data_management
import (
37 DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
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
45 datasetFileName = os.path.join(
'..',
'data',
'batch',
'mse.csv')
50 accuracyThreshold = 0.0000001
52 initialPoint = np.array([[8], [2], [1], [4]], dtype=np.float64)
54 if __name__ ==
"__main__":
57 dataSource = FileDataSource(datasetFileName,
58 DataSourceIface.notAllocateNumericTable,
59 DataSourceIface.doDictionaryFromContext)
62 data = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
63 dependentVariables = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
64 mergedData = MergedNumericTable(data, dependentVariables)
67 dataSource.loadDataBlock(mergedData)
69 nVectors = data.getNumberOfRows()
71 mseObjectiveFunction = optimization_solver.mse.Batch(nVectors)
72 mseObjectiveFunction.input.set(optimization_solver.mse.data, data)
73 mseObjectiveFunction.input.set(optimization_solver.mse.dependentVariables, dependentVariables)
76 sgdAlgorithm = optimization_solver.sgd.Batch(mseObjectiveFunction)
79 sgdAlgorithm.input.setInput(optimization_solver.iterative_solver.inputArgument, HomogenNumericTable(initialPoint))
80 sgdAlgorithm.parameter.learningRateSequence = HomogenNumericTable(1, 1, NumericTableIface.doAllocate, learningRate)
81 sgdAlgorithm.parameter.nIterations = nIterations
82 sgdAlgorithm.parameter.accuracyThreshold = accuracyThreshold
86 res = sgdAlgorithm.compute()
89 printNumericTable(res.getResult(optimization_solver.iterative_solver.minimum),
"Minimum:")
90 printNumericTable(res.getResult(optimization_solver.iterative_solver.nIterations),
"Number of iterations performed:")