Accuracy calculation yields constant 0.0

ky_Pa: <p>训练和测试的时候这个Avg Accuracy一直是0%，acc有值，这是怎么回事大神们<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/0/3050f834ce34f6088ea7c79a3d8a94a9d841d528.png" data-download-href="https://discuss.pytorch.org/uploads/default/3050f834ce34f6088ea7c79a3d8a94a9d841d528" title="微信图片_20200216110741"><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/0/3050f834ce34f6088ea7c79a3d8a94a9d841d528.png" alt="微信图片_20200216110741" data-base62-sha1="6TqouAAGmLzQCA0uQHG9hpfyhe0" width="690" height="180" data-dominant-color="393A34"><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">微信图片_20200216110741</span><span class="informations">1055×276 15.4 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> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/e/aefa2fae64eaea1bf7a5b80571ef3aa90c2e5c29.png" data-download-href="https://discuss.pytorch.org/uploads/default/aefa2fae64eaea1bf7a5b80571ef3aa90c2e5c29" title="微信图片_20200216110823"><img src="https://discuss.pytorch.org/uploads/default/original/3X/a/e/aefa2fae64eaea1bf7a5b80571ef3aa90c2e5c29.png" alt="微信图片_20200216110823" data-base62-sha1="oXV6hVg7vf7z7YRZdYEEMzrDaRj" width="690" height="493" data-dominant-color="2F312B"><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">微信图片_20200216110823</span><span class="informations">703×503 15.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>

ptrblck: <p>From Google translate:</p>
<blockquote>
<p>When training and testing, this Avg Accuracy is always 0%, acc is worth, what ’s going on?</p>
</blockquote>
<p>Could you post the shape of <code>labels</code> and <code>pred_cls</code>, please?</p>
<p>Also, <code>Variables</code> are deprecated since PyTorch <code>0.4.0</code> so you can just use tensors in newer versions. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<p>PS: I’ve edited your title as well so that more users can have a look.</p>

ky_Pa: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/d/ede052efb654f6a3edf1e44935ace150bfc75286.png" data-download-href="https://discuss.pytorch.org/uploads/default/ede052efb654f6a3edf1e44935ace150bfc75286" title="微信图片_20200216122446"><img src="https://discuss.pytorch.org/uploads/default/original/3X/e/d/ede052efb654f6a3edf1e44935ace150bfc75286.png" alt="微信图片_20200216122446" data-base62-sha1="xWlLT3dWKVkiYeYoDJw4Ht2b8XQ" width="690" height="359" data-dominant-color="2E302A"><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">微信图片_20200216122446</span><span class="informations">704×367 11.4 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>

ky_Pa: <p>还有这个 <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/6/065d30dd4c4e76d4a3ceea41157da875862e2ef7.png" data-download-href="https://discuss.pytorch.org/uploads/default/065d30dd4c4e76d4a3ceea41157da875862e2ef7" title="微信图片_20200216122707"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/6/065d30dd4c4e76d4a3ceea41157da875862e2ef7.png" alt="微信图片_20200216122707" data-base62-sha1="UiwzsSeuo3ZQCLIKjQNWDiMLHh" width="622" height="500" data-dominant-color="2D2F29"><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">微信图片_20200216122707</span><span class="informations">1065×855 29.2 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>

