Я хочу сериализовать данные для ввода модели 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.)
Я думаю, что все переменные должны быть тензорными, но я получаю ошибку типа, которая показывает, что я передаю тип массива.
Кто-нибудь знает это? Надеюсь на вашу помощь, спасибо!