__setattr__ unexpected result

Hi ! :slight_smile:

In the context of a ResNet-like network, I wanted to differentiate blocks and layers within those blocks by putting a label on the bloc/layer. My idea to address this issue was to first make use of the block_x.apply() like so for the blocks :

self.block1.apply(lambda m: m.__setattr__('block_num', 1))

It however fails and at runtime when trying this :

for m in model.modules():
      if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
        if not (m.block_num, m.layer_num) in self.combinations:

With this message :
AttributeError: ‘Conv2d’ object has no attribute ‘block_num’

While this fail, the model didn’t fire any error when the apply function was running. Any idea on how to tackle this issue ?