I have a custom implementation of Conv2D and I want to add a custom value to this model. Essentially, I want to set this value at the model level and then have access to it within MyConv2D. I tried to do this by adding a new parameter to the model definition and then passing that value my layer, like this:
class custom_model(nn.Module):
  def __init__(self):
    super(custom_model, self).__init__()
    self.custom_value = torch.nn.Parameter(torch.zeros(1), requires_grad=False)
    self.conv_1 = MyConv2d((self.custom_value.item()), 1, 4, 5, stride=1, padding=0)
    ...

Then when I load the model, I first add this new parameter to my state_dict:
copied_state_dict = torch.load(PATH, map_location=device)
copied_state_dict['custom_value'] = torch.tensor([1])
custom_net.load_state_dict(copied_state_dict)

Not only does this seem like a really inelegant solution but it also doesn’t work! Because I initialize custom_value with 0, it always stays zero. And that’s the point where I’ve exhausted my knowledge of PyTorch!
So is there a nice way for me to set a property (parameter or otherwise) at a model level and have access to it in my own layer code? I am not planning to train this model. I just load weights from a pre-trained model and only run inference.

I am OK to have it as a parameter. If I can directly access it within the MyConv2D layer, that’s all I need.
If I can use some other property of a model (I’ve seen mention of a buffer but not sure if that is a better solution here). But I would like an easy way to set this property before I run the model, as I need to change this property for each run.

Thanks!