Loop in tensorflow


I changed my question to explain my issue better:

I have a function: output_image = my_dunc(x) that x should be like (1, 4, 4, 1)

Please help me to fix the error in this part:

out = tf.Variable(tf.zeros([1, 4, 4, 3])) index = tf.constant(0) def condition(index): return tf.less(index, tf.subtract(tf.shape(x)[3], 1)) def body(index): out[:, :, :, index].assign(my_func(x[:, :, :, index])) return tf.add(index, 1), out out = tf.while_loop(condition, body, [index])

ValueError: The two structures don't have the same nested structure. First structure: type=list str=[] Second structure: type=list str=[<tf.Tensor 'while_10/Add_3:0' shape=() dtype=int32>, <tf.Variable 'Variable_2:0' shape=(1, 4, 4, 3) dtype=float32_ref>] More specifically: The two structures don't have the same number of elements. First structure: type=list str=[<tf.Tensor 'while_10/Identity:0' shape=() dtype=int32>]. Second structure: type=list str=[<tf.Tensor 'while_10/Add_3:0' shape=() dtype=int32>, <tf.Variable 'Variable_2:0' shape=(1, 4, 4, 3) dtype=float32_ref>]

I tested my code and I can get result from out = my_func(x[:, :, :, i]) with different values for i and also while_loop works when I comment the line out[:, :, :, index].assign(my_func(x[:, :, :, index])). Something is wrong in that line.



I understand that there is no for-loop and so on and just while, why?


Control structures are hard to get right and hard to optimize. In your case, what if the next example in the same batch has 5 channels. You would need to run 5 loop iterations and either mess up or waste compute resources for the first example with only 3 channels.

You need to think what exactly you are trying to achieve. Commonly you would have different weights for each channel so the system can't just create them out of thin air, they need to be trained properly.

If you just want to apply the same logic 3 times just re-arrange your tensor to be (3, 4, 4, 1). You get 3 results and you do what you want with them.

Usually when you actually need for loops (when handling sequences) you pad the examples so that they all have the same length and generate a model where the loop in unrolled (you would have 3 different operations, one for each iteration of the loop). Look for dynamic_rnn or static_rnn (first one can handle different lengths for each batch).



I understand that there is no for-loop and so on and just while, why?


According to <a href="http://download.tensorflow.org/paper/white_paper_tf_control_flow_implementation_2017_11_1.pdf" rel="nofollow">Implementation of Control Flow in TensorFlow</a>


They should fit well with the dataflow model of TensorFlow, and should be amenable to parallel and distributed execution and automatic differentiation.


I think distributed data flow graphs and <em>Automatic differentiation</em> across devices could have been the constraints leading to the introduction of very few such loop primitives.

There are several diagrams in this doc. that distributed computing experts can understand better. A more thorough explanation is beyond me.


  • Perl: Variable is printed as SCALAR(0x7faf2b804240)
  • Meteor variable scope (global, client, server, or all?)
  • What's the best way to collapse sparse data into single rows in R?
  • How to make a user wait with Laravel
  • Where in the relevant specification is it documented that some comments in a SQL script are, in fact
  • Send the string to its hex equivalent
  • How to remove the dot in to_char if the number is an integer
  • Python: Split a String Field into 3 Separate Fields using Lambda
  • CS1703: In Xamarin.Droid, should I use the .Net Standard windowsruntime.dll located in Mono.Framewor
  • how do you obtain the address of an instance after overriding the __str__ method in python
  • Generating anchors with PyYAML.dump()?
  • C function strchr - How to calculate the position of the character?
  • How to enable mapping the private property of the entity
  • Get Currently Active User in Android
  • Exception creating JSON with LINQ
  • Owin Authentication and claims in asp.net how to access user data
  • msbuild create itemgroup from property group
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • Parse a date string in a specific locale (not timezone!)
  • Is there a javascript serializer for JSON.Net?
  • Getting last autonumber in access
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Where to put my custom functions in Wordpress?
  • How to handle AllServersUnavailable Exception
  • When should I choose bucket sort over other sorting algorithms?
  • 0x202A in filename: Why?
  • Unanticipated behavior
  • Buffer size for converting unsigned long to string
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • How do I use LINQ to get all the Items that have a particular SubItem?