Unusual accuracy result

username: rai24
cooked: <p>def training(train_model, number_of_epochs=3, batch_size=10, learning_rate=1e-3):</p>
<pre><code>criterion = nn.MSELoss() # mean square error loss
optimizer = torch.optim.Adam(train_model.parameters(),
                             lr=learning_rate, 
                             weight_decay=1e-5) # &lt;--




for epoch in range(number_of_epochs):  # loop over the dataset multiple times
    
    running_loss = 0
    total_train = 0
    correct_train = 0
    for i, data in enumerate(traindataloader):
        # get the inputs
        t_image, mask = data
       # print(t_image)
       # print(mask)
        t_image, mask = Variable(t_image), Variable(mask)
    
        print(type(mask))
        # zeroes the gradient buffers of all parameters
        optimizer.zero_grad()
        # forward + backward + optimize
        outputs = train_model(t_image) # forward
       # print(type(outputs))
        
        loss = criterion(outputs,t_image ) # calculate the loss
        loss.backward() # back propagation
        optimizer.step() # update gradients
        running_loss += loss.item()
        
        # accuracy
        _, predicted = torch.max(outputs.data, 1)
       # print(predicted)
        total_train += t_image.nelement()
        
        correct_train +=(predicted==t_image).sum().item()
        print(correct_train)
        print(total_train)
        train_accuracy =100*(correct_train / total_train) 
       #avg_accuracy = train_accuracy / len(train_loader)                                     
        
        print("Epoch {}, train Loss: {:.3f},Training Accuracy = {}".format(epoch, loss.item(),train_accuracy))
</code></pre>
<p>Result: The accuracy is 966.73 which is, I understand is because of the division of correct_train and total_train, but the accuracy should not be like that, why it is so ???<br>
&lt;class ‘torch.Tensor’&gt;<br>
7424540<br>
768000<br>
Epoch 0, train Loss: 0.044,Training Accuracy = 966.7369791666667<br>
&lt;class ‘torch.Tensor’&gt;<br>
14163910<br>
1536000<br>
Epoch 0, train Loss: 0.041,Training Accuracy = 922.1295572916666<br>
&lt;class ‘torch.Tensor’&gt;</p>

username: ptrblck
cooked: <p>Are you working on a segmentation use case?<br>
If so, then <code>t_image.nelement</code> would return the number of pixels in the input batch including the color channels, which might be wrong.<br>
Also, are you using an older PyTorch version, as you are still using <code>Variables</code>?<br>
If that’s the case, check if <code>(predicted==t_image).sum()</code> might be overflowing, as this was an issue in old PyTorch versions (pre <code>1.0</code>, if I’m not mistaken).</p>

