Tensor baskets with various shapes

I have a problem returning a tuple of two variables v , wt , where v has shape=(20,20) and wt has shape=(1,) . wt is a variable that is a weight value. I want to return a tuple (v, wt) inside a map_fn

my code looks a bit close to this

 tf.map_fn(fn, nonzeros(Matrix, dim, row)) nonzeros(Matrix, dim, row) returns a (index, value) 

fn will return a tuple, but I get an error:

 ValueError: The two structures don't have the same number of elements. First structure: <dtype: 'int64'>, second structure: (<tf.Tensor 'map_2/while/while/Exit_1:0' shape=(20,) dtype=float32>, <tf.Tensor 'map_2/while/Sub:0' shape=() dtype=int64>). 
+5
source share
1 answer

Here you return the results of the tf.while loop. the tf.while returns a tuple of several values, in your case we see that your while loop returned the value of interest and the counter value as a tuple.

 (<tf.Tensor 'map_2/while/while/Exit_1:0' shape=(20,) dtype=float32>, <tf.Tensor 'map_2/while/Sub:0' shape=() dtype=int64>) 

What you want to pass from map_fn is perhaps only the first of these two values. Therefore, in code that you have not shown here, you should have something like:

 value, counter = tf.while(...) return value 

What do you have:

 return tf.while(...) 

So the error you see complains that <dtype: 'int64'> does not match the tuple that you are passing. When you fix the while loop, you will compare <dtype: 'int64'> with <tf.Tensor 'map_2/while/while/Exit_1:0' shape=(20,) dtype=float32> , which is supposedly (20,) and will match (although you may encounter an int / float problem).

+1
source

Source: https://habr.com/ru/post/1266002/


All Articles