【发布时间】:2018-06-01 08:24:44
【问题描述】:
尝试运行时,抛出如下异常(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)
张量流 1.2.1 蟒蛇 3.6 数字化
更新更多信息:
考虑到@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)
那么,我是否必须对以下内容进行更改以反映以下内容的语法?
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)
【问题讨论】:
-
会不会是 input_series? inputs_series = tf.split(1, truncated_backprop_length, batchX_placeholder) labels_series = tf.unstack(batchY_placeholder, axis=1)
标签: python tensorflow neural-network lstm recurrent-neural-network