The PyTorch add_module() function
• deep-learning • PermalinkI have been building some bespoke PyTorch models, and have just been
stung by a bug; it turns out that using the add_module()
method is
sometimes critical to making a PyTorch model work. Without this
method, the program may just crash, but might also just about work but
give completely meaningless results.
Though there do not seem to be any hints about this in the
documentation, it seems that PyTorch determines the layers or others
Module
s used in a particular Module
by looking at the type of
object stored in each member of the Module
. And if that object is
not a Module
, PyTorch does not recognise it and will not
backpropagate through it.