ZeroDivisionError: division by zero

Imad: <p>Hi, everyone, I have an error with this code:</p>
<p>dataset=pd.read_csv(‘out.csv’,delimiter=’,’,skiprows=1, squeeze=True)<br>
x=dataset.iloc[:,0:4]<br>
y=dataset.iloc[:,[4]]</p>
<p>x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(x, y,<br>
test_size=0.2,<br>
random_state=42)<br>
train_set=x_train,y_train<br>
test_set=x_test,y_test</p>
<p>print(train_set)<br>
print(test_set)</p>
<p>train_loader = DataLoader(dataset=train_set,batch_size=batch_size,drop_last=True, shuffle=False)</p>
<p>test_loader = DataLoader(dataset=test_set ,batch_size=batch_size, drop_last=True, shuffle=False)</p>
<p>t=Transformer(dim_val,dim_attn,input_size,dec_seq_len,out_seq_len,n_decoder_layers,n_encoder_layers,n_heads)<br>
model=t<br>
model.to(device)<br>
optimizer=torch.optim.Adam(model.parameters(),lr=lr)</p>
<p>losses=[]</p>
<p>for b ,(inputs,labels) in enumerate (train_loader):</p>
<pre><code>     inputs = inputs.to(device=device)         
     inputs=torch.tensor(inputs)
     scores = model(inputs)                  
     criterion = nn.MSELoss()                             
 
     loss=criterion(scores,labels)

     losses.append(loss.items())

     optimizer.zero_grad()
     loss.backward()
     optimizer.step()
</code></pre>
<p>“the error in this line below”<br>
print(f"Cost at epoch {epochs} is {sum(losses)/len(losses)}")</p>
<p>def check_accuracy(loader, model):<br>
num_correct = 0<br>
num_samples = 0<br>
model.eval()</p>
<pre><code>with torch.no_grad():
    for z, y in loader:
        z = z.to(device=device)
        y = y.to(device=device)                          
        scores = model(z)
        _, predictions = scores.max()               
        num_correct+=(predictions==y).sum(1)
        num_correct=torch.tensor(num_correct)

        num_samples += predictions.size(0)
                
    print(
         f"Got {num_correct} / {num_samples} with accuracy {float(num_correct)/float(num_samples)*100:.2f}"
     )

model.train()
</code></pre>
<p>print(“Checking accuracy on Training Set”)<br>
check_accuracy(train_loader, model)</p>
<p>print(“Checking accuracy on Test Set”)<br>
check_accuracy(test_loader, model)</p>

KFrank: <p>Hi Imad!</p>
<aside class="quote no-group" data-username="Imad" data-post="1" data-topic="144048" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/f08c70/48.png" class="avatar"> Imad:</div>
<blockquote>
<p>“the error in this line below”<br>
print(f"Cost at epoch {epochs} is {sum(losses)/len(losses)}")</p>
</blockquote>
</aside>
<p>This suggests that <code>losses</code> is of zero length.  Try printing out <code>len (losses)</code><br>
just before the division to check.</p>
<p>If this is the case, it would suggest that your <code>train_loader</code> is “empty” for<br>
some reason.</p>
<aside class="quote no-group">
<blockquote>
<p>for b ,(inputs,labels) in enumerate (train_loader):</p>
</blockquote>
</aside>
<p>Try adding a print statement, e.g., <code>print (b)</code>, at the top of your<br>
<code>train_loader</code> loop, just after the <code>for b ...</code> line.  Is the body of the loop<br>
ever executed?</p>
<p>If <code>train_loader</code> is empty, you will have to work backward to figure out why.</p>
<p>Best.</p>
<p>K. Frank</p>

Imad: <p>Thanks for replying. yes, I checked before it’s empty. I have two for loops the first one work, but I forget to add it to the code the second one not working its jump directly to the print statement and give zero result.<br>
for epoch in range(epochs):<br>
losses=[]<br>
for  inputs,labels in enumerate (train_loader):<br>
inputs = inputs.to(device=device)<br>
labels = labels.to(device=device)<br>
inputs.clone().detach()<br>
labels.clone().detach()<br>
scores = model(inputs)<br>
loss=criterion(scores,labels)</p>
<pre><code>      losses.append(loss.item())

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

print(f"Cost at epoch {epoch} is {sum(losses)/len(losses)}")</code></pre>

