【问题标题】:ValueError: The two structures don't have the same number of elementsValueError:两个结构的元素数量不同
【发布时间】:2017-08-04 06:10:06
【问题描述】:
with tf.variable_scope('forward'):
  cell_img_fwd = tf.nn.rnn_cell.GRUCell(hidden_state_size, hidden_state_size)
  img_init_state_fwd = rnn_img_mapped[:, 0, :]
  img_init_state_fwd = tf.multiply(
      img_init_state_fwd, 
      tf.zeros([batch_size, hidden_state_size]))
  rnn_outputs2, final_state2 = tf.nn.dynamic_rnn(
      cell_img_fwd, 
      rnn_img_mapped, 
      initial_state=img_init_state_fwd, 
      dtype=tf.float32)

这是我用于输入尺寸为 100x196x50 的 GRU 的代码,它应该沿第二维(即 196)解包。 hidden_state_size 是 50,batch_size 是 100。但是我收到以下错误:

ValueError: The two structures don't have the same number of elements.
First structure: Tensor("backward/Tile:0", shape=(100, 50), dtype=float32), 
second structure: 
  (<tf.Tensor 'backward/bwd_states/while/GRUCell/add:0' shape=(100, 50) dtype=float32>, 
   <tf.Tensor 'backward/bwd_states/while/GRUCell/add:0' shape=(100, 50) dtype=float32>).

有什么办法解决这个问题吗?

【问题讨论】:

    标签: tensorflow recurrent-neural-network gated-recurrent-unit


    【解决方案1】:

    您好,我遇到了同样的问题,我尝试这样做:

    highest = tf.map_fn(lambda x : (-x, x), indices)
    

    这给了我一个类似的错误信息:

    ValueError: The two structures don't have the same number of elements.
    
    First structure (1 elements): <dtype: 'int32'>
    
    Second structure (2 elements): (<tf.Tensor 'map/while/Neg:0' shape=() dtype=int32>, <tf.Tensor 'map/while/TensorArrayReadV3:0' shape=() dtype=int32>)
    

    我通过明确 dtypes 解决了这个问题:

    highest = tf.map_fn(lambda x : (-x, x), indices, dtype=(tf.int32, tf.int32))
    

    【讨论】:

    • 你救了我的命!
    • 感谢您的 dtypes 显式方法,它对我帮助很大。
    • 谢谢,我可以补充一下,它现在实际上已经写在文档中了: > 如果它与 elems 的数据类型不同,用户必须提供 dtype。 (tensorflow.org/api_docs/python/tf/map_fn)
    猜你喜欢
    • 2018-05-29
    • 1970-01-01
    • 2020-02-19
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多