【问题标题】:Tensorflow: How to insert an unknown tensor?Tensorflow:如何插入未知张量?
【发布时间】:2018-10-14 10:21:17
【问题描述】:

我想序列化 LSTM 模型输入的数据,例如,

import numpy as np
import tensorflow as tf

input_x=np.array([[1,2,1,2,1,2],[3,4,3,4,3,4],[10,20,1,2,1,2],[30,40,3,4,3,4],[100,200,1,2,1,2],[300,400,3,4,3,4]])#shape:6-6
# x = tf.placeholder(tf.float32,[None,6])
x=input_x
x_copy=x.copy()
# x_copy=tf.identity(x)

batch_size=6
n_steps=2

count=0
for i in range(int(batch_size/n_steps)-1):#total insert
    for j in range(n_steps-1):
        for k in range(n_steps):
            x_copy=np.insert(x_copy,(i+1)*n_steps+count,x[i*n_steps+j+k+1],axis=0)
            count+=1
res=x_copy
print('input_x\n',input_x)
print('res\n',res)

输出如下:

input_x
 [[  1   2   1   2   1   2]
 [  3   4   3   4   3   4]
 [ 10  20   1   2   1   2]
 [ 30  40   3   4   3   4]
 [100 200   1   2   1   2]
 [300 400   3   4   3   4]]
res
 [[  1   2   1   2   1   2]
 [  3   4   3   4   3   4]
 [  3   4   3   4   3   4]
 [ 10  20   1   2   1   2]
 [ 10  20   1   2   1   2]
 [ 30  40   3   4   3   4]
 [ 30  40   3   4   3   4]
 [100 200   1   2   1   2]
 [100 200   1   2   1   2]
 [300 400   3   4   3   4]]

由于我设置了 n_steps=2,所以数据会重复一次,除了第一行和最后一行。

但是,现在我想用张量而不是数组来操作。代码更改如下:

import numpy as np
import tensorflow as tf

input_x=np.array([[1,2,1,2,1,2],[3,4,3,4,3,4],[10,20,1,2,1,2],[30,40,3,4,3,4],[100,200,1,2,1,2],[300,400,3,4,3,4]])#shape:6-6
x = tf.placeholder(tf.float32,[None,6])
# x=input_x

# x_copy=x.copy()
x_copy=tf.identity(x)

batch_size=6
n_steps=2

count=0
for i in range(int(batch_size/n_steps)-1):#total insert
    for j in range(n_steps-1):
        for k in range(n_steps):
            x_copy=np.insert(x_copy,(i+1)*n_steps+count,x[i*n_steps+j+k+1],axis=0)
            count+=1
res=x_copy
# print('input_x\n',input_x)
# print('res\n',res)

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    batch_x=input_x
    result=sess.run([res,],feed_dict={
        x:batch_x,
    })
    print('result\n',result)

然后我遇到一个错误,可以显示如下:

TypeError: Fetch argument array(<tf.Tensor 'strided_slice_3:0' shape=(6,) dtype=float32>,
      dtype=object) has invalid type <class 'numpy.ndarray'>, must be a string or Tensor. (Can not convert a ndarray into a Tensor or Operation.)

我认为所有变量都应该是张量,但我得到了类型错误,表明我提供了一个数组类型。

有人知道吗?希望得到您的帮助,谢谢!

【问题讨论】:

  • 你的第一个代码,即使它有效,也是低效的。 np.insert 创建一个新数组;它不只是修改输入。在循环中使用它不是一个好主意,更不用说嵌套循环了。这也可能是 tensorflow 问题您可以将x_copy 开头为tf,但在一个insert 之后它将是ndarray
  • 感谢您的回复。我想你是正确的。现在我没有一个好主意来实现它。也许我应该使用tf.tile 而不是np.insert
  • 好的,也许我用tf.slice()tf.concat()解决了这个问题。先通过tf.slice()复制张量,然后将它们连接起来。

标签: python numpy tensorflow typeerror tensor


【解决方案1】:
# for i in range(int(batch_size-n_steps)+1):#total insert
    # for k in range(n_steps):
        # hidden1_copy_tmp=tf.slice(hidden1, [k+i, 0], [1, hidden1.shape[1]])
        # if hidden1_copy== None:
            # hidden1_copy=hidden1_copy_tmp
        # else:
            # hidden1_copy=tf.concat([hidden1_copy, hidden1_copy_tmp], 0) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    相关资源
    最近更新 更多