My network is overfitting while training giving 100% accuracy, How to fix this?

username: Ali_Mashoud
cooked: <p>Hello, I have been working on a Fashion MNIST hackathon and I have been using CNN architecture to train batches. However, one problem that I feel I am facing is that my network is overfitting the data. Can someone tell me if it is overfitting the data or is it okay to have 100% accuracy. What improvements I can make to improve the model. Here are the codes of both model architecture and the model training-results.</p>
<blockquote>
<p>class ConvolutionalNetwork(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(3,6,3,1)<br>
self.conv2 = nn.Conv2d(6,16,3,1)<br>
self.fc1 = nn.Linear(5<em>5</em>16,120)<br>
self.fc2 = nn.Linear(120,84)<br>
self.fc3= nn.Linear(84,10)</p>
<pre><code>def forward(self, X):
    X= f.relu(self.conv1(X))
    X =f.max_pool2d(X,2,2)
    X= f.relu(self.conv2(X))
    X= f.max_pool2d(X,2,2)
    X= X.view(-1,5*5*16)
    X= f.relu(self.fc1(X))
    X= f.relu(self.fc2(X))
    X= self.fc3(X)
    return f.log_softmax(X, dim=1)
</code></pre>
</blockquote>
<p><strong>Code for Training Batches:</strong></p>
<blockquote>
<p>import time<br>
start_time = time.time()<br>
epochs = 5<br>
train_losses = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
test_losses = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
train_correct = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
test_correct = <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>for i in range(epochs):<br>
trn_corr=0<br>
tst_corr = 0</p>
<pre><code>#Run the training batches
for b, (X_train,y_train) in enumerate(train_loader):
    b+=1
    
    y_pred = model(X_train)
    loss= criterion(y_pred,y_train)
    predicted = torch.max(y_pred.data, 1)[1]
    batch_corr = (predicted==y_train).sum()
    trn_corr += batch_corr
    
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    #Print interim results
    if b%600 == 0:
        print(f'epoch: {i:2}  batch: {b:4} [{10*b:6}/60000]  loss: {loss.item():10.8f}  \
</code></pre>
<p>accuracy: {trn_corr.item()<em>100/(10</em>b):7.3f}%')</p>
<pre><code>loss=loss.detach().numpy()
train_losses.append(loss)
train_correct.append(trn_corr)

#Run testing batches
with torch.no_grad():
        for b, (X_test,y_test) in enumerate(test_loader):
            b+=1
            y_val = model(X_test)
            
            predicted = torch.max(y_val.data, 1)[1]
            tst_corr += (predicted==y_test).sum()
           
loss = criterion(y_val,y_test)            
loss=loss.detach().numpy()
test_losses.append(loss)
test_correct.append(tst_corr)
</code></pre>
<p>print(f"time_duration:{start_time-time.time()}")</p>
</blockquote>
<p>The results are in the form of images shown below:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/1/41ba65037ec1033791bbdd14ff96a0bcb464c844.png" data-download-href="https://discuss.pytorch.org/uploads/default/41ba65037ec1033791bbdd14ff96a0bcb464c844" title="Capture"><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/1/41ba65037ec1033791bbdd14ff96a0bcb464c844.png" alt="Capture" data-base62-sha1="9nsnOC627jq6XOsyEEWZn1RRiOo" width="672" height="500" data-dominant-color="EAEAEA"><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">Capture</span><span class="informations">720×535 35.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>
<p>.</p>

username: KFrank
cooked: <p>Hi Ali!</p>
<aside class="quote no-group" data-username="Ali_Mashoud" data-post="1" data-topic="133370" data-full="true">
<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/ali_mashoud/48/33224_2.png" class="avatar"> Ali_Mashoud:</div>
<blockquote>
<p>is it okay to have 100% accuracy.</p>
</blockquote>
</aside>
<p>No, not after training a network on a real problem for less than<br>
an epoch.</p>
<p>You have a bug somewhere.  If I had to guess, I would guess that<br>
your <code>train_loader</code> is returning the same <code>y_train</code> class label (for<br>
example, always <code>0</code>) for all of your samples.</p>
<aside class="quote no-group">
<blockquote>
<p>The results are in the form of images shown below:</p>
</blockquote>
</aside>
<p>As an aside, please don’t post screenshots of textual information.<br>
Doing so breaks accessibility, searchability, and copy-paste.</p>
<p>Best.</p>
<p>K. Frank</p>

username: Ali_Mashoud
cooked: <p>Hy K.Frank. Thank you for your kind advice. I am importing Fashion MNIST data via this code. Can it be a problem:</p>
<blockquote>
<p>train_data=datasets.ImageFolder(“C:/Users/mashood/Desktop/tootoo/Datasets/Fashion_MNIST/train”,transform=train_transform)<br>
test_data = datasets.ImageFolder(“C:/Users/mashood/Desktop/tootoo/Datasets/Fashion_MNIST/test”,transform=test_transform)</p>
</blockquote>

username: Ali_Mashoud
cooked: <p>Hy K.Frank. Thank you for your kind advice. I am importing Fashion MNIST data via this code. Can it be a problem:</p>
<pre><code>train_data=datasets.ImageFolder(“C:/Users/mashood/Desktop/tootoo/Datasets/Fashion_MNIST/train”,transform=train_transform)
test_data = datasets.ImageFolder(“C:/Users/mashood/Desktop/tootoo/Datasets/Fashion_MNIST/test”,transform=test_transform)</code></pre>

username: KFrank
cooked: <p>Hi Ali!</p>
<aside class="quote no-group" data-username="Ali_Mashoud" data-post="4" data-topic="133370" data-full="true">
<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/ali_mashoud/48/33224_2.png" class="avatar"> Ali_Mashoud:</div>
<blockquote>
<p>Can it be a problem:</p>
</blockquote>
</aside>
<p>Try printing out <code>predicted</code> and <code>y_train</code> for some of your batches<br>
and see if they make sense.</p>
<p>Best.</p>
<p>K. Frank</p>