How to customize the number of layers in pytorch?

I want to feed the number of layers and their parameters in the input, so that I can vary the structure of the network and number of layers. How could I implement this in pytorch? In tensorflow you can just create any number of layers but in pytorch this seems not so obvious.

You can make a class that subclasses nn.module (more info here). It could take in nlayers as a constructor argument, and use that to determine what layers to build in the constructor.