如何用TensorFlow计算矩阵运算?
问题描述:
我有一个包含从0到1浮点数的大熊猫数据帧。
我想将这个矩阵以一定的功率(例如6)取幂。如何用TensorFlow计算矩阵运算?
我开始使用scipy
但操作正在采取真的,真的很长,我的7000x7000矩阵,所以我想这将是测试出tensorflow
我道歉,一个极好的机会,如果所用的符号约迷幻,我想我正确地输入了一切。我想要使用placeholder
和feed
。 我的功能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)
答
编辑:问题已更新到删除错误讯息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 –
能否请您给的速度增益评论? – davidhigh