Python 如何用张量流计算矩阵运算?
我有一个包含从0到1的浮动的数据帧。Python 如何用张量流计算矩阵运算?,python,numpy,matrix,tensorflow,exponential,Python,Numpy,Matrix,Tensorflow,Exponential,我有一个包含从0到1的浮动的数据帧。 我想将这个矩阵的幂运算到一定的幂(例如6)。 我开始使用scipy,但是操作对我的7000x7000矩阵来说真的非常耗时,所以我认为这将是一个测试tensorflow 如果符号是关于trippy的,我很抱歉,我以为我输入的东西都是正确的。我想使用占位符和提要。 我的函数exp_corr输入一个数据帧对象,然后将矩阵按某个整数的幂进行幂运算 如何将占位符与提要内容一起使用? 这是我的密码: #Example DataFrame L_test = [[0.999
我想将这个矩阵的幂运算到一定的幂(例如6)。 我开始使用
scipy
,但是操作对我的7000x7000矩阵来说真的非常耗时,所以我认为这将是一个测试tensorflow
如果符号是关于trippy的,我很抱歉,我以为我输入的东西都是正确的。我想使用占位符
和提要
。
我的函数exp_corr
输入一个数据帧对象,然后将矩阵按某个整数的幂进行幂运算
如何将占位符与提要内容一起使用?
这是我的密码:
#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
Dataframe看起来像:
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)
编辑:问题已更新以删除错误消息。您非常接近能够将矩阵输入到您的程序中。以下版本的
exp\u 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
特别是,TensorFlow op构造函数的name
参数(如tf.constant()
和tf.pow()
)必须是一个不包含空格的字符串
定义了节点名称的语法。节点名称必须匹配以下正则表达式(基本上是字母数字,加上
、
和/
,但不能以
或/
开头):
你能对速度的提高发表意见吗?
[A-Za-z0-9.][A-Za-z0-9_./]*