Mini batching in a basic neural network model using pytorch

Pearl_20: <p>hello, I am working on a neural network model and I have tried using mini batching to optimize my code but I get this error</p>
<pre><code class="lang-auto">ValueError: Expected input batch_size (466) to match target batch_size (1869).
</code></pre>
<p>my code is</p>
<pre><code class="lang-auto">from __future__ import print_function
from builtins import range
from torch.utils.data import DataLoader
import torchvision.transforms as transforms

import pandas as pd
import numpy as np

# Data sets
IRIS_TRAINING = r'C:\Users\User\PycharmProjects\AI LAB\prof NN code\train.txt'
IRIS_TEST = r'C:\Users\User\PycharmProjects\AI LAB\prof NN code\test.txt'
train_data = np.genfromtxt(IRIS_TRAINING, skip_header=1, 
    dtype=float, delimiter=';') 
test_data = np.genfromtxt(IRIS_TEST, skip_header=1, 
    dtype=float, delimiter=';') 

#initialize batch size
BATCH_SIZE = 400
dataloadertrain = DataLoader(dataset= train_data, batch_size=BATCH_SIZE, shuffle=True)
dataloadertest = DataLoader(dataset= test_data, batch_size=BATCH_SIZE, shuffle=True)

print("Array Dimension = ",len(train_data.shape))


# In[352]:


#split x and y (feature and target)
xtrain = train_data[:,:4001]
ytrain = train_data[:,4001]


# In[353]:



import torch
import torch.nn as nn
import torch.nn.functional as f
torch.manual_seed(1234)

#hyperparameters
hl = 8
lr = 0.01
num_epoch = 2000


# In[354]:


#build model
class Net(nn.Module):

    def __init__(self, n_feature, hl, n_output):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(n_feature, hl)
        self.fc2 = nn.Linear(hl, n_output)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x
        


# In[355]:


#choose optimizer and loss function
criterion = nn.CrossEntropyLoss()
#optimizer = torch.optim.SGD(net.parameters(), lr=lr)
#optimizer = torch.optim.Adagrad(net.parameters(), lr=lr)
optimizer = torch.optim.Adam(net.parameters(), lr=lr)
#optimizer = torch.optim.SGD(net.parameters(), lr=lr, momentum=0.9)
#optimizer = torch.optim.RMSprop(net.parameters(), lr=lr)


# In[356]:


net_batch = Net(n_feature=4001, hl=10, n_output=3)  
loss_list = []
avg_loss_list = []
batch_size = 16
n_batches = int(len(train_data) / batch_size) 
print(n_batches)

for epoch in range(len(train_data)):
    X = torch.Tensor(xtrain).float()
    Y = torch.Tensor(ytrain).long()
    for batch in range(n_batches):
       batch_X, batch_Y = X[batch*batch_size:(batch+1)*batch_size,], Y[batch*batch_size:(batch+1)*batch_size,]
       prediction = net_batch(batch_X)
       loss = criterion(out, Y)
       loss_list.append(loss)

       optimizer_batch.zero_grad()
       loss.backward() 
       optimizer_batch.step() 
       print('\repoch: {}\tbatch: {}\tLoss =  {:.3f}'.format(epoch, batch, loss), end="")


                              
    

   


# In[ ]:


#feedforward - backprop
optimizer.zero_grad()
out = net(X)
loss.backward()
optimizer.step()
acc = 100 * torch.sum(Y==torch.max(out.data, 1)[1]).double() / len(Y)
if (epoch % 50 == 1):
	    print ('Epoch [%d/%d] Loss: %.4f   Acc: %.4f' 
                  %(epoch+1, num_epoch, loss.item(), acc.item()))





# In[320]:




#split x and y (feature and target)
xtest = test_data[:,:4001]
ytest = test_data[:,4001]

#get prediction
X = torch.Tensor(xtest).float()
Y = torch.Tensor(ytest).long()
out = net(X)
_, predicted = torch.max(out.data, 1)

#get accuration
print('Accuracy of testing %.2f %%' % (100 * torch.sum(Y==predicted).double() / len(Y)))
</code></pre>
<p>these are the specifics of my dataset:</p>
<ul>
<li>1747 training example stored in train.txt file,</li>
<li>600 testing example saved in test.txt</li>
<li>Each example has 4001 features.</li>
<li>Two Classes only labeled as 1 and 0.</li>
</ul>
<p>I would also take any advice regarding my code since I am a beginner</p>

ptrblck: <p>Your code seems to be a mix of different code snippets, as you are hitting these issues:</p>
<ul>
<li>
<code>torch.nn.functional</code> is imported as <code>f</code> but used as <code>F</code>
</li>
<li>
<code>net</code> is not initialized before the <code>optimizer</code> is created</li>
<li>
<code>DataLoader</code>s are created, but never used</li>
<li>
<code>Y</code> is used in the loss calculation while it seems <code>batch_Y</code> should be used (which creates the shape mismatch)</li>
<li>
<code>optmizer_batch</code> is used while <code>optimizer</code> is initialized</li>
</ul>
<p>Make sure to post an executable code snippet for faster debugging, as cleaning up the code often takes longer than spotting the actual error <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>