rahul_kumar2: <p>The super class of ZeroDivisionError is ArithmeticError. This exception raised when the second argument of a division or modulo operation is zero.  In Mathematics, when a number is divided by a zero, the result is an infinite number. It is impossible to write an Infinite number physically. Python interpreter throws “<a href="http://net-informations.com/python/err/zero.htm" rel="noopener nofollow ugc">ZeroDivisionError</a>: division by zero” error if the result is infinite number. While implementing any program logic and there is division operation make sure always handle ArithmeticError or ZeroDivisionError so that program will not terminate.</p>
<pre><code class="lang-auto">try:
    z = x / y
except ZeroDivisionError:
    z = 0
</code></pre>
<p>Or check before you do the division:</p>
<pre><code class="lang-auto">if y == 0:
    z = 0
else:
    z = x / y
</code></pre>
<p>The latter can be reduced to:</p>
<p><code>z = 0 if y == 0 else (x / y)</code></p>

usama_meraj: <p>hello ive tried this and it wont work for me I’m getting zero error at pred_accuracy I’ve attached the code below<br>
“”"</p>
<p>correct_pred = 0<br>
almost_correct_pred = 0<br>
total_predict = 0</p>
<p>pred = np.array([1,3])</p>
<p><span class="hashtag">#set</span> test folder path<br>
test_folder = dataset + ‘/Test’</p>
<p><span class="hashtag">#loop</span> over each .bmp file in the test folder and pass it through the network<br>
for test_im in glob(test_folder + ‘/*.bmp’):</p>
<pre><code>#get actual temp from filename
temp = os.path.basename(test_im).split('_')[2]

#pass image through the model to get predicted temp and confidence
prediction, conf = predict(trained_model, test_im, 3)

#change the indices back to class labels
prediction = [idx_to_class[i] for i in prediction]

#create array of actual temp, predicted temp and confidence
for i in range(len(prediction)):
    
    row = np.transpose(np.array([temp, prediction[i], conf[i]]))

    pred = np.vstack((pred, row))

#print actual temp, predicted temp and confidence
print('True Label: {true_label}, Predicted Label: {predicted_label} with confidence of {confidence}\n'.format(true_label=temp, predicted_label=prediction[0], confidence=conf[0]))


#calculate ratio of correct and almost correct (within 50C) predictions
total_predict += 1


if temp == prediction[0]:
    
    correct_pred += 1

try:

    if int(temp) == int(prediction[0]) + 50 or int(temp) == int(prediction[0]) - 50:
        
        almost_correct_pred += 1
except ValueError:
    
    temp = temp[:-1]
    
    if int(temp) == int(prediction[0][:-1]) + 50 or int(temp) == int(prediction[0][:-1]) - 50:
        
        almost_correct_pred += 1
</code></pre>
<p>pred_acc = correct_pred / total_predict<br>
almost_correct_acc = (almost_correct_pred + correct_pred) / total_predict</p>
<p>print(‘{correct}/{total} images predicted correctly ({percentage}%)’.format(correct=correct_pred, total=total_predict, percentage=str(pred_acc<em>100)[:5]))<br>
print(‘{correct}/{total} images almost predicted correctly ({percentage}%)’.format(correct=almost_correct_pred+correct_pred, total=total_predict, percentage=str(almost_correct_acc</em>100)[:5]))</p>
<p>pred = np.delete(pred, 0, axis=0)<br>
pred = pred.astype(‘float’)</p>
<p>“”“”</p>
<p>can someone help me with this I am really confused haha</p>

ptrblck: <p>Could you check which line of code is raising the error? I would guess <code>total_predict</code> might be set to 0 so could you check if that’s indeed the case?</p>

usama_meraj: <p>This code works perfectly fine on pretrained Alex net model. This issue arises when I implement my own small CNN model the code is working fine I’m getting satisfactory accuracy and losses but for some reason it’s failing to compute predictions at Pred_acc where total correction is initialsed by correction = 0 then full_correction += 1 in a if loop. I’m not sure what’s going on it’s as if it’s not reading my statements in the loop when I’m computing predictions</p>

ptrblck: <p>I’m unsure where the <code>correction</code> variable is used but check which variable is set to zero and raises the error. Once you’ve narrowed it down check why it’s not updated as you are expecting.</p>