首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >坦索弗洛:如何插入未知张量?

坦索弗洛:如何插入未知张量?
EN

Stack Overflow用户
提问于 2018-05-04 02:30:25
回答 1查看 78关注 0票数 0

我想序列化用于输入LSTM模型的数据,

代码语言:javascript
复制
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)

产出如下:

代码语言:javascript
复制
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时,除了第一行和最后一行之外,数据将重复一次。

但是,现在我想使用张量而不是array.And来操作代码,如下所示:

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
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.)

我认为所有变量都应该是张量,但是我得到了typeerror,它显示了一个数组类型。

有人知道吗?希望你的帮助,谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-21 13:16:04

代码语言:javascript
复制
# 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) 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50166288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档