Repository navigation
Description
I used numpy to write a program to calcuate the cosine of matrix, and built is as a flask service. I found the cpu sys time cos too much when requesting services continuously. who can tell me what is the problem?
np.show_config()
blas_mkl_info:
NOT AVAILABLE
blis_info:
NOT AVAILABLE
openblas_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/local/lib']
language = c
define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/local/lib']
language = c
define_macros = [('HAVE_CBLAS', None)]
lapack_mkl_info:
NOT AVAILABLE
openblas_lapack_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/local/lib']
language = c
define_macros = [('HAVE_CBLAS', None)]
lapack_opt_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/local/lib']
language = c
define_macros = [('HAVE_CBLAS', None)]
Reproducing code example:
class ClusterStreaming:
...
def cosine_distance(self, matrix1, matrix2):
matrix1_matrix2 = np.dot(matrix1, matrix2.transpose())
matrix1_norm = np.sqrt(np.multiply(matrix1, matrix1).sum(axis=1))
matrix1_norm = matrix1_norm[:, np.newaxis]
matrix2_norm = np.sqrt(np.multiply(matrix2, matrix2).sum(axis=1))
matrix2_norm = matrix2_norm[:, np.newaxis]
cosine_distance = np.divide(matrix1_matrix2, np.dot(matrix1_norm, matrix2_norm.transpose()))
return cosine_distance
def find_most_sim_cluster(self, news_vec, matrix, topK):
cos_distances = list(self.cosine_distance(np.array([news_vec]), matrix)[0])
max_topk_index = map(cos_distances.index, heapq.nlargest(topK, cos_distances))
max_topk_similar = []
for index in max_topk_index:
max_topk_similar.append(1 - cos_distances[index])
return max_topk_index, max_topk_similar
def flask_api(self, storage):
...
self.cosine_distance()
self.find_most_sim_cluster()
return resp
cluster_handler = ClusterStreaming()
cluster_handler.flask_api(..)
Activity
mattip commentedon Sep 19, 2019
This kind of question is not really an issue with NumPy, you may find a forum like StackOverflow more amenable to information about profiling or running code.
With that, you should have answers to questions like: how many cores are available, are you using a NumPy binary with any BLAS backend, what else is running on the target, what does profiling show?
seberg commentedon Sep 21, 2019
You may want to google for old issues, and things such as threadpoolctl and OMP_NUM_THREADS environment variable. There are open issues about this, so closing this one.
Micy1919 commentedon Sep 23, 2019
got it, thanks