TensorFlow操作和Numpy乘法的时间比较 [英] Time comparison for TensorFlow operation and Numpy multiplication

查看:280
本文介绍了TensorFlow操作和Numpy乘法的时间比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试优化我的计算;对于我尝试过的大多数操作,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)

推荐答案

您的结果与在基准测试中,他在

具有16GiB RAM的英特尔酷睿i5-4460 CPU和具有4G的Nvidia GTX 970 在Linux Mint上使用Theano 0.8.2,Tensorflow 0.11.0,CUDA 8.0的GiB RAM 18

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

他的加法结果表明:

His results for addition shows that:

他还测试了其他一些功能,例如矩阵乘法:

He also tested a few other functions such as matrix multiplication:

结果是:

很明显,Theano和TensorFlow的主要优势是 快速的点积和矩阵指数.点积是 使用Theano/Tensorflow分别快约8倍和7倍 与NumPy相比,最大的矩阵.奇怪的是,矩阵加法 GPU库运行缓慢,而NumPy在这些库中运行速度最快 测试.

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.

在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乘法的时间比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