From 2f2bde2e75d0f635c3ae7e21650320498eee9363 Mon Sep 17 00:00:00 2001 From: Manos Gionanidis Date: Wed, 17 Apr 2019 09:48:33 +0300 Subject: [PATCH 01/14] Update mf.py --- mf.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/mf.py b/mf.py index bddd9ce..1829b24 100644 --- a/mf.py +++ b/mf.py @@ -1,9 +1,15 @@ +import pycuda.autoinit +import pycuda.gpuarray as gpuarray +import skcuda.linalg as linalg import numpy as np +import math +import skcuda.misc as misc + class MF(): - def __init__(self, R, K, alpha, beta, iterations): + def __init__(self, gpu, R, K, alpha, beta, iterations): """ Perform matrix factorization to predict empty entries in a matrix. @@ -13,8 +19,10 @@ def __init__(self, R, K, alpha, beta, iterations): - K (int) : number of latent dimensions - alpha (float) : learning rate - beta (float) : regularization parameter + - gpu (boolean) : declares the GPU implementation """ + self.gpu = gpu self.R = R self.num_users, self.num_items = R.shape self.K = K @@ -85,13 +93,47 @@ def sgd(self): def get_rating(self, i, j): """ - Get the predicted rating of user i and item j + Get the predicted rating of user i and item j, regarding the value of boolean variable 'gpu' we do our calculation accordingly """ - prediction = self.b + self.b_u[i] + self.b_i[j] + self.P[i, :].dot(self.Q[j, :].T) + + if(self.gpu): + + #initialization + linalg.init() + + #make the appropriate format + p_gpu = gpuarray.to_gpu(self.P) + q_gpu = gpuarray.to_gpu(self.Q) + + prediction = self.b + self.b_u[i] + self.b_i[j] + linalg.dot(p_gpu[i, :],q_gpu[j, :],transb='T') + + + else: + + prediction = self.b + self.b_u[i] + self.b_i[j] + self.P[i, :].dot(self.Q[j, :].T) + + return prediction def full_matrix(self): """ Computer the full matrix using the resultant biases, P and Q """ - return self.b + self.b_u[:,np.newaxis] + self.b_i[np.newaxis:,] + self.P.dot(self.Q.T) + + if(self.gpu): + + #initialization + linalg.init() + + #make the appropriate format + p_gpu = gpuarray.to_gpu(self.P) + q_gpu = gpuarray.to_gpu(self.Q) + + #we denote as transb='T' that we take the second argument, matrix q_gpu as transpose + fullMatrix = self.b + self.b_u[:,np.newaxis] + self.b_i[np.newaxis:,] + linalg.dot(p_gpu, q_gpu, transb='T').get() + + else: + + fullMatrix = self.b + self.b_u[:,np.newaxis] + self.b_i[np.newaxis:,] + self.P.dot(self.Q.T) + + return fullMatrix From 21b125ad4d3c5711829317a51905516678f08f45 Mon Sep 17 00:00:00 2001 From: Manos Gionanidis Date: Wed, 17 Apr 2019 09:54:49 +0300 Subject: [PATCH 02/14] Update README.md --- README.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2482ddb..c4c103d 100644 --- a/README.md +++ b/README.md @@ -18,11 +18,28 @@ R = np.array([ [0, 1, 5, 4], ]) + +#because there are many dot products to calculate during the procedure, GPU can be used for this task +while True: + + gpu_input = input('Do you want to run it to GPU, yy/nn: ') + + if(gpu_input=='yy' or gpu_input=='nn'): + + gpu = (gpu_input=='yy') + + if(gpu): print('GPU implementation \n\n') + + break + + + # Perform training and obtain the user and item matrices -mf = MF(R, K=2, alpha=0.1, beta=0.01, iterations=20) +mf = MF(gpu, R, K=2, alpha=0.1, beta=0.01, iterations=20) training_process = mf.train() print(mf.P) print(mf.Q) +print("GPU Implementation: ",gpu) print(mf.full_matrix()) # Prints the following: From cee4b8443db711b84a93ad261704454d37a213af Mon Sep 17 00:00:00 2001 From: Manos Gionanidis Date: Wed, 17 Apr 2019 10:01:39 +0300 Subject: [PATCH 03/14] Update README.md --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index c4c103d..9dc75f7 100644 --- a/README.md +++ b/README.md @@ -55,10 +55,42 @@ print(mf.full_matrix()) [ 0.18071811 -1.2672859 ] [-1.4211893 0.20465575]] + GPU Implementation: False + [[ 4.98407556 2.99856476 3.96309763 1.01351377] [ 3.99274702 2.27661831 3.20365416 1.0125506 ] [ 1.0064803 1.00498576 2.37696737 4.98530109] [ 1.00999456 0.59175173 2.58437035 3.99597255] [ 2.26471556 1.01985428 4.9871617 3.9942251 ]] + ''' + + + +# Perform training and obtain the user and item matrices, using GPU for the multiplications +mf = MF(gpu, R, K=2, alpha=0.1, beta=0.01, iterations=20) + + +# Prints the following: +''' + +[[-1.45063442 -0.61549057] + [-1.18644282 -0.25861855] + [ 0.98953876 0.41593886] + [ 0.83830212 -0.55288053] + [-0.50317654 1.14994057]] + +[[-1.37430118 0.245039 ] + [ 0.02231156 -1.12899481] + [-0.98101813 0.33079953] + [ 1.65238654 0.79608774]] + +GPU Implementation: True + +[[4.993465 2.98861347 5.3236645 1.01310464] + [3.99121154 1.86497915 4.45591865 1.00713051] + [1.01303675 0.99894327 2.39137093 4.98668646] + [1.01104568 2.11692377 2.24681459 3.99308259] + [4.1289681 1.02158625 4.98319103 3.98910547]] + ''' ``` From e6a4bbde862029bfce70a414a7cd6267cad56abb Mon Sep 17 00:00:00 2001 From: Manos Gionanidis Date: Fri, 19 Apr 2019 10:15:20 +0300 Subject: [PATCH 04/14] Update README.md --- README.md | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 9dc75f7..7fed3a3 100644 --- a/README.md +++ b/README.md @@ -20,22 +20,12 @@ R = np.array([ #because there are many dot products to calculate during the procedure, GPU can be used for this task -while True: - - gpu_input = input('Do you want to run it to GPU, yy/nn: ') - - if(gpu_input=='yy' or gpu_input=='nn'): - - gpu = (gpu_input=='yy') - - if(gpu): print('GPU implementation \n\n') - - break +#use_gpu = False/True, Set to True to use GPU in training and prediction # Perform training and obtain the user and item matrices -mf = MF(gpu, R, K=2, alpha=0.1, beta=0.01, iterations=20) +mf = MF(R, K=2, alpha=0.1, beta=0.01, iterations=20, use_gpu=False) training_process = mf.train() print(mf.P) print(mf.Q) @@ -55,8 +45,6 @@ print(mf.full_matrix()) [ 0.18071811 -1.2672859 ] [-1.4211893 0.20465575]] - GPU Implementation: False - [[ 4.98407556 2.99856476 3.96309763 1.01351377] [ 3.99274702 2.27661831 3.20365416 1.0125506 ] [ 1.0064803 1.00498576 2.37696737 4.98530109] @@ -67,7 +55,7 @@ print(mf.full_matrix()) # Perform training and obtain the user and item matrices, using GPU for the multiplications -mf = MF(gpu, R, K=2, alpha=0.1, beta=0.01, iterations=20) +mf = MF(R, K=2, alpha=0.1, beta=0.01, iterations=20, use_gpu=True) # Prints the following: @@ -83,8 +71,6 @@ mf = MF(gpu, R, K=2, alpha=0.1, beta=0.01, iterations=20) [ 0.02231156 -1.12899481] [-0.98101813 0.33079953] [ 1.65238654 0.79608774]] - -GPU Implementation: True [[4.993465 2.98861347 5.3236645 1.01310464] [3.99121154 1.86497915 4.45591865 1.00713051] From 57b35ba270fed90bac1ad838c28f5f24ad0b7c52 Mon Sep 17 00:00:00 2001 From: Manos Gionanidis Date: Fri, 19 Apr 2019 10:15:53 +0300 Subject: [PATCH 05/14] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 7fed3a3..7f5c5ef 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,6 @@ mf = MF(R, K=2, alpha=0.1, beta=0.01, iterations=20, use_gpu=False) training_process = mf.train() print(mf.P) print(mf.Q) -print("GPU Implementation: ",gpu) print(mf.full_matrix()) # Prints the following: From e38bea7cd35c36cdf7bb9ff03f90e593d1d8b519 Mon Sep 17 00:00:00 2001 From: Manos Gionanidis Date: Fri, 19 Apr 2019 10:19:43 +0300 Subject: [PATCH 06/14] Update mf.py --- mf.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mf.py b/mf.py index 1829b24..d3c7ad6 100644 --- a/mf.py +++ b/mf.py @@ -9,7 +9,7 @@ class MF(): - def __init__(self, gpu, R, K, alpha, beta, iterations): + def __init__(self, R, K, alpha, beta, iterations, use_gpu): """ Perform matrix factorization to predict empty entries in a matrix. @@ -19,7 +19,7 @@ def __init__(self, gpu, R, K, alpha, beta, iterations): - K (int) : number of latent dimensions - alpha (float) : learning rate - beta (float) : regularization parameter - - gpu (boolean) : declares the GPU implementation + - use_gpu (boolean) : declares the GPU implementation """ self.gpu = gpu @@ -96,7 +96,7 @@ def get_rating(self, i, j): Get the predicted rating of user i and item j, regarding the value of boolean variable 'gpu' we do our calculation accordingly """ - if(self.gpu): + if(self.use_gpu): #initialization linalg.init() @@ -120,7 +120,7 @@ def full_matrix(self): Computer the full matrix using the resultant biases, P and Q """ - if(self.gpu): + if(self.use_gpu): #initialization linalg.init() From 58670c8700db44047a3f3b5cc5a20cdd6c5a489c Mon Sep 17 00:00:00 2001 From: Manos Gionanidis Date: Fri, 19 Apr 2019 10:41:31 +0300 Subject: [PATCH 07/14] Update mf.py --- mf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mf.py b/mf.py index d3c7ad6..56bff67 100644 --- a/mf.py +++ b/mf.py @@ -93,7 +93,7 @@ def sgd(self): def get_rating(self, i, j): """ - Get the predicted rating of user i and item j, regarding the value of boolean variable 'gpu' we do our calculation accordingly + Get the predicted rating of user i and item j, regarding the value of boolean variable 'use_gpu' we do our calculation accordingly """ if(self.use_gpu): From 11348d09bc94749f1b855457653f223d3e91ecb4 Mon Sep 17 00:00:00 2001 From: Manos Gionanidis Date: Fri, 19 Apr 2019 10:57:39 +0300 Subject: [PATCH 08/14] Add files via upload --- requirements.txt | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c1d1299 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,53 @@ +Prerequisite libraries: + + + ----> math + + import math + + + ----> numpy + + pip: pip install numpy + + anaconda: conda install -c anaconda numpy + + + ----> scikit-cuda (skcuda) + + pip: pip install scikit-cuda + + anaconda: conda install -c lukepfister scikits.cuda + + Dependecies: + + - Python 2.7 or 3.4. + + - Setuptools 0.6c10 or later. + + - Mako 1.0.1 or later. + + - NumPy 1.2.0 or later. + + - PyCUDA 2016.1 or later (some parts of scikit-cuda might not work properly with earlier versions). + + - NVIDIA CUDA Toolkit 5.0 or later. + + + ----> pycuda + + pip: pip install pycuda + + anaconda: conda install -c lukepfister pycuda + + Dependecies: + + - Nvidia's CUDA toolkit. PyCUDA was developed against version 2.0 beta. It may work with other versions, too. + + - A C++ compiler, preferably a Version 4.x gcc. + + - A working Python installation, Version 2.4 or newer. + + additional information can be found: https://wiki.tiker.net/PyCuda/Installation + + From c2804e4738e613bd801618defc6be5bd13704fea Mon Sep 17 00:00:00 2001 From: Manos Gionanidis Date: Fri, 19 Apr 2019 10:57:56 +0300 Subject: [PATCH 09/14] Rename requirements.txt to requirements.md --- requirements.txt => requirements.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename requirements.txt => requirements.md (100%) diff --git a/requirements.txt b/requirements.md similarity index 100% rename from requirements.txt rename to requirements.md From 220a5f6c4e4a792e31073a3b4669b9a81b673713 Mon Sep 17 00:00:00 2001 From: Manos Gionanidis Date: Fri, 19 Apr 2019 10:58:19 +0300 Subject: [PATCH 10/14] Update requirements.md --- requirements.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/requirements.md b/requirements.md index c1d1299..c79c4c7 100644 --- a/requirements.md +++ b/requirements.md @@ -1,19 +1,19 @@ Prerequisite libraries: - ----> math + ----> **math** import math - ----> numpy + ----> **numpy** pip: pip install numpy anaconda: conda install -c anaconda numpy - ----> scikit-cuda (skcuda) + ----> **scikit-cuda (skcuda)** pip: pip install scikit-cuda @@ -34,7 +34,7 @@ Prerequisite libraries: - NVIDIA CUDA Toolkit 5.0 or later. - ----> pycuda + ----> **pycuda** pip: pip install pycuda From 34b5bc2a30864cfb3c8012abfe3eb47580a1f9c1 Mon Sep 17 00:00:00 2001 From: Manos Gionanidis Date: Fri, 19 Apr 2019 10:58:35 +0300 Subject: [PATCH 11/14] Update requirements.md --- requirements.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.md b/requirements.md index c79c4c7..c9a39d7 100644 --- a/requirements.md +++ b/requirements.md @@ -1,7 +1,7 @@ Prerequisite libraries: - ----> **math** + ----> ##math## import math From 227902bfce3037c91c8021e2caae81579a16c2cd Mon Sep 17 00:00:00 2001 From: Manos Gionanidis Date: Fri, 19 Apr 2019 10:59:13 +0300 Subject: [PATCH 12/14] Update requirements.md --- requirements.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/requirements.md b/requirements.md index c9a39d7..7b40f73 100644 --- a/requirements.md +++ b/requirements.md @@ -1,19 +1,19 @@ Prerequisite libraries: - ----> ##math## + ----> ## math import math - ----> **numpy** + ----> numpy pip: pip install numpy anaconda: conda install -c anaconda numpy - ----> **scikit-cuda (skcuda)** + ----> scikit-cuda (skcuda) pip: pip install scikit-cuda From e1c80da0ea61ed26adaca7b887cda48d818bba90 Mon Sep 17 00:00:00 2001 From: Manos Gionanidis Date: Fri, 19 Apr 2019 10:59:24 +0300 Subject: [PATCH 13/14] Update requirements.md --- requirements.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.md b/requirements.md index 7b40f73..fd7c316 100644 --- a/requirements.md +++ b/requirements.md @@ -1,7 +1,7 @@ Prerequisite libraries: - ----> ## math + ----> #math import math From eb9f323999a1a814f085ff034fab9801540895df Mon Sep 17 00:00:00 2001 From: Manos Gionanidis Date: Fri, 19 Apr 2019 10:59:36 +0300 Subject: [PATCH 14/14] Update requirements.md --- requirements.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements.md b/requirements.md index fd7c316..c1d1299 100644 --- a/requirements.md +++ b/requirements.md @@ -1,7 +1,7 @@ Prerequisite libraries: - ----> #math + ----> math import math @@ -34,7 +34,7 @@ Prerequisite libraries: - NVIDIA CUDA Toolkit 5.0 or later. - ----> **pycuda** + ----> pycuda pip: pip install pycuda