2016-03-02 196 views
1

我有一個包含從0到1浮點數的大熊貓數據幀。
我想將這個矩陣以一定的功率(例如6)取冪。如何用TensorFlow計算矩陣運算?

我開始使用scipy但操作正在採取真的,真的很長,我的7000x7000矩陣,所以我想這將是測試出tensorflow

我道歉,一個極好的機會,如果所用的符號約迷幻,我想我正確地輸入了一切。我想要使​​用placeholderfeed。 我的功能exp_corr輸入一個熊貓數據框對象,然後將該矩陣冪乘以某個整數的冪。

如何在feed_dict中使用佔位符?

這裏是我的代碼:

#Example DataFrame 
L_test = [[0.999999999999999, 
    0.374449352805868, 
    0.000347439531148995, 
    0.00103026903356954, 
    0.0011830950375467401], 
[0.374449352805868, 
    1.0, 
    1.17392596672424e-05, 
    1.49428208843456e-07, 
    1.216664263989e-06], 
[0.000347439531148995, 
    1.17392596672424e-05, 
    1.0, 
    0.17452569907144502, 
    0.238497202355299], 
[0.00103026903356954, 
    1.49428208843456e-07, 
    0.17452569907144502, 
    1.0, 
    0.7557000865939779], 
[0.0011830950375467401, 
    1.216664263989e-06, 
    0.238497202355299, 
    0.7557000865939779, 
    1.0]] 
labels = ['AF001', 'AF002', 'AF003', 'AF004', 'AF005'] 
DF_corr = pd.DataFrame(L_test,columns=labels,index=labels) 
DF_signed = np.tril(np.ones(DF_corr.shape)) * DF_corr 

數據框的樣子:

   AF001   AF002  AF003 AF004 AF005 
AF001 1.000000 0.000000e+00 0.000000 0.0000  0 
AF002 0.374449 1.000000e+00 0.000000 0.0000  0 
AF003 0.000347 1.173926e-05 1.000000 0.0000  0 
AF004 0.001030 1.494282e-07 0.174526 1.0000  0 
AF005 0.001183 1.216664e-06 0.238497 0.7557  1 

矩陣指數函數我想:

#TensorFlow Computation 
def exp_corr(DF_var,exp=6): 
#  T_feed = tf.placeholder("float", DF_var.shape) ? 
    T_con = tf.constant(DF_var.as_matrix(),dtype="float") 
    T_exp = tf.pow(T_con, exp) 

    #Initiate 
    init = tf.initialize_all_variables() 
    sess = tf.Session() 
    DF_exp = pd.DataFrame(sess.run(T_exp)) 
    DF_exp.columns = DF_var.column; DF_exp.index = DF_var.index 
    sess.close() 
    return(DF_exp) 

DF_exp = exp_corr(DF_signed) 
+0

能否請您給的速度增益評論? – davidhigh

回答

3

編輯:問題已更新到刪除錯誤訊息GE。您可以將矩陣饋送到您的程序中,非常接近。您exp_corr()功能的以下版本應該做的伎倆:

def exp_corr(DF_var,exp=6): 
    T_feed = tf.placeholder(tf.float32, DF_var.shape) 
    T_exp = tf.pow(T_feed, exp) 

    sess = tf.Session() 

    # Use the `feed_dict` argument to specify feeds. 
    DF_exp = pd.DataFrame(sess.run(T_exp, feed_dict={T_feed: DF_var.as_matrix()})) 
    DF_exp.columns = DF_var.column; DF_exp.index = DF_var.index 

    sess.close() 

    return DF_exp 

原來的問題與你的程序是錯誤消息:

Node 'Input Dataframe': Node name contains invalid characters 

尤其是name參數TensorFlow運構造函數(如tf.constant()tf.pow())必須是不包含空格的字符串。

節點名稱的語法定義爲here。節點名稱必須與以下正則表達式匹配(主要是字母,數字,加._,和/,而與_/開始):

[A-Za-z0-9.][A-Za-z0-9_./]* 
+0

感謝您的編輯! +1 –