Python 比较Ano和tensorflow函数的速度,如何使tensorflow函数更快?
操作系统:windows 10 程序:核心i7-6700 使用anaconda的python 3.7 theano版本1.0.4安装使用:conda安装theano pygpu tensorflow 2.1.0版安装使用:pip安装tensorflow 两者都使用cpu运行 我正在将我的一些代码从theano重写为tensorflow,我发现tensorflow中的性能没有theano快,所以我肯定缺少了一些导致tensorflow代码变慢的东西 下面是示例代码:Python 比较Ano和tensorflow函数的速度,如何使tensorflow函数更快?,python,tensorflow,theano,Python,Tensorflow,Theano,操作系统:windows 10 程序:核心i7-6700 使用anaconda的python 3.7 theano版本1.0.4安装使用:conda安装theano pygpu tensorflow 2.1.0版安装使用:pip安装tensorflow 两者都使用cpu运行 我正在将我的一些代码从theano重写为tensorflow,我发现tensorflow中的性能没有theano快,所以我肯定缺少了一些导致tensorflow代码变慢的东西 下面是示例代码: import numpy as
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)
输出:
编号0.488721609156006
tensorflow 2.4310362339019775
比率4.9742761289013275
所以theano看起来比tensorflow快5倍左右。我怎样才能使ToSoFalm代码至少比TeaNo快得多?< P>。结果发现,Theano在CPU上运行时比Tensorflow快得多。如果您有多个GPU,Tensorflow只会比Theano运行得更快