TypeError: 'GECorrection' object is not iterable

Aiman_Mutasem-bellh: <p>Dear all <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>I have initialized a class as (nn.Module) named ‘GECorrection’, and  I’m trying to call it inside another one.</p>
<p>I got the TypeError: ‘GECorrection’ object is not iterable. I spent a lot of time trying to find the problem to work on, but I failed.</p>
<p>Any suggestions to start fixing this issue?</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
 
from heinsen_routing import Routing

class GECorrection(nn.Module):

    def __init__(self, a, mu, d_cov, d_inp, d_out, n_out):
        super().__init__()
        
        self.d_inp = d_inp # int value 
        self.d_out = d_out # int value 
        self.n_out = n_out # int value 
        self.d_cov = d_cov # int value 
        
        
        self.routings = nn.Sequential(Routing(d_cov=d_cov, d_inp=d_inp, d_out=d_out, n_out=n_out))
        #self.routings = Routing(d_cov = d_cov, d_inp = d_inp, d_out = d_out, n_out = n_out)
        
        #-----------------------------------------------------------------------------------------
        # Until here everything responds well 
        # 'self.routings' in iterable (Iterable = True)
        #-----------------------------------------------------------------------------------------
        
        
    def forward(self, a, mu):
        #-----------------------------------------------------------------------------------------
        # forward can't response even for empty tensors I get the same error 
        # TypeError: 'GECorrection' object is not iterable
        #-----------------------------------------------------------------------------------------

        for Routing in self.routings:
            
            a, mu, sig2 = Routing(a, mu)

        return a, mu, sig2

a_inp = torch.randn(128,  29)       
mu_inp = torch.randn(128, 29, 8, 32)

a, mu, sig2 = GECorrection(a_inp, mu_inp, 8, 32, 32, 29)

TypeError: 'GECorrection' object is not iterable.

</code></pre>

harsha_g: <p>Please notice how an object derived from the <code>nn.Module</code> base class is instantiated and called.</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
 
from heinsen_routing import Routing

class GECorrection(nn.Module):

    def __init__(self, d_cov, d_inp, d_out, n_out):
        super().__init__()
        
        self.d_inp = d_inp # int value 
        self.d_out = d_out # int value 
        self.n_out = n_out # int value 
        self.d_cov = d_cov # int value 
      
        self.routings = nn.Sequential(Routing(d_cov=d_cov, d_inp=d_inp, d_out=d_out, n_out=n_out))
        #self.routings = Routing(d_cov = d_cov, d_inp = d_inp, d_out = d_out, n_out = n_out)
   
    def forward(self, a, mu):
        for Routing in self.routings:
            a, mu, sig2 = Routing(a, mu)

        return a, mu, sig2

a_inp = torch.randn(128,  29)       
mu_inp = torch.randn(128, 29, 8, 32)

myObject = GECorrection(8, 32, 32, 29)
a, mu, sig2 = myObject(a_inp, mu_inp)
</code></pre>
<p>This should work now.</p>

Aiman_Mutasem-bellh: <p>Thank you so much Mr. hadrsha_g it’s working now. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

harsha_g: <p>I don’t see anything wrong with your implementation. You might want to perform some hyperparameter optimization to see if you get any better results. Also, I am sure you may have across <a href="https://arxiv.org/pdf/1909.00188.pdf" rel="nofollow noopener">this</a> work. See if there’s anything in there that can help engineer your model towards a better performance. Sorry for not being much helpful. Good Luck!</p>