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):
T_feed = tf.placeholder(tf.float32,DF_var.shape)
T_signed = tf.add(
tf.constant(0.5,dtype=tf.float32),
tf.mul(T_feed,tf.constant(0.5,dtype=tf.float32))
)
T_ones = tf.constant(np.tril(np.ones(DF_var.shape)),dtype=tf.float32)
T_tril = tf.mul(T_signed,T_ones)
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
)
sess.close()
return(DF_signed)
def trans2(DF_var):
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()
print str(time.time() - start_time)
source
share