我一直在努力优化我的计算;对于我尝试过的大多数操作,tensorflow
都要快得多。我试图做一个相当简单的operation...Transform矩阵(将每个值乘以1/2,然后将1/2加到该值中)。
在@mrry的帮助下,我能够在tensorflow
中执行这些操作。然而,令我惊讶的是,numpy
方法明显更快?!
对于数据科学家来说,tensorflow
似乎是一个非常有用的工具,我认为这有助于澄清它的用途和优势。
我不是以最有效的方式使用tensorflow
数据结构和操作吗?,我不知道非tensorflow方法会有多快。我使用的是2012年中期Macbook Air 4GB内存
trans1是tensorflow版本,而trans2是numpy版本。DF_var是一个熊猫数据对象
import pandas as pd
import tensorflow as tf
import numpy as np
def trans1(DF_var):
#Total user time is 31.8532807827 seconds
#Create placeholder
T_feed = tf.placeholder(tf.float32,DF_var.shape)
#Matrix transformation
T_signed = tf.add(
tf.constant(0.5,dtype=tf.float32),
tf.mul(T_feed,tf.constant(0.5,dtype=tf.float32))
)
#Get rid of of top triangle
T_ones = tf.constant(np.tril(np.ones(DF_var.shape)),dtype=tf.float32)
T_tril = tf.mul(T_signed,T_ones)
#Start Graph Session
sess = tf.Session()
DF_signed = pd.DataFrame(
sess.run(T_tril,feed_dict={T_feed: DF_var.as_matrix()}),
columns = DF_var.columns, index = DF_var.index
)
#Close Graph Session
sess.close()
return(DF_signed)
def trans2(DF_var):
#Total user time is 1.71233415604 seconds
M_computed = np.tril(np.ones(DF_var.shape))*(0.5 + 0.5*DF_var.as_matrix())
DF_signed = pd.DataFrame(M_computed,columns=DF_var.columns, index=DF_var.index)
return(DF_signed)
我的计时方法是:
import time
start_time = time.time()
#operation
print str(time.time() - start_time)
发布于 2017-04-16 02:59:17
您的结果与另一个人的基准兼容。
在他的基准测试中,他比较了NumPy,Theano和Tensorflow
使用Theano 0.8.2,Tensorflow 0.11.0,CUDA 8.0在Linux 18上使用英特尔核心i5-4460和带有4 GiB内存的Nvidia GTX 970 CPU
他的补充结果表明:
他还测试了其他一些函数,如矩阵乘法:
研究结果如下:
很明显,Theano和TensorFlow的主要优点是非常快的点积和矩阵指数。对于最大矩阵,与NumPy相比,Theano/Tensorflow的点积分别快8倍和7倍。奇怪的是,在GPU库中,矩阵添加速度很慢,而NumPy在这些测试中是最快的。 矩阵的最小值和均值在Theano中是慢的,在Tensorflow中是快速的。目前还不清楚为什么西亚诺在这些操作中速度慢(比NumPy差)。
https://stackoverflow.com/questions/35738854
复制相似问题