[Solved][Pytorch1.8] AttributeError: 'TanhBijector' object has no attribute 'domain'

Hi I’m training DRL. I met a problem showing below after updating to torch 1.8.1+cu111. I’ve used the previous nightly version before. It was okay. Is it related to version problem? Thanks…

File "/home/jeff/VirtualEnv/openai_py38/lib/python3.8/site-packages/torch/distributions/transforms.py", line 287, in domain
    domain = self.parts[0].domain
AttributeError: 'TanhBijector' object has no attribute 'domain

And this is my class using “torch.distributions.Transform”. I’ve tried to add 2 variables, self.domain and self.codomain. Then, it seems that it should be defined by other terms.

class TanhBijector(torch.distributions.Transform):
    def __init__(self):
        super().__init__()
        self.bijective = True
        #self.domain = [0,1]
        #self.codomain = [0,1]

    @property
    def sign(self): return 1.

    def _call(self, x): return torch.tanh(x)

    def _inverse(self, y: torch.Tensor):
        y = torch.where(
            (torch.abs(y) <= 1.),
            torch.clamp(y, -0.99999997, 0.99999997),
            y)
        y = atanh(y)
        return y

    def log_abs_det_jacobian(self, x, y):
        return 2. * (np.log(2) - x - F.softplus(-2. * x))

It seems that self.domain and self.codomain are currectly commented out. So your class won’t see them. Try uncommenting ?

#self.domain = [0,1]
#self.codomain = [0,1]

self.domain and self.codomain are added by myself to try its function. It seems that they are not normal type variables.

So you add domain and codomain to a class instance after instantiating it?

1 Like

It’s from torch/distibutions/transforms.py

1 Like

Thanks AlphaBetaGamma96. I’ve find a solution which I can continue to train it when using torch1.8.1+cu111. The following is my solution after declaring these 2 variables to access the Constraint().

class TanhBijector(torch.distributions.Transform):
    def __init__(self):
        super().__init__()
        self.bijective = True
        self.domain = torch.distributions.constraints.Constraint()
        self.codomain = torch.distributions.constraints.Constraint()
3 Likes