170622 high perfermance computing: no_loop vs two loops for calculating distance matrix
来源:互联网 发布:航天证券交易软件 编辑:程序博客网 时间:2024/05/01 12:11
- Here we use a no loop method instead of two loops method for calculating distance matrix.
- And we find that the improved method is about 11 times faster than the traditional way.
This is our running environment and results:
This is our python code:
#!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Thu Jun 22 09:22:52 2017@author: brucelau"""import numpy as npimport matplotlib.pyplot as pltimport datetime# define no loop functionsdef no_loops(M,N): Xtr_square = np.sum(N**2, axis=1).reshape([1, N.shape[0]]) Xte_square = np.sum(M**2, axis=1).reshape([M.shape[0], 1]) Xtr_dot_Xte = M.dot(N.T) dists = (Xtr_square+Xte_square-2*Xtr_dot_Xte)**.5 return dists# define two loop functionsdef two_loops(M,N): r1 = M.shape[0] r2 = N.shape[0] DM = np.ones((r1,r2)) for i in range(r1): for j in range(r2): vec1 = M[i,:] vec2 = N[j,:] DM[i,j] = np.linalg.norm(vec1 - vec2) return DM# generate dataM_test = np.random.randint(1,11,size = (600,784))N_test = np.random.randint(1,11,size = (100,784))# data visualizationplt.matshow(M_test)plt.matshow(N_test)# time compared1 = datetime.datetime.now()dists_1 = no_loops(M_test,N_test)d2 = datetime.datetime.now()dists_2 =two_loops(M_test,N_test)d3 =datetime.datetime.now()t1=d2-d1t2=d3-d2print('#########################################################')print('Time for calculating no__loop is ',t1)print('Time for calculating twoloops is ',t2)times = t2/t1print('no loop function is about %d times faster than two loops function'%times)# error calculateprint('The error is ',np.sum(dists_1-dists_2),'between two methods')
阅读全文
0 0
- 170622 high perfermance computing: no_loop vs two loops for calculating distance matrix
- High Performance Computing Training
- High Performance Computing Training
- Towards a High-Level Trusted Computing API for Android Software Stack
- ArrayFire:高性能并行计算软件库 ArrayFire: a high performance software library for parallel computing
- Hard Computing VS Soft Computing
- Online Algorithms for calculating variance
- Embarrassingly parallel for loops
- 4.4 - For Loops
- JS (Coursera): For Loops
- Grid Computing: The New Frontier of High Performance Computing
- 高性能计算(High Performance Computing)
- HPCC(High Performance Computing Challenge)的输出
- vmd rmsd distance matrix
- Grid Computing For Electromagnetics
- Architecting for Green Computing
- General formula for calculating memory usage
- Perfermance Test
- CACD2000人脸数据整理
- 异常信息:spring mvc org.springframework.beans.NullValueInNestedPathException错误
- ECMAScript 2015官方文档翻译(二)
- git 导出多个版本的文件,用于更新
- 删除链表中的重复元素
- 170622 high perfermance computing: no_loop vs two loops for calculating distance matrix
- 关于web.XML文件的welcome-file设置成 .do/.action问题
- Matlab 图像处理总结
- The last packet successfully received from the server was 30,516,920 milliseconds ago.
- dySE:一个 Java 搜索引擎的实现,第 3 部分 查询服务
- ExpandableTextView——一个可折叠的Textview
- 日期、字符串、时间戳之间的互相转换
- python for循环和range内置函数
- 我是如何入门、成长并进阶为数据分析师的?