将值插入张量,如for循环

时间:2018-08-14 21:56:21

标签: python-3.x pandas tensorflow numpy-ndarray

我有下面的代码,其中我遍历数据帧中的列,并将两个一维张量的点积插入每个值中。与其将值插入数据帧中,我不希望将它们插入张量中。我是tensorflow的新手,我想知道如何使用tensorflow做到这一点。是否有一个Tensorflow函数可以让我替换此forloop?

代码:

a = tf.placeholder(tf.float32, shape=(None))
b = tf.placeholder(tf.float32, shape=(None))

dot_a_b = tf.tensordot(a, b, 1)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)

    test_df = pd.DataFrame(columns=x_tst_df.columns, index=x_df.index)
    for i, m in enumerate(test_df.columns):
        test_df.loc[:, m] = sess.run(dot_a_b,feed_dict={a: [1,i,3], b: [1,2,5]})

数据:

输出:

print(test_df.head(n=3))

            channel_0  channel_1  channel_2  channel_3  channel_4
2014-01-05       16.0       18.0       20.0       22.0       24.0
2014-01-12       16.0       18.0       20.0       22.0       24.0
2014-01-19       16.0       18.0       20.0       22.0       24.0

输入:

print(x_tst_df.head())

            channel_0  channel_1  channel_2  channel_3  channel_4
2014-01-05   0.087209   0.440568   0.517735   0.796773   0.240661
2014-01-12   0.896037   0.934202   0.188758   0.310457   0.825940
2014-01-19   0.427786   0.057234   0.053251   0.007518   0.793536

0 个答案:

没有答案