Conditional Model Architectures

How can I make conditional model architectures?

I’m writing a GAN and currently I have two classes defined as:

class Generator(nn.Module):
    ...
class Discriminator(nn.Module):
    ...

but I want to have multiple architectures dependent on the size of my input eg:

if len(input) == 16:
    # Use 16x16 Discriminator and Generator

if len(input) == 64:
    # Use 64x64 Discriminator and Generator

...

Is there a best-practice way to do this? I could define classes for each of my inputs but that seems a bit inefficient.

If you would like to call to a specific model based on a condition, you would have to store these model instances somehow, no?
I’m not sure I understand the use case correctly, so please correct me.
Or would you like to use the same instance of your model with different paths inside the model?

My solution ended up being:

from architectures import (Discriminator16, Discriminator64, Discriminator128, Generator16, Generator64, Generator128)

 # Create the Discriminator
 if ndf == 16:
   netD = Discriminator16(ngpu).to(device)
 elif ndf == 64:
   netD = Discriminator64(ngpu).to(device)
 elif ndf == 128:
   netD = Discriminator128(ngpu).to(device)
...

Yes I was just wondering if it there was a better way of doing it. BTW you do a great job on these forums, your answers have helped me greatly!

1 Like