AttributeError: 'GraphNetwork' object has no attribute 'layer_number'

username: danielmanu93
cooked: <p>I’m trying to load a created GNN model into an RNN controller and train to get the accuracy but when I run the RNN controller code I get this error “AttributeError: ‘GraphNetwork’ object has no attribute ‘layer_number’” even though “layer_number” is defined in the GNN model code. Any ideas on how to solve this will be greatly appreciated. thanks in advance. Below is a part of my GNN code where I’m getting the error.<br>
The error is from this line “resultant_length = self.layer_number * state_number”</p>
<p>class GraphNetwork(torch.nn.Module):</p>
<pre><code>def __init__(self, actions, num_features, num_labels, dropout=0.6, multi_label=False, batch_normal=False,
             residual=False, state_number=5, layer_number=3): #Hidden dimension absent: state number = 5
    
    # parameters =&gt; actions:
    # parameters =&gt; multi_labels:

    super(GraphNetwork, self).__init__()
    
    # args
    self.num_labels = num_labels
    self.dropout = dropout
    self.multi_label = multi_label
    self.num_features = num_features
    self.residual = residual
    
    # check architecture of GNN
    self.compute_actions(actions, state_number)
    self.layer_number = layer_number
    
    # layer module
    self.construct_model(actions, batch_normal, dropout, num_features, num_labels, state_number)

def construct_model(self, actions, batch_normal, dropout, num_features, num_labels, state_number):
    self.gates = torch.nn.ModuleList()
    self.layers = torch.nn.ModuleList()
    self.prediction = None
    self.construct_hidden_layers(actions, batch_normal, dropout, self.layer_number, num_features, num_labels, state_number)

def compute_actions(self, actions, state_number):
    state_length = len(actions)
    if not self.residual:
        resultant_length = self.layer_number * state_number
    else:
        resultant_length = sum([state_number+i for i in range(1,self.layer_number+1)])
    if state_length != resultant_length:
        raise RuntimeError("False Input : Unmatched input")
    if actions[-1] == self.num_label: #last action number = no. of labels; pass
        pass
    else:
        raise RuntimeError("False architecture") 
</code></pre>
<p>Below is the part of my controller code that loads the created GNN.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/4/4/44b66bc556699b572ac302dd2cf2cf76a8db7d72.jpeg" alt="1" data-base62-sha1="9NRiNg8nwA78TwGOn1RakSV7eHU" width="684" height="443"></p>

username: ptrblck
cooked: <p>The error is raised, because you are defining <code>self.layer_number</code> after calling <code>self.compute_actions</code>, which already tries to access this attribute.<br>
Move the assignment of <code>self.layer_number</code> before calling <code>compute_actions</code> and it should work.</p>

username: danielmanu93
cooked: <p>Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a><br>
I already replaced that line of code some days back and It worked fine but I have a follow up issue which I will really appreciate if you can assist.</p>

username: Gamboa147
cooked: <p>‘Tensor’ object has no attribute ‘initializer’. And it seems there is a Tensor object in tf.local_variables(), and the code initializing it causes the error.</p>
<p><a href="https://www.citefast.us/" rel="noopener nofollow ugc">CiteFast</a></p>

username: ptrblck
cooked: <p><code>tf.local_variables()</code> seems to be a TensorFlow operation, so I’m unsure where you are seeing this error.</p>