tensorflow GPU小测试
2019.01.18补充:这个例子不具有代表性,涉及到卷积运算的时候,GPU的加速效果会体现得比较明显。
简单测试了一下tensorflow的GPU计算和CPU计算的区别。这里的计算例子只非常简单的小规模矩阵相乘,但是也体现出了CPU和GPU算力的差距,代码及结果如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45import tensorflow as tf import datetime #running # Creates a graph.(cpu version) print('cpu version') starttime1 = datetime.datetime.now() with tf.device('/gpu:0'): a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0,1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[6, 9], name='a') b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0,1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[9, 6], name='b') c = tf.matmul(a, b) c = tf.matmul(c,a) c = tf.matmul(c,b) # Creates a session with log_device_placement set to True. sess1 = tf.Session(config=tf.ConfigProto(log_device_placement=True)) # Runs the op. for i in range(59999): sess1.run(c) print(sess1.run(c)) sess1.close() endtime1 = datetime.datetime.now() time1 = (endtime1 - starttime1).microseconds #print('time1:',time1) ############################################# print('gpuversion') # Creates a graph.(gpu version) starttime2 = datetime.datetime.now() #running with tf.device('/gpu:0'): a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0,1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[6, 9], name='a') b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0,1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[9, 6], name='b') c = tf.matmul(a, b) c = tf.matmul(c,a) c = tf.matmul(c,b) # Creates a session with log_device_placement set to True. sess2 = tf.Session(config=tf.ConfigProto(log_device_placement=True)) # Runs the op. for i in range(59999): sess2.run(c) print(sess2.run(c)) sess2.close() endtime2 = datetime.datetime.now() time2 = (endtime2 - starttime2).microseconds print('time1:',time1) print('time2:',time2)
结果如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89cpu version [[ 18225. 36450. 54675. 72900. 91125. 109350.] [ 24300. 48600. 72900. 97200. 121500. 145800.] [ 18225. 36450. 54675. 72900. 91125. 109350.] [ 24300. 48600. 72900. 97200. 121500. 145800.] [ 18225. 36450. 54675. 72900. 91125. 109350.] [ 24300. 48600. 72900. 97200. 121500. 145800.]] gpuversion [[ 18225. 36450. 54675. 72900. 91125. 109350.] [ 24300. 48600. 72900. 97200. 121500. 145800.] [ 18225. 36450. 54675. 72900. 91125. 109350.] [ 24300. 48600. 72900. 97200. 121500. 145800.] [ 18225. 36450. 54675. 72900. 91125. 109350.] [ 24300. 48600. 72900. 97200. 121500. 145800.]] time1: 356158 time2: 363249
注:以上时间单位是微秒
最后
以上就是受伤鼠标最近收集整理的关于tensorflow GPU小测试的全部内容,更多相关tensorflow内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复