测试代码如下:


import sys


import numpy as np


import tensorflow as tf


from datetime import datetime


device_name=sys.argv[1] # Choose device from cmd line. Options: gpu or cpu


shape=(int(sys.argv[2]), int(sys.argv[2]))


if device_name=="gpu":


device_name="/gpu:0"


else:


device_name="/cpu:0"


with tf.device(device_name):


random_matrix=tf.random_uniform(shape=shape, minval=0, maxval=1)


dot_operation=tf.matmul(random_matrix, tf.transpose(random_matrix))


sum_operation=tfuce_sum(dot_operation)


startTime=datetime.now()


with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as session:


result=session(sum_operation)


print(result)


# It can be hard to see the results on the terminal with lots of output -- add some newlines to improve readability.


print("


" * 5)


print("Shape:", shape, "Device:", device_name)


print("Time taken:", datetime.now() - startTime)


print("


" * 5)


详细日志:


python .\demo01.py gpu 1500


2022-07-29 23:45:53.521707: I T:\src\githubensorflowensorflow\core\platform\cpu_feature_guard:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2


2022-07-29 23:45:54.093709: I T:\src\githubensorflowensorflow\core\common_runtime\gpu\gpu_device:1392] Found device 0 with properties:


name: GeForce 940MX major: 5 minor: 0 memoryClockRate(GHz): 1.189


pciBusID: 0000:02:00.0


totalMemory: 2.00GiB freeMemory: 1.66GiB


2022-07-29 23:45:54.109284: I T:\src\githubensorflowensorflow\core\common_runtime\gpu\gpu_device:1471] Adding visible gpu devices: 0


2022-07-29 23:45:55.044900: I T:\src\githubensorflowensorflow\www.tisheng8.com\core\common_runtime\gpu\gpu_device:952] Device interconnect StreamExecutor with strength 1 edge matrix:


2022-07-29 23:45:55.055221: I T:\src\githubensorflowensorflow\core\common_runtime\gpu\gpu_device:958] 0


2022-07-29 23:45:55.061725: I T:\src\githubensorflowensorflow\core\common_runtime\gpu\gpu_device:971] 0: N


2022-07-29 23:45:55.070524: I T:\src\githubensorflowensorflow\core\common_runtime\gpu\gpu_device:1084] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1422 MB memory) -> physical GPU (device: 0, name: GeForce 940MX, pci bus id: 0000:02:00.0, compute capability: 5.0)


Device mapping:


/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce 940MX, pci bus id: 0000:02:00.0, compute capability: 5.0


2022-07-29 23:45:55.217599: I T:\src\githubensorflowensorflow\core\common_runtime\direct_session:288] Device mapping:


/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce 940MX, pci bus id: 0000:02:00.0, compute capability: 5.0


random_uniform/RandomUniform: (RandomUniform): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.240213: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] random_uniform/RandomUniform: (RandomUniform)/job:localhost/replica:0/task:0/device:GPU:0


random_uniform/sub: (Sub): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.253903: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] random_uniform/sub: (Sub)/job:localhost/replica:0/task:0/device:GPU:0


random_uniform/mul: (Mul): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.263435: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] random_uniform/mul: (Mul)/job:localhost/replica:0/task:0/device:GPU:0


random_uniform: (Add): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.277615: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] random_uniform: (Add)/job:localhost/replica:0/task:0/device:GPU:0


transpose/Rank: (Rank): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.290588: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] transpose/Rank: (Rank)/job:localhost/replica:0/task:0/device:GPU:0


transpose/sub: (Sub): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.302476: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] transpose/sub: (Sub)/job:localhost/replica:0/task:0/device:GPU:0


transpose/Range: (Range): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.312525: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] transpose/Range: (Range)/job:localhost/replica:0/task:0/device:GPU:0


transpose/sub_1: (Sub): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.325154: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] transpose/sub_1: (Sub)/job:localhost/replica:0/task:0/device:GPU:0


transpose: (Transpose): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.338301: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] transpose: (Transpose)/job:localhost/replica:0/task:0/device:GPU:0


MatMul: (MatMul): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.349577: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] MatMul: (MatMul)/job:localhost/replica:0/task:0/device:GPU:0


Sum: (Sum): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.359293: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] Sum: (Sum)/job:localhost/replica:0/task:0/device:GPU:0


random_uniform/shape: (Const): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.372087: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] random_uniform/shape: (Const)/job:localhost/replica:0/task:0/device:GPU:0


random_uniform/min: (Const): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.386133: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] random_uniform/min: (Const)/job:localhost/replica:0/task:0/device:GPU:0


random_uniform/max: (Const): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.396248: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] random_uniform/max: (Const)/job:localhost/replica:0/task:0/device:GPU:0


transpose/sub/y: (Const): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.409642: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] transpose/sub/y: (Const)/job:localhost/replica:0/task:0/device:GPU:0


transpose/Range/start: (Const): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.423110: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] transpose/Range/start: (Const)/job:localhost/replica:0/task:0/device:GPU:0


transpose/Range/delta: (Const): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.435466: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] transpose/Range/delta: (Const)/job:localhost/replica:0/task:0/device:GPU:0


Const: (Const): /job:localhost/replica:0/task:0/device:GPU:0


2022-07-29 23:45:55.447107: I T:\src\githubensorflowensorflow\core\common_runtime\placer:886] Const: (Const)/job:localhost/replica:0/task:0/device:GPU:0


842822460.0


cpu 花了3分钟:


Shape: (1500, 1500) Device: /cpu:0


Time taken: 0:03:09.855242


gup 花了4秒钟:


Shape: (1500, 1500) Device: /gpu:0


Time taken: 0:00:04.823442


有个警告:


Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2


这个是对cpu的优化,这边用gpu 进行计算,可以忽略了。


使用gpu还是不错的。


速度超级快,4 秒就行,要是cpu的还要跑个3 分钟。


同样的安装cuda,cudnn 在 linux上面也是类似的。


更多相关文章

  1. Android横屏竖屏切换的问题
  2. android 解析json数据格式
  3. 利用Handler来更新android的UI
  4. Android(安卓)API 28 访问服务器失败 提示CLEARTEXT
  5. Android(安卓)SDK安装失败处理办法
  6. Android中替换ExpandableListView控件前面的箭头图标
  7. Android属性之android:priority
  8. Android(安卓)SDK安装失败处理办法
  9. Android(安卓)OpenGL学习笔记(二)之----三角形的绘制.

随机推荐

  1. Android 5.0以上Button,ImageView自带阴影
  2. Android应用开发相关下载资源(2016/07/24
  3. 一:android开发简介
  4. Android通过xml给控件添加边框
  5. activity的android:name 设置问题
  6. Android中HTTP请求出现“Received authen
  7. 微信支付接口 For Android(安卓)上手指南
  8. android 注解简介三: 自定义注解实现视图
  9. Android ProgressDialog 最佳处理方式
  10. Android(安卓)dumpsys命令的使用