-
Notifications
You must be signed in to change notification settings - Fork 40
Update: add gpu option #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2f2bde2
480069e
21b125a
cee4b84
e6a4bbd
57b35ba
e38bea7
58670c8
11348d0
c2804e4
220a5f6
34b5bc2
227902b
e1c80da
eb9f323
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,15 @@ | ||
| import pycuda.autoinit | ||
| import pycuda.gpuarray as gpuarray | ||
| import skcuda.linalg as linalg | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good if you can add a
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok I will write a .txt file like this and upload it. |
||
| import numpy as np | ||
| import math | ||
| import skcuda.misc as misc | ||
|
|
||
|
|
||
|
|
||
| class MF(): | ||
|
|
||
| def __init__(self, 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. | ||
|
|
@@ -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 | ||
| - use_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 'use_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.use_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.use_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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this part can be simplified to as follows, because this is not an actual script that can be executed, but just a sample of how to use the module:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you are right it is more clear like this, and also according to its significance it is better in the end of the arguments, as you proposed.