Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

tensorflow - ValueError: The two structures don't have the same number of elements

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)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

I resolved this by making the dtypes explicit:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...