尝试运行时,抛出以下异常(ValueError)
ValueError: Shape () must have rank at least 2
这是针对以下行抛出的:
states_series, current_state = tf.contrib.rnn.static_rnn(cell, inputs_series, init_state)
这里定义了cell
:
cell = tf.contrib.rnn.BasicLSTMCell(state_size, state_is_tuple=True)
查看 RNN 的规则和 Tesor_shape ,我可以看出这是某种张量维度形状问题。据我所知,它无法将 BasicLSTMCell
视为 2 阶矩阵?
完整错误:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/glennhealy/PycharmProjects/firstRNNTest/LSTM-RNN.py
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
return f(*args, **kwds)
Traceback (most recent call last):
File "/Users/glennhealy/PycharmProjects/firstRNNTest/LSTM-RNN.py", line 42, in <module>
states_series, current_state = tf.contrib.rnn.static_rnn(cell, inputs_series, init_state)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/rnn.py", line 1181, in static_rnn
input_shape = first_input.get_shape().with_rank_at_least(2)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py", line 670, in with_rank_at_least
raise ValueError("Shape %s must have rank at least %d" % (self, rank))
ValueError: Shape () must have rank at least 2
Process finished with exit code 1
代码:
state_size = 4
cell = tf.contrib.rnn.BasicLSTMCell(state_size, state_is_tuple=True)
states_series, current_state = tf.contrib.rnn.static_rnn(cell, inputs_series, init_state)
tensorflow 1.2.1 Python 3.6 NumPy
更新更多信息:
考虑到@Maxim 给出的建议,我可以看出问题出在我的 input_series
上,这导致了形状问题,但是,我似乎无法理解他的建议。
一些有助于解决问题的更多信息,看看我是否能理解如何解决这个问题:
以下是否可以替代我的 BatchY 和 BatchX 占位符?
X = tf.placeholder(dtype=tf.float32, shape=[None, n_steps, n_inputs])
X_seqs = tf.unstack(tf.transpose(X, perm=[1, 0, 2]))
basic_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=n_neurons)
output_seqs, states = tf.nn.static_rnn(basic_cell, X_seqs, dtype=tf.float32)
那么,我是否必须对以下内容进行更改以反射(reflect)以下内容的语法?
batchX_placeholder = tf.placeholder(tf.int32, [batch_size, truncated_backprop_length])
batchY_placeholder = tf.placeholder(tf.float32, [batch_size, truncated_backprop_length])
#unpacking the columns:
labels_series = tf.unstack(batchY_placeholder, axis=1)
inputs_series = tf.split(1, truncated_backprop_length, batchX_placeholder)
#Forward pass
cell = tf.contrib.rnn.BasicLSTMCell(state_size, state_is_tuple=True)
states_series, current_state = tf.contrib.rnn.static_rnn(cell, inputs_series, init_state)
losses = [tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels) for logits, labels in zip(logits_series,labels_series)]
total_loss = tf.reduce_mean(losses)
最佳答案
是的,问题出在 inputs_series
上。根据错误,它是一个形状为 ()
的张量,即只是一个数字。
来自 tf.nn.static_rnn
文档:
inputs
: A lengthT
list of inputs, each a Tensor of shape[batch_size, input_size]
, or a nested tuple of such elements.
在大多数情况下,您希望inputs
为[seq_length, None, input_size]
,其中:
seq_length
是序列长度,或 LSTM 单元的数量。None
代表批量大小(任意)。input_size
是每个单元格的特征数。
因此请确保您的占位符(以及由此转换而来的 inputs_series
)具有适当的形状。示例:
X = tf.placeholder(dtype=tf.float32, shape=[None, n_steps, n_inputs])
X_seqs = tf.unstack(tf.transpose(X, perm=[1, 0, 2]))
basic_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=n_neurons)
output_seqs, states = tf.nn.static_rnn(basic_cell, X_seqs, dtype=tf.float32)
更新:
这是 split 张量的错误方式:
# WRONG!
inputs_series = tf.split(1, truncated_backprop_length, batchX_placeholder)
你应该这样做(注意参数的顺序):
inputs_series = tf.split(batchX_placeholder, truncated_backprop_length, axis=1)
关于python - Tensorflow LSTM 抛出 ValueError : Shape () must have rank at least 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47877858/