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

impl_als_dense_batch.py

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

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