I am trying to port pytorch code to tf2 code for this repo. The overall logic of the code is as follows: