Subclassing nn.ModuleDict vs nn.Module

When should one subclass nn.ModuleDict over nn.Module?

for example, here,

is it better to use nn.ModuleDict whenever merging multiple neural networks?


class Y(nn.ModuleDict):
  def __init__(self):
    self['NetA'] = NetA()
    self['NetB'] = NetB()
def X(nn.Module):
  def __init__(self):
    self.modelone = NetA()
    self.modeltwo = NetB()

which is the preferred way?

It might depend on your use case, but nn.ModuleDict is just a container (dict), which stores modules and is used to register these modules properly inside a parent nn.Module.

Based on your code snippet, I would derive from nn.Module (your X class).