Solved by InnovArul in post #2 
              
              
                It seems exec can’t work with locals(). 

The below code works though. 
for layer in range(1, len(num_units)):
    x = F.relu(getattr(self, f'conv{layer}')(x)