Time Comparison for TensorFlow and Numpy Multiplication

I am trying to optimize my calculations; and for most of the operations I tried, it tensorflowis much faster. I'm trying to do a fairly simple operation ... Transform the matrix (multiply each value by 1/2, and then add 1/2 to that value).

Using @mrry I was able to perform these operations in tensorflow. However, to my surprise, the method numpywas significantly faster ?!

tensorflow seems to be an extremely useful tool for scholars, and I think this could help clarify its use and benefits.

I do not use tensorflowdata structures and operations in the most efficient way? I'm not sure that non-tensorflow methods will be faster. I am using Mid-2012 Macbook Air 4GB RAM

trans1 is the version with tensor flow, and trans2 is numpy. DF_var is a pandas DataFrame object

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)

My synchronization method was:

import time
start_time = time.time()
#operation
print str(time.time() - start_time)
0
source share
1 answer

Your results are compatible with tests from another guy .

In his test, he compared NumPy, Theano, and Tensorflow to

Intel Core i5-4460 processor with 16 GB of RAM and an Nvidia GTX 970 with 4 GiB RAM using Theano 0.8.2, Tensorflow 0.11.0, CUDA 8.0 on Linux Mint 18

, : enter image description here

, :

enter image description here

:

, Theano TensorFlow . 8 7 Theano/Tensorflow NumPy . , GPU, NumPy .

Theano Tensorflow. , (, NumPy) .

+1

Source: https://habr.com/ru/post/1664312/


All Articles