Local variable 'x_bs' referenced before assignment

pyg50: <p>I want to build a multi_input model of CNN, but the definition in the function does not work<br>
I get the error local “variable ‘x_bs’ referenced before assignment”.</p>
<p>The inputs are Image and two other metadata (1 dimension, 26 dimensions),  so 3 variables, the objective variable is continuous data, and want to build a FineTuning by  ResNet50.</p>
<p><strong>Here is my code</strong></p>
<pre><code class="lang-auto"># dataloader
df_train = torch.utils.data.TensorDataset(x_train,x_bs_train, x_cate_train, y_train)
df_val = torch.utils.data.TensorDataset(x_val,x_bs_val, x_cate_val, y_val)

batch_size=32

train_dataloader = torch.utils.data.DataLoader(df_train, batch_size=batch_size, shuffle=True)
val_dataloader = torch.utils.data.DataLoader(df_val, batch_size=batch_size, shuffle=False)

#model
class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        # pictures
        self.resnet = nn.Sequential(*list(models.resnet50(pretrained=True).children())[:-1])
        self.image_fc = nn.Linear(2048, 256)
        # metadata：bs 1dim, cate 26 dims
        self.bs_fc = nn.Linear(1, 256)
        self.cate_fc = nn.Linear(26, 256)
        self.all_fc = nn.Linear(256*3, 256*3) 
        self.dropout = nn.Dropout(0.25)
        # last_FC
        self.last_fc = nn.Linear(256, 1)

    def forward(self, image, bs, cate):
        # pictures_features
        x_img = self.resnet(image).view(-1, 2048) 
        x_img = F.relu(self.image_fc(x_img))
        # metadata_features
        x_bs = F.relu(self.bs_fc(x_bs))
        x_cate = F.relu(self.bs_fc(x_cate))
        # concat,relu, dropout
        x = torch.cat([x_img, x_bs, x_cate], 1)
        x = F.relu(self.all_fc(x))
        x = self.dropout(x)
        # last_FC
        y = self.last_fc(x)
        return y

model = MyModel()

#Check if the model behaves
for x_train,x_bs_train, x_cate_train, y_train in train_dataloader:
    y_pred = model(x_train,x_bs_train,x_cate_train)
</code></pre>
<p><strong>Error</strong></p>
<pre><code class="lang-auto">UnboundLocalError                         Traceback (most recent call last)
&lt;ipython-input-15-181704a40564&gt; in &lt;module&gt;
      1 for x_train,x_bs_train, x_cate_train, y_train in train_dataloader:
----&gt; 2     y_pred = model(x_train,x_bs_train,x_cate_train)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--&gt; 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

&lt;ipython-input-13-3a3ca0de36f3&gt; in forward(self, image, bs, cate)
     20         x_img = F.relu(self.image_fc(x_img))
     21         # metadata_features
---&gt; 22         x_bs = F.relu(self.bs_fc(x_bs))
     23         x_cate = F.relu(self.bs_fc(x_cate))
     24         # concat,relu, dropout

UnboundLocalError: local variable 'x_bs' referenced before assignment
</code></pre>
<p>I notice there is any mismatch between global and local variables, but I don’t know how to fix it.</p>

albanD: <aside class="quote no-group" data-username="pyg50" data-post="1" data-topic="71161">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pyg50/48/21022_2.png" class="avatar"> pyg50:</div>
<blockquote>
<p>x_bs = F.relu(self.bs_fc(x_bs))</p>
</blockquote>
</aside>
<p>In this line in your forward, you use <code>x_bs</code> before setting it. So I guess this is why you see this error no?</p>

pyg50: <p>Thank you. It was different from the name of the definition argument, solved this problem. We will assemble one by one!</p>