Skip to content

Commit ec87c23

Browse files
Updated calculation of train_loss and valid_loss
Using len(train_loader.dataset) and len(valid_loader.dataset) will result in wrong values as they return size of 'train_data' (i.e 60000 images). This is the reason for such difference in train_loss and valid_loss. 'train_loader' actually has size of 0.8*train_data (i.e 48000 images) and valid_loader actually has size of 0.2*train_data (i.e 12000 images). Hence calculate avg batch loss in each batch and divide by total batches. This works because all the batches in both the dataloaders are of 20(reason: 48000%20=0 and 12000%20=0)
1 parent 5dcb0a8 commit ec87c23

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

convolutional-neural-networks/mnist-mlp/mnist_mlp_solution_with_validation.ipynb

+4-4
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@
418418
" # perform a single optimization step (parameter update)\n",
419419
" optimizer.step()\n",
420420
" # update running training loss\n",
421-
" train_loss += loss.item()*data.size(0)\n",
421+
" train_loss += loss.item()\n",
422422
" \n",
423423
" ###################### \n",
424424
" # validate the model #\n",
@@ -430,12 +430,12 @@
430430
" # calculate the loss\n",
431431
" loss = criterion(output, target)\n",
432432
" # update running validation loss \n",
433-
" valid_loss += loss.item()*data.size(0)\n",
433+
" valid_loss += loss.item()\n",
434434
" \n",
435435
" # print training/validation statistics \n",
436436
" # calculate average loss over an epoch\n",
437-
" train_loss = train_loss/len(train_loader.dataset)\n",
438-
" valid_loss = valid_loss/len(valid_loader.dataset)\n",
437+
" train_loss = train_loss/len(train_loader)\n",
438+
" valid_loss = valid_loss/len(valid_loader)\n",
439439
" \n",
440440
" print('Epoch: {} \\tTraining Loss: {:.6f} \\tValidation Loss: {:.6f}'.format(\n",
441441
" epoch+1, \n",

0 commit comments

Comments
 (0)