username: rai24
cooked: <p>I am using PyTorch 1.4.0 and I am not sure about the segmentation use case thing, however, predicted results  the tensor tensor like this<br>
([[[0, 0, 0,  …, 0, 0, 0],<br>
[0, 0, 0,  …, 0, 0, 0],<br>
[0, 0, 0,  …, 0, 0, 0],<br>
…,<br>
[0, 0, 0,  …, 0, 0, 0],<br>
[0, 0, 0,  …, 0, 0, 0],<br>
[0, 0, 0,  …, 0, 0, 0]],<br>
correct_train = 7424540<br>
total_train= 768000</p>

username: ptrblck
cooked: <p>Could you explain your use case, please?<br>
I.e. what does the output of your model represent, which shapes do your output and target have?</p>

username: rai24
cooked: <p>The dataset is of images and the final output I am expecting is the feature extraction of those images. and I have not stored anything in target, because that is giving me error of singleton dimension.</p>

username: ptrblck
cooked: <p>What are you comparing the extracted features to, if you don’t store anything in the target?</p>

username: rai24
cooked: <p>I am comparing it with the original images in the trainload</p>

username: ptrblck
cooked: <p>Could you post the shapes of all tensors involved in these operations, please?</p>
<pre><code class="lang-python">loss = criterion(outputs,t_image )
(predicted==t_image)
</code></pre>

username: rai24
cooked: <p>Below are the outputs and thank you so much<br>
result of t_image  = tensor([[[[0., 0., 0.,  …, 0., 0., 0.],<br>
[0., 0., 0.,  …, 0., 0., 0.],<br>
[0., 0., 0.,  …, 0., 0., 0.],<br>
…,<br>
[0., 0., 0.,  …, 0., 0., 0.],<br>
[0., 0., 0.,  …, 0., 0., 0.],<br>
[0., 0., 0.,  …, 0., 0., 0.]]],</p>
<pre><code>    [[[0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      ...,
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.],
      [0., 0., 0.,  ..., 0., 0., 0.]]],
 -----
</code></pre>
<p>results of Outputs : tensor([[[[0.2135, 0.2111, 0.1918,  …, 0.1948, 0.2247, 0.2309],<br>
[0.1966, 0.2167, 0.2018,  …, 0.2265, 0.2696, 0.2443],<br>
[0.1625, 0.2062, 0.1870,  …, 0.2024, 0.2853, 0.2419],<br>
…,<br>
[0.1735, 0.2144, 0.2097,  …, 0.2037, 0.2798, 0.2286],<br>
[0.2016, 0.2447, 0.2487,  …, 0.2459, 0.2944, 0.2489],<br>
[0.2133, 0.2369, 0.2289,  …, 0.2182, 0.2556, 0.2369]]],</p>
<pre><code>    [[[0.2135, 0.2111, 0.1918,  ..., 0.1949, 0.2247, 0.2309],
      [0.1966, 0.2167, 0.2018,  ..., 0.2265, 0.2696, 0.2443],
      [0.1625, 0.2062, 0.1870,  ..., 0.2024, 0.2853, 0.2419],
      ...,
      [0.1735, 0.2144, 0.2097,  ..., 0.2038, 0.2799, 0.2287],
      [0.2016, 0.2447, 0.2487,  ..., 0.2459, 0.2944, 0.2489],
      [0.2133, 0.2369, 0.2290,  ..., 0.2182, 0.2556, 0.2369]]],


    [[[0.2135, 0.2109, 0.1915,  ..., 0.1949, 0.2246, 0.2309],
      [0.1965, 0.2172, 0.2023,  ..., 0.2268, 0.2692, 0.2439],
      [0.1625, 0.2061, 0.1867,  ..., 0.2028, 0.2851, 0.2419],
      ...,
      [0.1733, 0.2132, 0.2092,  ..., 0.2038, 0.2800, 0.2292],
      [0.2020, 0.2442, 0.2484,  ..., 0.2467, 0.2951, 0.2496],
      [0.2134, 0.2364, 0.2287,  ..., 0.2185, 0.2558, 0.2368]]],


    ...,


    [[[0.2135, 0.2111, 0.1920,  ..., 0.1949, 0.2247, 0.2309],
      [0.1966, 0.2167, 0.2021,  ..., 0.2264, 0.2696, 0.2443],
      [0.1625, 0.2062, 0.1868,  ..., 0.2024, 0.2852, 0.2418],
      ...,
</code></pre>
<p>loss = tensor(0.0259, grad_fn=)<br>
tensor(0.0315, grad_fn=)<br>
…</p>
<p>correct_train= 7479690<br>
total_train = 768000<br>
train_accuracy = 973.91796875<br>
predicted = tensor([[[0, 0, 0,  …, 0, 0, 0],<br>
[0, 0, 0,  …, 0, 0, 0],<br>
[0, 0, 0,  …, 0, 0, 0],<br>
…,<br>
[0, 0, 0,  …, 0, 0, 0],<br>
[0, 0, 0,  …, 0, 0, 0],<br>
[0, 0, 0,  …, 0, 0, 0]],</p>
<pre><code>    [[0, 0, 0,  ..., 0, 0, 0],
     [0, 0, 0,  ..., 0, 0, 0],
     [0, 0, 0,  ..., 0, 0, 0],</code></pre>

username: ptrblck
cooked: <p>Could you post the shapes only, not the values, via <code>print(tensor.shape)</code>?</p>

username: rai24
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="10" data-topic="74620">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>print(tensor.shape)</p>
</blockquote>
</aside>
<p>print(loss.shape)</p>
<p>torch.Size(<span class="chcklst-box fa fa-square-o fa-fw"></span>)<br>
tensor([[[0, 0, 0,  …, 0, 0, 0],<br>
[0, 0, 0,  …, 0, 0, 0],<br>
[0, 0, 0,  …, 0, 0, 0],<br>
…,<br>
[0, 0, 0,  …, 0, 0, 0],<br>
[0, 0, 0,  …, 0, 0, 0],<br>
[0, 0, 0,  …, 0, 0, 0]],</p>
<pre><code>    [[0, 0, 0,  ..., 0, 0, 0],
     [0, 0, 0,  ..., 0, 0, 0],
     [0, 0, 0,  ..., 0, 0, 0],
     ...,
     [0, 0, 0,  ..., 0, 0, 0],
     [0, 0, 0,  ..., 0, 0, 0],
     [0, 0, 0,  ..., 0, 0, 0]],
</code></pre>
<p>##############################<br>
print(predicted.shape)<br>
torch.Size([10, 240, 320])</p>
<p>###########################<br>
print(t_image.shape)<br>
torch.Size([10, 1, 240, 320])<br>
############################</p>

username: rai24
cooked: <p>print(outputs.shape)<br>
torch.Size([10, 1, 240, 320])</p>