ptrblck: <p>Could you please use a translation service, which would make it easier to support you? <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>Print the shapes of all tensors for the accuracy calculation via:</p>
<pre><code class="lang-python">print(pred_cls.shape)
print(...)
</code></pre>
<p>so that we can have a look at the calculation.</p>
<p>Also, you can post your code here directly by wrapping it into three backticks ```.</p>

ky_Pa: <p>Where should the two prints be added?And can I send the entire code?</p>

ptrblck: <p>No, please don’t send unnecessary code and lets first start with the shape information.<br>
Add the print statements before calculating the accuracy.</p>
<pre><code class="lang-python">        ...
        # optimize critic
        optimizer_critic.step()

        print(pred_src.shape, pred_src.max(), pred_src.min())
        print(pred_tgt.shape, pred_tgt.max(), pred_tgt.min())
        pred_cls_src = torch.squeeze(pred_src.max(1)[1])
        pred_cls_tgt = torch.squeeze(pred_tgt.max(1)[1])
        acc = ((pred_cls_src == label_src).float().mean() + (pred_cls_tgt == label_tgt).float().mean()) / 2
</code></pre>

ky_Pa: <p>I got this result after trying<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/6/2611a1c5e49e3f4988859dbdf9bd36d2ec7a9048.png" data-download-href="https://discuss.pytorch.org/uploads/default/2611a1c5e49e3f4988859dbdf9bd36d2ec7a9048" title="微信图片_20200216125841"><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/6/2611a1c5e49e3f4988859dbdf9bd36d2ec7a9048.png" alt="微信图片_20200216125841" data-base62-sha1="5qLV2SxKQqdLTg8A9tWazqVLyWs" width="690" height="329" data-dominant-color="40413B"><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">微信图片_20200216125841</span><span class="informations">1365×652 28.4 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>

ky_Pa: <p>What should I do next</p>

ptrblck: <p>Thanks for the output!<br>
Could you do the same for the labels please?</p>

ky_Pa: <p>Is this the case<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/1/51bd72079c4161938351c967c40a4d8d74029665.png" data-download-href="https://discuss.pytorch.org/uploads/default/51bd72079c4161938351c967c40a4d8d74029665" title="微信图片_20200216135731"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/1/51bd72079c4161938351c967c40a4d8d74029665.png" alt="微信图片_20200216135731" data-base62-sha1="bF6yITeM7Z3ju1O6I8VTqw8wSNL" width="690" height="207" data-dominant-color="393A34"><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">微信图片_20200216135731</span><span class="informations">1355×407 22.9 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>

ky_Pa: <p>I put print in this position, right? It would be better if you could directly tell me where to add because I am a newbie, please trouble you, thank you!<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/1/e19bdde39aee2c1152895c0b958675f7ba42ac66.png" data-download-href="https://discuss.pytorch.org/uploads/default/e19bdde39aee2c1152895c0b958675f7ba42ac66" title="微信图片_20200216140038"><img src="https://discuss.pytorch.org/uploads/default/original/3X/e/1/e19bdde39aee2c1152895c0b958675f7ba42ac66.png" alt="微信图片_20200216140038" data-base62-sha1="wbPnb0AXj2qkeuMqwYLZuUlr2N8" width="690" height="330" data-dominant-color="2F312C"><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">微信图片_20200216140038</span><span class="informations">1051×504 19.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>

ptrblck: <p>Based on the shapes the calculation should be correct and it also seems the training loop prints a valid accuracy.<br>
Could you post the code to calculate the validation accuracy directly using three backticks ``` please?<br>
It’s a bit hard to follow your code structure using screenshots.</p>

ky_Pa: <pre><code class="lang-auto">"""Adversarial adaptation to train target encoder."""

import os

import torch
import torch.optim as optim
from torch import nn

from utils import make_variable
from core.test import eval

