Python* API Reference for Intel® Data Analytics Acceleration Library 2018 Update 3

impl_als_dense_batch.py

1 # file: impl_als_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 
18 
19 import os
20 import sys
21 
22 import daal.algorithms.implicit_als.training.init
23 import daal.algorithms.implicit_als.prediction.ratings
24 from daal.algorithms.implicit_als import training, prediction
25 from daal.data_management import FileDataSource, DataSourceIface
26 
27 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
28 if utils_folder not in sys.path:
29  sys.path.insert(0, utils_folder)
30 from utils import printNumericTable
31 
32 DAAL_PREFIX = os.path.join('..', 'data')
33 
34 # Input data set parameters
35 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'implicit_als_dense.csv')
36 
37 # Algorithm parameters
38 nFactors = 2
39 
40 dataTable = None
41 initialModel = None
42 trainingResult = None
43 
44 
45 def initializeModel():
46  global dataTable, initialModel
47 
48  # Read trainDatasetFileName from a file and create a numeric table to store the input data
49  dataSource = FileDataSource(
50  trainDatasetFileName, DataSourceIface.doAllocateNumericTable,
51  DataSourceIface.doDictionaryFromContext
52  )
53 
54  # Retrieve the input data
55  dataSource.loadDataBlock()
56 
57  dataTable = dataSource.getNumericTable()
58  # Create an algorithm object to initialize the implicit ALS model with the default method
59  initAlgorithm = training.init.Batch()
60  initAlgorithm.parameter.nFactors = nFactors
61 
62  # Pass a training data set and dependent values to the algorithm
63  initAlgorithm.input.set(training.init.data, dataTable)
64  res = initAlgorithm.compute()
65 
66  # Initialize the implicit ALS model
67  initialModel = res.get(training.init.model)
68 
69 
70 def trainModel():
71  global trainingResult
72 
73  # Create an algorithm object to train the implicit ALS model with the default method
74  algorithm = training.Batch()
75 
76  # Pass a training data set and dependent values to the algorithm
77  algorithm.input.setTable(training.data, dataTable)
78  algorithm.input.setModel(training.inputModel, initialModel)
79 
80  algorithm.parameter.nFactors = nFactors
81 
82  # Build the implicit ALS model and retrieve the algorithm results
83  trainingResult = algorithm.compute()
84 
85 
86 def testModel():
87 
88  # Create an algorithm object to predict recommendations of the implicit ALS model
89  algorithm = prediction.ratings.Batch()
90  algorithm.parameter.nFactors = nFactors
91 
92  algorithm.input.set(prediction.ratings.model, trainingResult.get(training.model))
93 
94  res = algorithm.compute()
95  predictedRatings = res.get(prediction.ratings.prediction)
96 
97  printNumericTable(predictedRatings, "Predicted ratings:")
98 
99 if __name__ == "__main__":
100 
101  initializeModel()
102  trainModel()
103  testModel()

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