Local variable 'x_bs' referenced before assignment

I want to build a multi_input model of CNN, but the definition in the function does not work
I get the error local “variable ‘x_bs’ referenced before assignment”.

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.

Here is my code

# 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)


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)

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)


UnboundLocalError                         Traceback (most recent call last)
<ipython-input-15-181704a40564> in <module>
      1 for x_train,x_bs_train, x_cate_train, y_train in train_dataloader:
----> 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:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

<ipython-input-13-3a3ca0de36f3> in forward(self, image, bs, cate)
     20         x_img = F.relu(self.image_fc(x_img))
     21         # metadata_features
---> 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

I notice there is any mismatch between global and local variables, but I don’t know how to fix it.

In this line in your forward, you use x_bs before setting it. So I guess this is why you see this error no?

Thank you. It was different from the name of the definition argument, solved this problem. We will assemble one by one!