<built-in method float of Tensor object at 0x7fc85283d640>

hs99: <p>Hello…Needed some help…Am pretty new to pytorch <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>I have the CNN model and I wanted to evaluate it:</p>
<p>def evaluate_model(model, test_loader, criterion, best_model_name):<br>
model.load_state_dict(torch.load(best_model_name))</p>
<pre><code># Specify the heartbeat classes from above
classes = {
    0: 'N - Normal Beat', 
    1: 'S - Supraventricular premature or ectopic beat',
    2: 'V - Premature ventricular contraction', 
    3: 'F - Fusion of ventricular and normal beat', 
    4: 'Q - Unclassified beat'}

# track test loss
test_loss = 0.0
class_correct = list(0. for i in range(5))
class_total = list(0. for i in range(5))

model.eval()
# iterate over test data
for data, target in test_loader:
    # move tensors to GPU if CUDA is available
    if train_on_gpu:
        data, target = data.cuda(), target.cuda()
    # forward pass: compute predicted outputs by passing inputs to the model
    print(data.float)
    output = model(data.float())
    # calculate the batch loss
    loss = criterion(output, target.long())
    # update test loss 
    test_loss += loss.item()*data.size(0)
    # convert output probabilities to predicted class
    _, pred = torch.max(output, 1)
    # compare predictions to true label
    correct_tensor = pred.eq(target.data.view_as(pred))
    correct = np.squeeze(correct_tensor.numpy()) if not train_on_gpu else np.squeeze(correct_tensor.cpu().numpy())
    # calculate test accuracy for each object class
    for i in range(batch_size):
        label = target.data[i].int()
        class_correct[label] += correct[i].item()
        class_total[label] += 1

# average test loss
test_loss = test_loss/len(test_loader.dataset)
print('Test Loss: {:.6f}\n'.format(test_loss))

for i in range(5):
    if class_total[i] &gt; 0:
        print('Test Accuracy of %5s: %2d%% (%2d/%2d)' % (
            classes[i], 100 * class_correct[i] / class_total[i],
            np.sum(class_correct[i]), np.sum(class_total[i])))
    else:
        print('Test Accuracy of %5s: N/A (no training examples)' % (classes[i]))

print('\nTest Accuracy (Overall): %2d%% (%2d/%2d)' % (
    100. * np.sum(class_correct) / np.sum(class_total),
    np.sum(class_correct), np.sum(class_total)))
</code></pre>
<hr>
<p>When I run this code, instead of getting the desired output of accuracy of each class,<br>
Rather I get an output as shown:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/b/8b833126d6b1e55c10964752aa5c50825db0ac53.png" data-download-href="https://discuss.pytorch.org/uploads/default/8b833126d6b1e55c10964752aa5c50825db0ac53" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/8/b/8b833126d6b1e55c10964752aa5c50825db0ac53.png" alt="image" data-base62-sha1="jUbrEffMo2lAo5LhErTggMKbkTF" width="690" height="339" data-dominant-color="505257"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">784×386 41.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>How can I correct this??Plz help!!</p>

albanD: <p>Hi,</p>
<p>When you do <code>print(data.float)</code> this is accessing a method and not calling it. You should do <code>print(data.float())</code></p>

hs99: <p><a class="mention" href="/u/alband">@albanD</a> Thanks!!<br>
But I have one more query!<br>
This is my code to evaluate the model:</p>
<p>def evaluate_model(model, test_loader, criterion, best_model_name):<br>
model.load_state_dict(torch.load(best_model_name))</p>
<pre><code># Specify the heartbeat classes from above
classes = {
    0: 'N - Normal Beat', 
    1: 'S - Supraventricular premature or ectopic beat',
    2: 'V - Premature ventricular contraction', 
    3: 'F - Fusion of ventricular and normal beat', 
    4: 'Q - Unclassified beat'}

# track test loss
test_loss = 0.0
class_correct = list(0. for i in range(5))
class_total = list(0. for i in range(5))

model.eval()
# iterate over test data
for data, target in test_loader:
    # move tensors to GPU if CUDA is available
    if train_on_gpu:
        data, target = data.cuda(), target.cuda()
    # forward pass: compute predicted outputs by passing inputs to the model
    #print(data.float())
    output = model(data.float())
    # calculate the batch loss
    loss = criterion(output, target.long())
    # update test loss 
    test_loss += loss.item()*data.size(0)
    # convert output probabilities to predicted class
    _, pred = torch.max(output, 1)
    # compare predictions to true label
    correct_tensor = pred.eq(target.data.view_as(pred))
    correct = np.squeeze(correct_tensor.numpy()) if not train_on_gpu else np.squeeze(correct_tensor.cpu().numpy())
    # calculate test accuracy for each object class
    for i in range(batch_size):
        label = target.data[i].int()
        class_correct[label] += correct[i].item()
        class_total[label] += 1

# average test loss
test_loss = test_loss/len(test_loader.dataset)
print('Test Loss: {:.6f}\n'.format(test_loss))

for i in range(5):
    if class_total[i] &gt; 0:
        print('Test Accuracy of %5s: %2d%% (%2d/%2d)' % (
            classes[i], 100 * class_correct[i] / class_total[i],
            np.sum(class_correct[i]), np.sum(class_total[i])))
    else:
        print('Test Accuracy of %5s: N/A (no training examples)' % (classes[i]))

print('\nTest Accuracy (Overall): %2d%% (%2d/%2d)' % (
    100. * np.sum(class_correct) / np.sum(class_total),
    np.sum(class_correct), np.sum(class_total)))
</code></pre>
<p>When I run:<br>
evaluate_model(model_1, test_loader_1, criterion, ‘model_ecg_heartbeat_categorization_1.pt’)</p>
<p>I get the error <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/9/e94dd89cbbfffce4593950be00cffea54f5c246a.png" data-download-href="https://discuss.pytorch.org/uploads/default/e94dd89cbbfffce4593950be00cffea54f5c246a" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/e/9/e94dd89cbbfffce4593950be00cffea54f5c246a.png" alt="image" data-base62-sha1="xhU2bk2w0p39hvBWEvIw9YhAJgC" width="690" height="220" data-dominant-color="282D33"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">792×253 28.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Criterion is NLLLoss and batch size is 32…<br>
How do I correct this??Pls help !!<img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"> <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=12" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:" loading="lazy" width="20" height="20"> <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=12" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:" loading="lazy" width="20" height="20"> <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=12" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:" loading="lazy" width="20" height="20"></p>

albanD: <p>Looks like your target Tensor is too small as it has a batch size of 8. You should check why.</p>