比较 theano 与张量流函数的速度,如何使张量流函数更快?



OS: Windows 10 处理器:酷睿i7-6700

Python 3.7 使用 Anaconda

Theano版本 1.0.4 安装使用: Conda 安装 Theano pygpu

TensorFlow 版本 2.1.0 安装使用:pip install tensorflow

两者都使用 cpu 运行

我正在将我的一些代码从theano重写到tensorflow,我发现tensorflow的性能不如theano快,所以我一定错过了一些导致tensorflow代码变慢的东西。

下面是示例代码:

import numpy as np
import theano
import theano.tensor as T
from theano import function
import tensorflow as tf
from time import time

#define tensorflow function
@tf.function
def tf_mean(data):
return tf.math.reduce_mean(data, axis=0)
#define theano function
tdata = T.dmatrix('tdata')
tmean = T.mean(tdata, axis=0)
theano_mean = function([tdata], tmean)
if __name__ == '__main__':
np.random.seed(1234)
randomdata = np.random.random((3000,10))
#run first time to warm up
check_th = theano_mean(randomdata)
check_tf = tf_mean(randomdata)

# run each 10000 times
start = time()
for i in range(10000):
theano_mean(randomdata)
thtime = time()-start
print('theano', thtime )
start = time()
for i in range(10000):
tf_mean(randomdata)
tftime = time()-start
print('tensorflow', tftime )
print('ratio', tftime / thtime)

输出: Theano 0.4887216091156006

张量流 2.4310362339019775

比率 4.9742761289013275

所以theano看起来比张量流快5倍左右。我怎样才能使上面的张量流代码更快,至少与theano相当?

事实证明,在CPU上运行时,Theano比Tensorflow快得多。Tensorflow只有在有多个GPU的情况下才能比Theano运行得更快。

最新更新