ValueError:两个结构没有相同数量的元素 [英] ValueError: The two structures don't have the same number of elements

查看:682
本文介绍了ValueError:两个结构没有相同数量的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.但是我得到以下错误:

This is my code for a GRU for input of dimension 100x196x50, it should be unpacked along the second dimension (that is 196). hidden_state_size is 50, batch_size is 100. However I get the following error:

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>).

有什么线索可以解决这个问题吗?

Any clue how to resolve this?

推荐答案

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

Hello I had the same problem, I tried to do this:

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

这给了我类似的错误消息:

This gave me a similar error message:

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显式解决了这个问题:

I resolved this by making the dtypes explicit:

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

这篇关于ValueError:两个结构没有相同数量的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