Tensorflow LSTM : ValueError: Shape must have rank at least 3
Tensorflow LSTM : ValueError: Shape must have rank at least 3
如何使用 tf.unstack 将输入馈送到 tensorflow 中的 LSTM rnn:
所以如果我的输入形状是 [4,5,2]
即 [batch_size , time_stamp , n_input]
现在如果我尝试:
dataet=[[[3, 5], [7, 2], [7, 6]],
[[2, 5], [1, 3], [4, 3]],
[[8, 1], [1, 8], [9, 3]],
[[1, 5], [6, 7], [4, 9]]]
import tensorflow as tf
from tensorflow.contrib import rnn
import numpy as np
input_x=tf.placeholder(dtype=tf.int32,shape=[4,3,2])
input_x=tf.cast(input_x,tf.float32)
data=tf.unstack(input_x,3,axis=1)
with tf.variable_scope('encoder') as scope:
cell=rnn.LSTMCell(num_units=250)
model=tf.nn.bidirectional_dynamic_rnn(cell,cell,inputs=data,dtype=tf.float32)
output,(fs,fc)=model
with tf.Session() as sess:
unstack_output,output_n=sess.run([output,data],feed_dict={input_x:dataet})
print(unstack_output,output_n)
我遇到错误:
/anaconda/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: compiletime version 3.6 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.5
return f(*args, **kwds)
Traceback (most recent call last):
File "/Users/exepaul/Desktop/limit_exceed/nad.py", line 25, in <module>
model=tf.nn.bidirectional_dynamic_rnn(cell,cell,inputs=data,dtype=tf.float32)
File "/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/rnn.py", line 416, in bidirectional_dynamic_rnn
time_major=time_major, scope=fw_scope)
File "/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/rnn.py", line 632, in dynamic_rnn
dtype=dtype)
File "/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/rnn.py", line 695, in _dynamic_rnn_loop
for input_ in flat_input)
File "/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/rnn.py", line 695, in <genexpr>
for input_ in flat_input)
File "/anaconda/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 673, in with_rank_at_least
raise ValueError("Shape %s must have rank at least %d" % (self, rank))
ValueError: Shape (2, 4) must have rank at least 3
如何在 tf.unstack 之后调整 RNN 的输入?
我试图检查 this 但没有答案
我的设置
Tensorflow : 1.6.0
Python 3.5.4 |Anaconda custom (x86_64)|
Osx 10.12.4
为什么要对输入数据进行 unstack
?。
RNN 的输入应该是 time_major == False
(默认)的 [batch_size, max_time, n_input]
形状的张量和 time_major == True
的 [max_time, batch_size, n_input]
形状的张量。
只需传递输入,无需 unstack
操作即可。
如何使用 tf.unstack 将输入馈送到 tensorflow 中的 LSTM rnn:
所以如果我的输入形状是 [4,5,2]
即 [batch_size , time_stamp , n_input]
现在如果我尝试:
dataet=[[[3, 5], [7, 2], [7, 6]],
[[2, 5], [1, 3], [4, 3]],
[[8, 1], [1, 8], [9, 3]],
[[1, 5], [6, 7], [4, 9]]]
import tensorflow as tf
from tensorflow.contrib import rnn
import numpy as np
input_x=tf.placeholder(dtype=tf.int32,shape=[4,3,2])
input_x=tf.cast(input_x,tf.float32)
data=tf.unstack(input_x,3,axis=1)
with tf.variable_scope('encoder') as scope:
cell=rnn.LSTMCell(num_units=250)
model=tf.nn.bidirectional_dynamic_rnn(cell,cell,inputs=data,dtype=tf.float32)
output,(fs,fc)=model
with tf.Session() as sess:
unstack_output,output_n=sess.run([output,data],feed_dict={input_x:dataet})
print(unstack_output,output_n)
我遇到错误:
/anaconda/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: compiletime version 3.6 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.5
return f(*args, **kwds)
Traceback (most recent call last):
File "/Users/exepaul/Desktop/limit_exceed/nad.py", line 25, in <module>
model=tf.nn.bidirectional_dynamic_rnn(cell,cell,inputs=data,dtype=tf.float32)
File "/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/rnn.py", line 416, in bidirectional_dynamic_rnn
time_major=time_major, scope=fw_scope)
File "/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/rnn.py", line 632, in dynamic_rnn
dtype=dtype)
File "/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/rnn.py", line 695, in _dynamic_rnn_loop
for input_ in flat_input)
File "/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/rnn.py", line 695, in <genexpr>
for input_ in flat_input)
File "/anaconda/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 673, in with_rank_at_least
raise ValueError("Shape %s must have rank at least %d" % (self, rank))
ValueError: Shape (2, 4) must have rank at least 3
如何在 tf.unstack 之后调整 RNN 的输入?
我试图检查 this 但没有答案
我的设置
Tensorflow : 1.6.0
Python 3.5.4 |Anaconda custom (x86_64)|
Osx 10.12.4
为什么要对输入数据进行 unstack
?。
RNN 的输入应该是 time_major == False
(默认)的 [batch_size, max_time, n_input]
形状的张量和 time_major == True
的 [max_time, batch_size, n_input]
形状的张量。
只需传递输入,无需 unstack
操作即可。