摘要:

1.以动态图形式计算一个简单的加法

2.cpu和gpu计算力比较(包括如何指定cpu和gpu)

3.关于gpu版本的tensorflow安装问题,可以参考另一篇博文:https://www.cnblogs.com/liuhuacai/p/11684666.html

正文:

1.在tensorflow中计算3.+4.

##1.创建输入张量
a = tf.constant(2.)
b = tf.constant(4.)
##2.计算结果
print('a+b=',a+b)

输出:a+b= tf.Tensor(7.0, shape=(), dtype=float32)

总结:20版本在加法实现过程中简单了不少,所见即所得。(1.x的实现过程相对复杂)据说动态的实现也是后端转化成静态图实现的。

2.cpu和gpu计算力比较

说明:通过计算不同大小的矩阵乘法,获得计算时间。

  1.指定cpu或gpu通过 with tf.device('/cpu:0'):或 with tf.device('/gpu:0'):指定,在需要加速的操作前添加即可(此处生成随机               数和矩阵乘法都加速)

  2.统计计算时间的函数timeit.timeit需要导入import timeit【timeit.timeit(需计时的函数或语句,计算次数)】

  3.计算量的大小与cpu和gpu计算时间的关系,计算量通过改变矩阵大小实现

import tensorflow as tf
import timeit
以矩阵A[10,n]和矩阵B[n,10]的乘法运算(分别在cpu和gpu上运行)来测试,
'''
with tf.device('/cpu:0'): ##指定操作用cpu计算
cpu_a = tf.random.normal([10,n]) ##生成符合高斯分布的随机数矩阵,通过改变n大小,增减计算量
cpu_b = tf.random.normal([n,10])
print(cpu_a.device,cpu_b.device)
with tf.device('/gpu:0'):
gpu_a = tf.random.normal([100n])
gpu_b = tf.random.normal([n,10])
print(gpu_a.device,gpu_b.device)
def cpu_run():
with tf.device('/cpu:0'): ##矩阵乘法,此操作采用cpu计算
c = tf.matmul(cpu_a,cpu_b)
return c
def gpu_run():
with tf.device('/gpu:0'): ##矩阵乘法,此操作采用gpu计算
c = tf.matmul(gpu_a,gpu_b)
return c
##第一次计算需要热身,避免将初始化时间计算在内
cpu_time = timeit.timeit(cpu_run,number=10)
gpu_time = timeit.timeit(gpu_run,number=10)
print('warmup:',cpu_time,gpu_time)
##正式计算10次,取平均值
cpu_time = timeit.timeit(cpu_run,number=10)
gpu_time = timeit.timeit(gpu_run,number=10)
print('run_time:',cpu_time,gpu_time)

  通过改变矩阵大小,增加矩阵乘法的计算量:计算结果如下

  结论:1.在计算量较小的情况下,cpu的计算速度比gpu计算速度快,但是都是微量级别的差异

     2.随着计算量的增加,cpu的计算时间逐步增加,而gpu的计算时间相对平缓,在计算量达到一定程度之后,gpu的优势就出来了。

  实现过程的完整代码:

  

import tensorflow as tf
import timeit
import matplotlib.pyplot as plt
'''
以矩阵A[10,n]和矩阵B[n,10]的乘法运算(分别在cpu和gpu上运行)来测试,
'''
def cpu_gpu_compare(n):
with tf.device('/cpu:0'): ##指定操作用cpu计算
cpu_a = tf.random.normal([10,n]) ##生成符合高斯分布的随机数矩阵,通过改变n大小,增减计算量
cpu_b = tf.random.normal([n,10])
print(cpu_a.device,cpu_b.device)
with tf.device('/gpu:0'):
gpu_a = tf.random.normal([10,n])
gpu_b = tf.random.normal([n,10])
print(gpu_a.device,gpu_b.device)
def cpu_run():
with tf.device('/cpu:0'): ##矩阵乘法,此操作采用cpu计算
c = tf.matmul(cpu_a,cpu_b)
return c
def gpu_run():
with tf.device('/gpu:0'): ##矩阵乘法,此操作采用gpu计算
c = tf.matmul(gpu_a,gpu_b)
return c
##第一次计算需要热身,避免将初始化时间计算在内
cpu_time = timeit.timeit(cpu_run,number=10)
gpu_time = timeit.timeit(gpu_run,number=10)
print('warmup:',cpu_time,gpu_time)
##正式计算10次,取平均值
cpu_time = timeit.timeit(cpu_run,number=10)
gpu_time = timeit.timeit(gpu_run,number=10)
print('run_time:',cpu_time,gpu_time)
return cpu_time,gpu_time
n_list1 = range(1,2000,5)
n_list2 = range(2001,10000,100)
n_list = list(n_list1)+list(n_list2)
time_cpu =[]
time_gpu =[]
for n in n_list:
t=cpu_gpu_compare(n)
time_cpu.append(t[0])
time_gpu.append(t[1])
plt.plot(n_list,time_cpu,color = 'red',label='cpu')
plt.plot(n_list,time_gpu,color='green',linewidth=1.0,linestyle='--',label='gpu')
plt.ylabel('耗时',fontproperties = 'SimHei',fontsize = 20)
plt.xlabel('计算量',fontproperties = 'SimHei',fontsize = 20)
plt.title('cpu和gpu计算力比较',fontproperties = 'SimHei',fontsize = 30)
plt.legend(loc='upper right')
plt.show()

(一)tensorflow-gpu2.0学习笔记之开篇(cpu和gpu计算速度比较)的更多相关文章

  1. DirectX 总结和DirectX 9.0 学习笔记

    转自:http://www.cnblogs.com/graphics/archive/2009/11/25/1583682.html DirectX 总结 DDS DirectXDraw Surfac ...

  2. 一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移

    不得不说微软的技术迭代还是很快的,上了微软的船就得跟着她走下去,前文一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx.superviso ...

  3. SQL反模式学习笔记1 开篇

    什么是“反模式” 反模式是一种试图解决问题的方法,但通常会同时引发别的问题. 反模式分类 (1)逻辑数据库设计反模式 在开始编码之前,需要决定数据库中存储什么信息以及最佳的数据组织方式和内在关联方式. ...

  4. vue2.0学习笔记之路由(二)路由嵌套+动画

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. vue2.0学习笔记之路由(二)路由嵌套

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. TensorFlow机器学习框架-学习笔记-001

    # TensorFlow机器学习框架-学习笔记-001 ### 测试TensorFlow环境是否安装完成-----------------------------```import tensorflo ...

  7. hdcms v5.7.0学习笔记

    hdcms v5.7.0学习笔记 https://note.youdao.com/ynoteshare1/index.html?id=c404d63ac910eb15a440452f73d6a6db& ...

  8. dhtmlxgrid v3.0学习笔记

    dhtmlxgrid v3.0学习笔记 分类: dhtmlx JavaScript2012-01-31 15:41 1744人阅读 评论(0) 收藏 举报 stylesheetdatecalendar ...

  9. OAuth 2.0学习笔记

    文章目录 OAuth的作用就是让"客户端"安全可控地获取"用户"的授权,与"服务商提供商"进行互动. OAuth在"客户端&quo ...

随机推荐

  1. java fastjson:Map与json以及JSONObject ,JSONObject与String互转

    import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson ...

  2. Codeforces Round #613 (Div. 2) A-E简要题解

    contest链接:https://codeforces.com/contest/1285 A. Mezo Playing Zoma 签到 #include<iostream> #incl ...

  3. codeforces 1282C. Petya and Exam (贪心)

    链接:https://codeforces.com/contest/1282/problem/C 题意:  有一个人参加考试,考试只有两种题,一种是简单题,每道题耗时固定为a:另一种是困难题,每道题耗 ...

  4. Maven项目中配置文件导出问题

    1.将该设置写在pom.xml中 <build> <resources> <resource> <directory>src/main/resource ...

  5. httpclient发送请求的几种方式

    package asi; import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; ...

  6. Axure 8.1.0.3377 激活码 授权码 (亲测有效)

    适用版本 Axure 8.1.0.3377 zdfans.com gP5uuK2gH+iIVO3YFZwoKyxAdHpXRGNnZWN8Obntqv7++FF3pAz7dTu8B61ySxli 亲测 ...

  7. MySQL在cmd命令行查看端口号

    在命令行输入: show global variables like 'port'; 转自:https://blog.csdn.net/zhufengy/article/details/8014778 ...

  8. 关于Dev-C++的安装以及基本使用方法

    我觉得Dev-C++是一款小巧方便的编译器,就给那些刚刚学习编程的同学讲一下这个软件的安装和基本的编译以及一些使用的技巧. (完全是傻瓜式的截图和教程,内容过于冗余,主要是考虑到这些新生没有接触过编程 ...

  9. 安全文件传输协议之SFTP的使用

    一.SFTP概述 在前几篇文章,我们讲到了文件传输协议FTP(File Transfer Protocol),那也是使用比较广泛的文件服务器,但是我们需要知道,Linux系统并不自带FTP程序 如果要 ...

  10. opencv:形态学操作-开闭操作

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...