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

impl_als_csr_batch.py

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

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