Add additional scalar parameter to pytorch model gives runtimeerror

Viewed 38

I'm trying to add a scalar parameter to my model (code too complex to attach), but it is effectively like:

class WholeModel:
    def __init__(...):
        self.new_parameter = Parameter(torch.scalar_tensor(0.1, requires_grad=True))
        self.model = self.make_model()
        

    def make_model(self):
        d = distribution()  # returns a Distribution which is a Module
        d = transform_distribution(d, self.new_parameter)
        d.register_parameter(name='new', param=self.new_parameter)
        return d

However, I run into this error RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.

If I change self.new_parameter = Parameter(torch.scalar_tensor(0.1)) to self.new_parameter = torch.scalar_tensor(0.1) and remove the register_parameter, then it compiles and runs (but then obviously its not then learning the parameter).

I've also tried using a tensor rather than a scalar_tensor but this also doesn't work. The error occurs with/without requires_grad.

Any ideas? It really is just a simple addition to a blackbox model. Thanks

0 Answers
Related