如何使用 TensorFlow 计算矩阵运算?
How to compute matrix operation with TensorFlow?
我有一个 pandas 数据框,其中包含从 0 到 1 的浮点数。
我想对这个矩阵求幂(例如 6)。
我开始使用 scipy
,但是对于我的 7000x7000 矩阵来说,操作真的非常非常长,所以我认为这将是测试 tensorflow
的绝佳机会
如果符号是关于迷幻的,我深表歉意,我以为我输入的所有内容都是正确的。我想使用 placeholder
和 feed
。
我的函数 exp_corr
输入一个 pandas 数据框对象,然后将矩阵取幂为某个整数的幂。
如何将占位符与 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)
编辑:问题已更新以删除错误消息。您非常接近能够将矩阵输入到您的程序中。以下版本的 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
特别是,TensorFlow op 构造函数(如 tf.constant()
和 tf.pow()
)的 name
参数必须是 不包含空格的字符串.
节点名称的语法已定义 here。节点名称必须匹配以下正则表达式(本质上是字母数字,加上 .
、_
和 /
,但不以 _
或 /
开头) :
[A-Za-z0-9.][A-Za-z0-9_./]*
我有一个 pandas 数据框,其中包含从 0 到 1 的浮点数。
我想对这个矩阵求幂(例如 6)。
我开始使用 scipy
,但是对于我的 7000x7000 矩阵来说,操作真的非常非常长,所以我认为这将是测试 tensorflow
如果符号是关于迷幻的,我深表歉意,我以为我输入的所有内容都是正确的。我想使用 placeholder
和 feed
。
我的函数 exp_corr
输入一个 pandas 数据框对象,然后将矩阵取幂为某个整数的幂。
如何将占位符与 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)
编辑:问题已更新以删除错误消息。您非常接近能够将矩阵输入到您的程序中。以下版本的 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
特别是,TensorFlow op 构造函数(如 tf.constant()
和 tf.pow()
)的 name
参数必须是 不包含空格的字符串.
节点名称的语法已定义 here。节点名称必须匹配以下正则表达式(本质上是字母数字,加上 .
、_
和 /
,但不以 _
或 /
开头) :
[A-Za-z0-9.][A-Za-z0-9_./]*