I am working on a simple regression problem (X,Y) with ~200K sample size and I tried batch-feeding and no-batch-feeding. It seems when I feed the data in batches, it is significantly slower than just using no-batch at all. I was expecting the otherwise. Here is my code. Can you guys let me know if there is any bug there causing this issue? I attached the major part of the code, thank you!
This is super slow
temp_dataloder = DataLoader(dataset = temp_data, batch_size = 256, shuffle = False)
learning_rate = 0.001
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr = learning_rate)
num_epochs = 100
for epoch in range(num_epochs):
for X, Y in iter(temp_dataloder):
X = X.to(device)
Y = Y.to(device)
y_pred = model(X).to(device)
loss = criterion(y_pred, Y)
loss.backward()
optimizer.step()
optimizer.zero_grad()
if (epoch+1)%10==0:
print(f’epoch : {epoch+1} loss: {loss.item()}’)
And this is super fast
X = temp_data.x
X = X.to(device)
Y = temp_data.y
Y = Y.to(device)
learning_rate = 0.001
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr = learning_rate)
num_epochs = 100
for epoch in range(num_epochs):
y_pred = model(X).to(device)
loss = criterion(y_pred, Y)
loss.backward()
optimizer.step()
optimizer.zero_grad()
if (epoch+1)%10==0:
print(f'epoch : {epoch+1} loss: {loss.item()}')