Update: I was able to solve this using built-in Caffe2 operators. The While and If operators, which I wasn’t aware of, were extremely useful: here is a great tutorial that helped me understand how to use them: https://github.com/caffe2/tutorials/blob/master/Control_Ops.ipynb 3