TensorFlow 运算和 Numpy 乘法的时间比较
Time comparison for TensorFlow operation and Numpy multiplication
我一直在努力优化我的计算;对于我尝试过的大多数操作,tensorflow
要快得多。我正在尝试做一个相当简单的操作...转换矩阵(将每个值乘以 1/2,然后将 1/2 添加到该值)。
在@mrry 的帮助下,我能够在 tensorflow
中完成这些操作。然而令我惊讶的是,numpy
方法明显更快?!
tensorflow
对数据科学家来说似乎是一个非常有用的工具,我认为这有助于阐明它的用途和优势。
我没有以最有效的方式使用 tensorflow
数据结构和操作吗? 我不确定非张量流方法会更快。我使用的是 2012 年中期 Macbook Air 4GB RAM
trans1是tensorflow版本,trans2是numpy。 DF_var 是一个 pandas 数据框对象
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)
您的结果与 benchmarks from another guy 兼容。
在他的基准测试中,他在
上比较了 NumPy、Theano 和 Tensorflow
an Intel core i5-4460 CPU with 16GiB RAM and a Nvidia GTX 970 with 4
GiB RAM using Theano 0.8.2, Tensorflow 0.11.0, CUDA 8.0 on Linux Mint
18
他的加法结果表明:
他还测试了一些其他函数,例如矩阵乘法:
结果是:
It is clear that the main strengths of Theano and TensorFlow are very
fast dot products and matrix exponents. The dot product is
approximately 8 and 7 times faster respectively with Theano/Tensorflow
compared to NumPy for the largest matrices. Strangely, matrix addition
is slow with the GPU libraries and NumPy is the fastest in these
tests.
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
要快得多。我正在尝试做一个相当简单的操作...转换矩阵(将每个值乘以 1/2,然后将 1/2 添加到该值)。
在@mrry 的帮助下,我能够在 tensorflow
中完成这些操作。然而令我惊讶的是,numpy
方法明显更快?!
tensorflow
对数据科学家来说似乎是一个非常有用的工具,我认为这有助于阐明它的用途和优势。
我没有以最有效的方式使用 tensorflow
数据结构和操作吗? 我不确定非张量流方法会更快。我使用的是 2012 年中期 Macbook Air 4GB RAM
trans1是tensorflow版本,trans2是numpy。 DF_var 是一个 pandas 数据框对象
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)
您的结果与 benchmarks from another guy 兼容。
在他的基准测试中,他在
上比较了 NumPy、Theano 和 Tensorflowan Intel core i5-4460 CPU with 16GiB RAM and a Nvidia GTX 970 with 4 GiB RAM using Theano 0.8.2, Tensorflow 0.11.0, CUDA 8.0 on Linux Mint 18
他的加法结果表明:
他还测试了一些其他函数,例如矩阵乘法:
结果是:
It is clear that the main strengths of Theano and TensorFlow are very fast dot products and matrix exponents. The dot product is approximately 8 and 7 times faster respectively with Theano/Tensorflow compared to NumPy for the largest matrices. Strangely, matrix addition is slow with the GPU libraries and NumPy is the fastest in these tests.
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.