【问题标题】:TensorFlow Bidirectional GRU returns ValueError because of <supposedly> incorrect shape由于 <supposedly> 形状不正确,TensorFlow 双向 GRU 返回 ValueError
【发布时间】:2016-08-18 04:03:29
【问题描述】:

我正在使用 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:这是一个 GRUCell 实例,初始化为整数值char_hidden_size,在本例中为 50

self.char_gru_cell_bw:这是一个 GRUCell 实例,初始化为整数值char_hidden_size,在本例中为 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或rnn_cell库出了点问题。

这似乎也很奇怪,因为错误与空形状有关(与我假设的标量而不是张量相关联),但我唯一能够更改的是 bidirectional_rnn 函数参数中的标量是sequence_length 参数。我尝试忽略它并仅使用初始状态,反之亦然,但会弹出相同的错误。

有人遇到过类似的问题吗?我的整个系统都因此而瘫痪,希望得到一些反馈。提前致谢

【问题讨论】:

    标签: tensorflow bidirectional labeling gated-recurrent-unit


    【解决方案1】:

    找出问题所在 - 参数 sequence_length 实际上应该是每个批次的长度为 batch_size 的整数列表,而不是整数本身。轻松解决,感谢您的参与:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-12
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 2019-07-02
      • 2021-03-10
      相关资源
      最近更新 更多