My CNN model show weird accuracy and some time even did not show accuracy. i am looking for the solution for a long time please help

username: Muhammad_Izaz
cooked: <p>a =torch.tensor([])<br>
total_step = len(xy)<br>
loss_list = []</p>
<p>acc_list = []<br>
for epoch in range(num_epochs):<br>
<span class="hashtag">#for</span> data in x_loader:<br>
total_trained=0<br>
correctly_trained=0</p>
<pre><code>for i in x_train,y_train:
    
    #criterion(outputs, y)

    outputs = model(x_train)
    
    loss = criterion(outputs, torch.max(y_train, 1)[1])
    loss_list.append(loss.item())
    
    

    # Backprop and perform Adam optimisation
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
#optimizer update the weight parameters to minimize the loss function
    total_trained += y_train.size(0)
    
    _ , predicted_labels = torch.max(outputs.data,1)

    correctly_trained += (y_train.data==predicted_labels).sum()

#Priniting the Accuracy and loss values for training data
print("Epoch:%d"%(epoch))
print("Training...")
print(" Accuracy:%f loss:%f"%((correctly_trained/total_trained)*100,loss))
</code></pre>
<p>this time the output is like this.</p>
<p>Epoch:0<br>
Training…<br>
Accuracy:116800.000000 loss:0.000000<br>
Epoch:1<br>
Training…<br>
Accuracy:116800.000000 loss:0.000000</p>

username: ptrblck
cooked: <p>Could you check the shape of <code>y_train</code> and <code>predicted_labels</code> and make sure that no broadcasting takes place?<br>
Also, check the <code>type()</code> of the comparison and cast the result to <code>long()</code> or <code>float()</code> if necessary to avoid an overflow. (If I recall correctly, the result might be an <code>uint8</code> in older PyTorch versions).</p>
<p>PS: Don’t use the <code>.data</code> attribute, as it might have unwanted side effects.</p>

username: DAlolicorn
cooked: <p>Inferring from the fact that you coded <code>torch.max(y_train, 1)[1]</code><br>
<code>y_train</code> is a label of shape <code>(N, Classes)</code>.</p>
<p><code>correctly_trained += (y_train.data==predicted_labels).sum()</code><br>
is accumulating <code>N * Classes</code> instances, while <code>total_trained += y_train.size(0)</code> only accumulates <code>N</code> instances.</p>
<p>I think that’s why you get accuracy bigger than 1.</p>

username: Muhammad_Izaz
cooked: <p>y_train shape is<br>
torch.Size([2374, 1])</p>

username: ptrblck
cooked: <p>For this shape, <code>torch.max(y_train, 1)</code> will yield a tensor full of zeros.<br>
Which criterion are you using and what values does <code>y_train</code> contain?</p>

username: Muhammad_Izaz
cooked: <p>criterion = nn.CrossEntropyLoss()<br>
y_train contain 1s and 0s</p>

username: ptrblck
cooked: <p>In that case you should remove <code>dim1</code> from the target via <code>y_train = y_train.suqeeze(1)</code> and also not call <code>torch.max(y_train, 1)</code>, but pass the target directly to the criterion instead.</p>

username: Muhammad_Izaz
cooked: <p>Target 1 is out of bounds.</p>

username: ptrblck
cooked: <p>For your target containing values in <code>[0, 1]</code> and <code>nn.CrossEntropyLoss</code>, the output of your model should have the shape <code>[batch_size, 2]</code>. Could you check that and rerun the code again, please?</p>

username: Muhammad_Izaz
cooked: <p>i am still stuck here. The total training set is 2374 and my model produce the correct output 2841678. whats the problem, please help?</p>

username: Muhammad_Izaz
cooked: <p>Now its showing “too many values to unpack (expected 2)” error</p>

username: braindotai
cooked: <p>Here is the working solution-</p>
<pre><code class="lang-python">train_iterator = torch.utils.data.TensorDataset(x_train, y_train.reshape(-1).long())
train_data = torch.utils.data.DataLoader(train_iterator, batch_size = 64, shuffle = True)

for x, y in train_data:
    break
print(x.shape, y.shape) # will print (64, 1, 100), (64,)

criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters())

epochs = 20
batches = len(train_data)
# number of batches in your dataset (where each batch contains 64 examples from your training data)

for epoch in range(epochs):
    epoch_loss = 0.0
    epoch_accuracy = 0.0
    for features, labels in train_data:
        outputs = model(features)
        loss = criterion(outputs, labels)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        epoch_loss += loss.item() # here we are accumulating loss for each training batch.
        epoch_accuracy += (outputs.argmax(1) == labels).float().mean()

    print(f'Epoch: {epoch} -&gt; Loss: {(epoch_loss/batches):.8f}, Accuracy: {(epoch_accuracy/batches):.8f}')
    # dividing epoch_loss by number of batches in our train_data to get mean epoch_loss
    # and printing epoch loss round of to 8 decimals.... same for epoch_accuracy
</code></pre>

username: Muhammad_Izaz
cooked: <p>Ok brother i have done it and its working perfectly.</p>