我正在使用 TensorFlow 0.9 版实现双向标记 GRU 网络(前一层,后一层)。在模型初始化时,TensorFlow 会初始化所有变量,创建 GRU 单元并正确应用所有常规转换,直到运行该tf.nn.bidirectional_rnn
函数,它会抛出与形状不正确的张量合并操作相关的 ValueError。这是代码:
# Create the cells
with tf.variable_scope('forward'):
self.char_gru_cell_fw = tf.nn.rnn_cell.GRUCell(char_hidden_size)
with tf.variable_scope('backward'):
self.char_gru_cell_bw = tf.nn.rnn_cell.GRUCell(char_hidden_size)
# Set initial state of the cells to be zero
self._char_initial_state_fw = \
self.char_gru_cell_fw.zero_state(batch_size, tf.float32)
self._char_initial_state_bw = \
self.char_gru_cell_bw.zero_state(batch_size, tf.float32)
# Size before: batch-chrpad-chrvocabsize
# Size after: batch-chrvocabsize
chargruinput = [tf.squeeze(input_, [1]) \
for input_ in tf.split(1, char_num_steps, chargruinput)]
# Run the bidirectional rnn and get the corner results
_, output_state_fw, output_state_bw = \
tf.nn.bidirectional_rnn(self.char_gru_cell_fw,
self.char_gru_cell_bw,
chargruinput,
sequence_length=char_num_steps,
initial_state_fw=self._char_initial_state_fw,
initial_state_bw=self._char_initial_state_bw)
当我运行它时,我收到以下错误:
Traceback (most recent call last):
File "frontbackgru.py", line 409, in <module>
main()
File "frontbackgru.py", line 226, in main
config=my_config)
File "/home/xG/Code/4-RNN/1-simple-cnn-input-classifier/gru_model.py", line 265, in __init__
initial_state_bw=self._char_initial_state_bw)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 453, in bidirectional_rnn
sequence_length, scope=fw_scope)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 156, in rnn
state_size=cell.state_size)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 343, in _rnn_step
_maybe_copy_some_through)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 1331, in cond
_, res_f = context_f.BuildCondBranch(fn2)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 1230, in BuildCondBranch
r = fn()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 317, in _maybe_copy_some_through
lambda: _copy_some_through(new_output, new_state))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 1331, in cond
_, res_f = context_f.BuildCondBranch(fn2)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 1230, in BuildCondBranch
r = fn()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 317, in <lambda>
lambda: _copy_some_through(new_output, new_state))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 298, in _copy_some_through
return ([math_ops.select(copy_cond, zero_output, new_output)]
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 1769, in select
name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 704, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2262, in create_op
set_shapes_for_outputs(ret)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1702, in set_shapes_for_outputs
shapes = shape_func(op)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 1578, in _SelectShape
t_e_shape = t_e_shape.merge_with(c_shape)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 570, in merge_with
(self, other))
ValueError: Shapes (32, 50) and () are not compatible
现在,函数的输入bidirectional_rnn
是:
self.char_gru_cell_fw
char_hidden_size
:这是一个 GRUCell 实例,在这种情况下用整数值 50 初始化
self.char_gru_cell_bw
char_hidden_size
:这是一个 GRUCell 实例,在这种情况下用整数值 50 初始化
chargruinput
:这是一个长度为 30 的列表,包含形状为 [ batch_size
, charvocab
] 的张量,在本例中为 [32,256]
sequence_length
: 一个整数,表示展开单元格的数量,char_num_steps
在本例中为 30。
initial_state_fw
: 一个与 GRU 状态相同形状的零填充张量,在本例中为 [32,50]
initial_state_bw
: 一个与 GRU 状态相同形状的零填充张量,在本例中为 [32,50]
我尝试查看导致抛出 ValueError 异常的模块,但是有很多低级的东西很可能工作正常,看看我上周工作的 CNN 是如何工作的,没有任何问题。这让我觉得在低级方法之前,rnn
orrnn_cell
库中出现了我以前没有使用过的问题。
这似乎也很奇怪,因为错误与空形状有关(与我假设的标量而不是张量相关联),但我唯一能够更改的是bidirectional_rnn
函数参数中的标量是sequence_length
参数。我尝试忽略它并仅使用初始状态,反之亦然,但会弹出相同的错误。
有没有人有类似的问题?我的整个系统都因此而瘫痪,希望得到一些反馈。提前致谢