title: How to get the last index of model's prediction?
body: <p>I am new to PyTorch. I have a variable pred which has a list of a tensor.</p>
  <pre><code>print(pred)
  output: [tensor([[176.64380, 193.86154, 273.84702, 306.30405, 0.83492, 2.00000]])]
  </code></pre>
  <p>So I wanted to access the last element which is the class. I did that by first converting the list into a tensor.</p>
  <pre><code>x = torch.stack(pred)
  output: tensor([[[176.64380, 193.86154, 273.84702, 306.30405, 0.83492, 2.00000]]])
  </code></pre>
  <p>Now, how do I access the last element or is there any better/efficient way of doing this?</p>
  <p>EDIT: For further reference here's the code that performs classification task.</p>
  <pre><code>def classify_face(image):
      device = torch.device(&quot;cpu&quot;)
      img = process_image(image)
      print('Image processed')
      # img = image.unsqueeze_(0)
      # img = image.float()
      
      pred = model(img)[0]
      
      # Apply NMS
      pred = non_max_suppression(pred, 0.4, 0.5, classes = [0, 1, 2], agnostic = None )
      if classify:
          pred = apply_classifier(pred, modelc, img, im0s)
      #print(pred)
      
      model.eval()
      model.cpu()
      
      print(pred)
          
      # output = non_max_suppression(output, 0.4, 0.5, classes = class_names, agnostic = False)
          
      #_, predicted = torch.max(output[0], 1)
      #print(predicted.data[0], &quot;predicted&quot;)
  
      classification = torch.cat(pred)[:, -1]
      index = int(classification)
      print(names[index])
      return names[index]
  </code></pre>
  <p>During prediction pred consists of <code>x1</code>, <code>y1</code>, <code>x2</code>, <code>y2</code>, <code>conf</code>, and <code>class</code>.</p>
  <p>e.g. <code>pred = [tensor([[176.64380, 193.86154, 273.84702, 306.30405, 0.83492, 2.00000]])]</code></p>
  <p>If there are no predictions made by the model then <code>pred</code> is simply empty.</p>
  <p>e.g. <code>pred = [tensor([], size=(0, 6))]</code></p>
  <p>Presently my program stops prediction if it receives an empty tensor and throws an error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;WEBCAM_DETECT.py&quot;, line 168, in &lt;module&gt;
      label = classify_face(frame)
    File &quot;WEBCAM_DETECT.py&quot;, line 150, in classify_face
      index = int(classification)
  ValueError: only one element tensors can be converted to Python scalars
  </code></pre>
  <p>EDIT1:
  It seems to work when I check length of the pred but I get this error when there are two or more rows in tensor.</p>
  <pre><code>[tensor([[212.38568, 117.47020, 339.35773, 266.00513,   0.74144,   2.00000],
          [214.60651, 118.50694, 339.90192, 265.91696,   0.44277,   0.00000]])]
  #################
  #################
  Traceback (most recent call last):
    File &quot;WEBCAM_DETECT.py&quot;, line 172, in &lt;module&gt;
      label = classify_face(frame)
    File &quot;WEBCAM_DETECT.py&quot;, line 154, in classify_face
      index = int(classification)
  ValueError: only one element tensors can be converted to Python scalars
  </code></pre>
  <p>How do I make my program sort of just ignore if there are no predictions made at a certain frame and continue onto the next frame?</p>