Hello dear all,
I have two different classes(binary classification) and i am trying to calculate AUROC, Accuracy and plot ROC. However, I could not understand clearly how ROC graphs are plotted. I am sharing the code and network with you.
NETWORK:(class Net(nn.Module))
def __init__(self):
super(Net, self).__init__()
self.pool = nn.MaxPool2d(2, 2)
self.conv1 = nn.Conv2d(3, 10, 5)
self.conv2 = nn.Conv2d(10, 20, 5)
self.fc1 = nn.Linear(20 * 5 * 5, 256)
self.fc2 = nn.Linear(256, 2)
def forward(self, x):
# print x.size()
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 20*5*5) # flatten the sensor
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
TRAIN:(def train())
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
inputs, labels = data
inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
TEST:(def test())
for data in testloader:
images, labels = data
outputs = net(Variable(images.cuda()))
_, predicted = torch.max(outputs.data, 1)
c = (predicted == labels.cuda()).squeeze().cpu().numpy()
y_score.append(outputs.cpu())
y.append(labels.cpu().numpy())
Above, I am trying to add y and y_scores to array. Then I am calculating roc and accuracy like below. In the above code “y_score.append(outputs.cpu())” this line give an error. But my main problem is not actually this. As I said before, I could not be sure whether this method is true or not when determining auroc.
fpr, tpr, _ = roc_curve(y, y_score)
roc_auc = auc(fpr, tpr)
print "average_precision_score : " + str(average_precision_score(y, y_score))
print "roc_auc_score : " + str(roc_auc_score(y, y_score))
print "accuracy : " + str(accuracy_score(y, y_score))
Thank you all