Hi everyone,
I am trying to train a CNN to recognize age, race, and gender using the UTKFace Dataset and was capable of training the model for gender using MSE Loss, but I didn’t like the results (regression for a classification issue). The thing is, when I tried to switch my loss function to BCELoss, I get this error which I really don’t understand after going through this post and this other one. I’ll paste some pictures of my code below, any sort of guidance is really appreciated : - )
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.cuda()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 94 * 94, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84,4) # 4 bc batch size
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 94 * 94)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Net()
net.to(torch.device("cuda:0"))
#loss_function and optimizer
criterion = nn.BCELoss()
optimizer = optim.Adam(net.parameters())
#Training the network
for epoch in range(10): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# get the inputs; data is a list of [inputs, labels]
inputs, _, labels = data
#getting the gender info from labels
genders = [train_map[label] for label in labels]
# zero the parameter gradients
optimizer.zero_grad()
labels = torch.Tensor(genders)
labels = torch.reshape(labels, (4,))
# forward + backward + optimize
outputs = net(inputs.cuda())
outputs = torch.reshape(outputs, (4, ))
loss = criterion(outputs, labels.cuda())
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % 500 == 0: # print every 2000 mini-batches
print('[%d, %5d] loss: %.8f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training')
RuntimeError Traceback (most recent call last)
<ipython-input-12-e3c4d5fc0ef2> in <module>
20
21 loss = criterion(outputs, labels.cuda())
---> 22 loss.backward()
23 optimizer.step()
24
~/.local/lib/python3.8/site-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
219 retain_graph=retain_graph,
220 create_graph=create_graph)
--> 221 torch.autograd.backward(self, gradient, retain_graph, create_graph)
222
223 def register_hook(self, hook):
~/.local/lib/python3.8/site-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
128 retain_graph = create_graph
129
--> 130 Variable._execution_engine.run_backward(
131 tensors, grad_tensors_, retain_graph, create_graph,
132 allow_unreachable=True) # allow_unreachable flag
RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)`