title: Faster method of computing confusion matrix?
body: <p>I am computing my confusion matrix as shown below for image semantic segmentation which is a pretty verbose approach:</p>
  
  <pre><code>def confusion_matrix(preds, labels, conf_m, sample_size):
      preds = normalize(preds,0.9) # returns [0,1] tensor
      preds = preds.flatten()
      labels = labels.flatten()
      for i in range(len(preds)):
          if preds[i]==1 and labels[i]==1:
              conf_m[0,0] += 1/(len(preds)*sample_size) # TP
          elif preds[i]==1 and labels[i]==0:
              conf_m[0,1] += 1/(len(preds)*sample_size) # FP
          elif preds[i]==0 and labels[i]==0:
              conf_m[1,0] += 1/(len(preds)*sample_size) # TN
          elif preds[i]==0 and labels[i]==1:
              conf_m[1,1] += 1/(len(preds)*sample_size) # FN 
      return conf_m
  </code></pre>
  
  <p>In the prediction loop:</p>
  
  <pre><code>conf_m = torch.zeros(2,2) # two classes (object or no-object)
  for img,label in enumerate(data):
      ...
      out = Net(img)
      conf_m = confusion_matrix(out, label, len(data))
      ...
  </code></pre>
  
  <p>Is there a faster approach (in PyTorch) to efficiently calculate the confusion matrix for a sample of inputs for image semantic segmentation?</p>