问题描述
我一直在尝试优化我的计算;对于我尝试过的大多数操作,tensorflow
都快得多.我正在尝试做一个相当简单的操作...变换一个矩阵(将每个值乘以1/2,然后将该值加1/2).
I've been trying to optimize my computations; and for most operations that I've tried, tensorflow
is much faster. I'm trying to do a fairly simple operation...Transform a matrix (multiply each value by 1/2 and then add 1/2 to that value).
借助@mrry,我能够在tensorflow
中执行这些操作.但是令我惊讶的是,numpy
方法显着更快?!
With the help of @mrry , I was able to do these operations in tensorflow
. However to my surprise, the numpy
method was significantly faster?!
tensorflow
似乎是一个非常有用的工具,我认为这可以帮助阐明其用途和优势.
tensorflow
seems like an extremely useful tool for data scientists and I think this could help clarify it's use and advantages.
我不是以最有效的方式使用tensorflow
数据结构和操作吗?我不确定使用非张量流方法会更快吗?我正在使用2012年中的Macbook Air 4GB RAM
Am I not using tensorflow
data structures and operations in the most efficient way? I'm not sure how non-tensorflow methods would be faster. I'm using a Mid-2012 Macbook Air 4GB RAM
trans1是张量流版本,而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)
推荐答案
您的结果与另一个人的基准测试.
在基准测试中,他在
他的加法结果表明:
His results for addition shows that:
他还测试了其他一些功能,例如矩阵乘法:
He also tested a few other functions such as matrix multiplication:
结果是:
在Theano中矩阵的最小值和平均值很慢,而在 Tensorflow.目前尚不清楚为什么Theano这么慢(比NumPy还差) 这些操作.
The minimum and mean of matrices are slow in Theano and quick in Tensorflow. It is not clear why Theano is as slow (worse than NumPy) for these operations.
这篇关于TensorFlow操作和Numpy乘法的时间比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!