zoukankan      html  css  js  c++  java
  • TensorFlow-cpu优化及numpy优化

    1,TensorFlow-cpu优化

    当你使用cpu版TensorFlow时(比如pip安装),你可能会遇到警告,说你cpu支持AVX/AVX2指令集,那么在以下网址下载对应版本。

    https://github.com/fo40225/tensorflow-windows-wheel

    具体使用github上有说明。

    根据测试,安装AVX指令集后相应数学计算(矩阵乘法、分解等)速度是原来的3倍左右

    2,numpy优化

    一般现在的numpy默认都是支持openblas的,但是我发现支持mkl的更快。下载地址

    https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy

    查看numpy支持的优化:np.__config__.show()

    以下附上测试代码及结果,你可以在自己电脑上测试。

    '''
    default numpy(openblas):
    ---------
    Dotted two 4096x4096 matrices in 1.99 s. Dotted two vectors of length 524288 in 0.40 ms. SVD of a 2048x1024 matrix in 1.75 s. Cholesky decomposition of a 2048x2048 matrix in 0.21 s. Eigendecomposition of a 2048x2048 matrix in 10.31 s. ------------------------------------------------------
    numpy+mkl:
    ---------- Dotted two 4096x4096 matrices in 1.56 s. Dotted two vectors of length 524288 in 0.33 ms. SVD of a 2048x1024 matrix in 1.07 s. Cholesky decomposition of a 2048x2048 matrix in 0.24 s. Eigendecomposition of a 2048x2048 matrix in 6.94 s.
    ''' import numpy as np from time import time # Let's take the randomness out of random numbers (for reproducibility) np.random.seed(0) size = 4096 A, B = np.random.random((size, size)), np.random.random((size, size)) C, D = np.random.random((size * 128, )), np.random.random((size * 128, )) E = np.random.random((int(size / 2), int(size / 4))) F = np.random.random((int(size / 2), int(size / 2))) F = np.dot(F, F.T) G = np.random.random((int(size / 2), int(size / 2))) # Matrix multiplication N = 20 t = time() for i in range(N): np.dot(A, B) delta = time() - t print('Dotted two %dx%d matrices in %0.2f s.' % (size, size, delta / N)) del A, B # Vector multiplication N = 5000 t = time() for i in range(N): np.dot(C, D) delta = time() - t print('Dotted two vectors of length %d in %0.2f ms.' % (size * 128, 1e3 * delta / N)) del C, D # Singular Value Decomposition (SVD) N = 3 t = time() for i in range(N): np.linalg.svd(E, full_matrices=False) delta = time() - t print("SVD of a %dx%d matrix in %0.2f s." % (size / 2, size / 4, delta / N)) del E # Cholesky Decomposition N = 3 t = time() for i in range(N): np.linalg.cholesky(F) delta = time() - t print("Cholesky decomposition of a %dx%d matrix in %0.2f s." % (size / 2, size / 2, delta / N)) # Eigendecomposition t = time() for i in range(N): np.linalg.eig(G) delta = time() - t print("Eigendecomposition of a %dx%d matrix in %0.2f s." % (size / 2, size / 2, delta / N))
  • 相关阅读:
    数字签名与HTTPS详解
    利用策略模式优化过多 if else 代码
    Redis 的事务到底是不是原子性的
    Spring Boot项目的接口防刷
    深入分析 ThreadLocal
    什么是四层和七层负载均衡?他们之间的区别是什么?
    MyEclipse或Eclipse中project的导入和导出
    org.hibernate.exception.ConstraintViolationException: could not insert:
    C++ STL vector(向量容器)的使用(附完整程序代码)
    Swift2.0语言教程之函数嵌套调用形式
  • 原文地址:https://www.cnblogs.com/lunge-blog/p/11904824.html
Copyright © 2011-2022 走看看