比较theano vs tensorflow函数速度,如何使tensorflow函数更快?

OS:Windows 10 处理器:核心i7-6700

使用蟒蛇的python 3.7

theano版本1.0.4使用以下方法安装:conda install 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

tensorflow 2.4310362339019775

比率4.9742761289013275

所以theano比张量流快5倍左右。如何至少至少与theano一样使Tensorflow代码更快?

bmlun 回答:比较theano vs tensorflow函数速度,如何使tensorflow函数更快?

事实证明,在CPU上运行时,Theano比Tensorflow快得多。如果您有多个GPU,Tensorflow的运行速度只会比Theano快。

本文链接:https://www.f2er.com/2748832.html

大家都在问