def train_tgt(src_encoder, src_classifier, tgt_encoder, critic, src_data_loader, tgt_data_loader, params):
    """Train encoder for target domain."""
    ####################
    # 1. setup network #
    ####################

    # setup criterion and optimizer
    criterion = nn.CrossEntropyLoss()
    optimizer_tgt = optim.Adam(tgt_encoder.parameters(),
                               lr=params.tgt_learning_rate,
                               betas=(params.beta1, params.beta2),
                               weight_decay=2.5e-5)
    optimizer_critic = optim.Adam(critic.parameters(),
                                  lr=params.critic_learning_rate,
                                  betas=(params.beta1, params.beta2),
                                  weight_decay=2.5e-5)
    len_data_loader = min(len(src_data_loader), len(tgt_data_loader))

    ####################
    # 2. train network #
    ####################

    for epoch in range(params.num_epochs):

        # set train state for Dropout and BN layers
        tgt_encoder.train()
        critic.train()

        # zip source and target data pair
        data_zip = enumerate(zip(src_data_loader, tgt_data_loader))
        for step, ((images_src, _), (images_tgt, _)) in data_zip:
            ###########################
            # 2.1 train discriminator #
            ###########################

            # make images variable
            images_src = make_variable(images_src)
            images_tgt = make_variable(images_tgt)

            # zero gradients for optimizer
            optimizer_critic.zero_grad()

            # extract and concat features
            feat_src = src_encoder(images_src)
            feat_tgt = tgt_encoder(images_tgt)
            feat_concat = torch.cat((feat_src, feat_tgt), 0)

            # predict on discriminator
            pred_src = critic(feat_src.detach())
            pred_tgt = critic(feat_tgt.detach())

            # prepare real and fake label
            label_src = make_variable(torch.ones(feat_src.size(0)).long())
            label_tgt = make_variable(torch.zeros(feat_tgt.size(0)).long())
            print(label_src.shape, label_src.max(), label_src.min())
            print(label_tgt.shape, label_tgt.max(), label_tgt.min())
            
            # compute loss for critic
            loss_critic = criterion(pred_src, label_src) + criterion(pred_tgt, label_tgt)
            loss_critic.backward()

            # optimize critic
            optimizer_critic.step()
           
            print(pred_src.shape, pred_src.max(), pred_src.min())
            print(pred_tgt.shape, pred_tgt.max(), pred_tgt.min())
            pred_cls_src = torch.squeeze(pred_src.max(1)[1])
            pred_cls_tgt = torch.squeeze(pred_tgt.max(1)[1])
            acc = ((pred_cls_src == label_src).float().mean() + (pred_cls_tgt == label_tgt).float().mean()) / 2

            ############################
            # 2.2 train target encoder #
            ############################

            # zero gradients for optimizer
            optimizer_critic.zero_grad()
            optimizer_tgt.zero_grad()

            # extract and target features
            feat_tgt = tgt_encoder(images_tgt)

            # predict on discriminator
            pred_tgt = critic(feat_tgt)

            # prepare fake labels
            label_tgt = make_variable(torch.ones(feat_tgt.size(0)).long())

            # compute loss for target encoder
            loss_tgt = criterion(pred_tgt, label_tgt)
            loss_tgt.backward()

            # optimize target encoder
            optimizer_tgt.step()

            #######################
            # 2.3 print step info #
            #######################
            if ((step + 1) % params.log_step == 0):
                print("Epoch [{:3d}/{:3d}] Step [{:3d}/{:3d}]:"
                      "critic_loss={:.5f} tgt_loss={:.5f} acc={:.5f}"
                      .format(epoch + 1,
                              params.num_epochs,
                              step + 1,
                              len_data_loader,
                              loss_critic.item(),
                              loss_tgt.item(),
                              acc.item()))

        #############################
        # 2.4 eval training model #
        #############################
        if ((epoch + 1) % params.eval_step == 0):
            print ("eval model on source data")
            eval(tgt_encoder, src_classifier, src_data_loader)
            print ("eval model on target data")
            eval(tgt_encoder, src_classifier, tgt_data_loader)

        #############################
        # 2.5 save model parameters #
        #############################
        if ((epoch + 1) % params.save_step == 0):
            torch.save(critic.state_dict(), os.path.join(
                params.model_root,
                "{}-{}-critic-{}.pt".format(params.src_dataset, params.tgt_dataset, epoch + 1)))
            torch.save(tgt_encoder.state_dict(), os.path.join(
                params.model_root,
                "{}-{}-target-encoder-{}.pt".format(params.src_dataset, params.tgt_dataset, epoch + 1)))

    torch.save(critic.state_dict(), os.path.join(
        params.model_root,
        "{}-{}-critic-final.pt".format(params.src_dataset, params.tgt_dataset)))
    torch.save(tgt_encoder.state_dict(), os.path.join(
        params.model_root,
        "{}-{}-target-encoder-final.pt".format(params.src_dataset, params.tgt_dataset)))
    return tgt_encoder</code></pre>

ky_Pa: <pre><code class="lang-auto">"""Test script to classify target data."""

import torch.nn as nn

from utils import make_variable


def eval(encoder, classifier, data_loader):
    """Evaluation for target encoder by source classifier on target dataset."""
    # set eval state for Dropout and BN layers
    encoder.eval()
    classifier.eval()

    # init loss and accuracy
    loss = 0
    acc = 0

    # set loss function
    criterion = nn.CrossEntropyLoss()

    # evaluate network
    for (images, labels) in data_loader:
        
        images = make_variable(images, volatile=True)
        labels = make_variable(labels).squeeze_()

        preds = classifier(encoder(images))
        loss += criterion(preds, labels).item()

        pred_cls = preds.data.max(1)[1]
        acc += pred_cls.eq(labels.data).cpu().sum()

    loss /= len(data_loader)
    acc /= len(data_loader.dataset)

    print("Avg Loss = {}, Avg Accuracy = {:2%}".format(loss, acc))</code></pre>

ptrblck: <p>The code should still work (besides the deprecated usage of <code>Variable</code> and the <code>.data</code> attribute).<br>
Could you check the <code>pred_cls</code> and <code>labels</code> values in your validation loop?</p>

ky_Paa: <p>This post was flagged by the community and is temporarily hidden.</p>

ptrblck: <p>Just print out the values and check, if the predictions are indeed all wrong, which would yield a zero accuracy.</p>

ptrblck: <pre><code class="lang-python">def eval(encoder, classifier, data_loader):
    ...
    # evaluate network
    for (images, labels) in data_loader:
        ...
        print(pred_cls)
        print(labels)
        print(pred_cls.eq(labels.data).cpu().sum())
        acc += pred_cls.eq(labels.data).cpu().sum()
        ...
</code></pre>
<p>Use this code to check your values in the validation loop.</p>
<p>PS: Also, please stick to one user account.</p>

ptrblck: <p>Try to call:</p>
<pre><code class="lang-python">acc = acc.float()
acc /= len(data_loader.dataset)
</code></pre>
<p>and check the accuracy again.<br>
It seems <code>acc</code> is an int which should thus result in an integer division when dividing with the length of your dataset.</p>