TypeError: 'GECorrection' object is not iterable

Dear all :slight_smile:

I have initialized a class as (nn.Module) named ‘GECorrection’, and I’m trying to call it inside another one.

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.

Any suggestions to start fixing this issue?

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.

Please notice how an object derived from the nn.Module base class is instantiated and called.

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)

This should work now.

Thank you so much Mr. hadrsha_g it’s working now. :slight_smile:

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 this 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!

1 Like