简体   繁体   English

如何使用TensorFlow计算矩阵运算?

[英]How to compute matrix operation with TensorFlow?

I have a pandas dataframe containing floats from 0 to 1. 我有一个包含从0到1的浮点数的pandas数据帧。
I want to exponentiate this matrix to a certain power (eg 6). 我想要将此矩阵取幂为某个幂(例如6)。

I started using scipy but the operation was taking really, really long for my 7000x7000 matrix so I thought this would be an excellent opportunity to test out tensorflow 我开始使用scipy但是我的7000x7000矩阵的操作非常非常长,所以我认为这将是测试tensorflow的绝佳机会

My apologies if the notation is about trippy, I thought I was inputting everything correctly. 如果符号是关于幻觉,我很抱歉,我以为我正在输入所有内容。 I want o use a placeholder and feed . 我想要使​​用placeholderfeed My function exp_corr inputs a pandas dataframe object and then exponentiates the matrix to the power of a certain integer. 我的函数exp_corr输入一个pandas数据帧对象,然后将矩阵取幂到某个整数的幂。

How do I use the placeholder with the feed_dict? 如何将占位符与feed_dict一起使用?

Here's my code: 这是我的代码:

#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 looks like: 数据帧看起来像:

              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

Matrix exponential function I tried: 矩阵指数函数我试过:

#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)

EDIT: The question has been updated to remove the error message. 编辑:问题已更新,以删除错误消息。 You are very close to being able to feed the matrix into your program. 您非常接近能够将矩阵提供给您的程序。 The following version of your exp_corr() function should do the trick: 以下版本的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

The original issue with your program was in the error message: 您的程序的原始问题在错误消息中:

Node 'Input Dataframe': Node name contains invalid characters

In particular, the name argument to TensorFlow op constructors (like tf.constant() and tf.pow() ) must be a string that does not contain spaces . 特别是,TensorFlow操作tf.constant()name参数(如tf.constant()tf.pow() )必须是不包含空格的字符串。

The syntax for node names is defined here . 此处定义了节点名称的语法。 Node names must match the following regular expression (essentially alpha-numeric, plus . , _ , and / , but not starting with _ or / ): 节点名称必须与以下正则表达式匹配(基本上是字母数字,加上._/ ,但不是以_/开头):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM