Freeze a gru -- getting error that weight does not exist


I want to freeze my GRU after a certain amount of training. I have a ‘model’ Module object which has a gru in it. My code looks like this:

def freeze_encoding(self, do_freeze=True):
    self.model.gru.weight_ih.requires_grad = not do_freeze
    self.model.gru.weight_hh.requires_grad = not do_freeze
    if self.model.gru.bias == True:
        self.model.gru.bias_ih.requires_grad = not do_freeze
        self.model.gru.bias_hh.requires_grad = not do_freeze
    print('freeze encoding')

This code throws an error the weight_ih doesn’t exist. What would be the suggested way to do this right? Thanks.

I’m trying this:

def freeze_encoding(self, do_freeze=True):
    for weight in self.model.parameters():
        weight.requires_grad = not do_freeze

    if do_freeze: print('freeze encoding')

I think it would operate on the whole model and the gru I’m interested in is part of that.