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

username: 111367
cooked: <p>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…</p>
<pre><code class="lang-auto">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
</code></pre>
<p>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.</p>
<pre><code class="lang-auto">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) &lt;= 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))
</code></pre>

username: AlphaBetaGamma96
cooked: <p>It seems that self.domain and self.codomain are currectly commented out. So your class won’t see them. Try uncommenting ?</p>
<pre><code class="lang-auto">#self.domain = [0,1]
#self.codomain = [0,1]
</code></pre>

username: 111367
cooked: <p>self.domain and self.codomain are added by myself to try its function. It seems that they are not normal type variables.</p>

username: AlphaBetaGamma96
cooked: <p>So you add <code>domain</code> and <code>codomain</code> to a class instance after instantiating it?</p>

username: 111367
cooked: <p>It’s from torch/distibutions/transforms.py<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/8/089ec31c393b0bd1ede62b86a8f4a59c62fb1d15.png" data-download-href="https://discuss.pytorch.org/uploads/default/089ec31c393b0bd1ede62b86a8f4a59c62fb1d15" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/8/089ec31c393b0bd1ede62b86a8f4a59c62fb1d15.png" alt="image" data-base62-sha1="1efXTGFBTMjdPKn6T4VhiaD9p3f" width="690" height="206" data-dominant-color="44233A"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">729×218 39.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: 111367
cooked: <p>Thanks <a href="https://discuss.pytorch.org/u/AlphaBetaGamma96">AlphaBetaGamma96</a>. 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().</p>
<pre><code class="lang-auto">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()
</code></pre>