How to assign a module to a network module without it being added to parameters?

I want to do something like:

class Foo(nn.Module):
    def __init__(self, featurizer):
        self.featurizer = featurizer

… so I want to use featurizer in the forward method. But I dont want featurizer (which is derived from nn.Module) to have its parameters included in the .parameters() of a Foo object.

How to achieve this? I know I could do eg assign a list to Foo, and then add the featurizer to this list, but that seems a bit … convoluted. Wodnering if there is some simpler way?

(edit: for now, I’ve done exactly the create a list thing:

    self.featurizer_l = [featurizer]

and in forward:

    x = self.featurizer_l[0](x)

Can you use a Variable to represent featurizer? A variable in not included in the parameter generator.

Depending on your setup, the cleanest way might be to code the module in a way that you can use register_buffer similar to BatchNorm and friends do for running statistics.
If that is not an option, I think the cleanest interface would be to pass in the Module’s __call__ method and store and call that.

Best regards


1 Like