I am training a feed-forward neural network in PyTorch. When I use PyTorch’s DataLoader class, which includes batching, I experience a much longer training time. By much longer I mean about 200x longer…
The network architecture is a sequential network consisting of Linear layers and ReLu activation functions. The exact number of layers and nodes per layer can vary. Yet, no matter what numbers I choose, it is always slower when I use PyTorch’s DataLoader.
I’m running this code on an Apple Macbook Pro M2/2022. Therefore, I cannot use cuda in PyTorch, but I have tried mps, which does not speed things up.
My code can be seen below. X and y are the features and labels, respectively. There are 5 features and one label (just in case you’re wondering). I don’t know exactly how long this dataset is (self-recorded data), but my estimation is about 1-2 million rows. No matter what batch size I use in the DataLoader, just using batching is significantly slower than not using it.
model = Model() # This inherits from nn.Module and uses nn.Sequential with nn.Linear and
# nn.ReLu. The error is not in here as I have been using this model before
# X and y are columns from a dataframe that is probably about 1-2 million rows long
X_train = X
y_train = y
X_train = torch.FloatTensor(X_train)
y_train = torch.FloatTensor(y_train).view(-1, 1)
dataset = TensorDataset(X_train, y_train)
dataset = DataLoader(dataset, batch_size=64, shuffle=False)
error_func = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
epochs = 5000
minimal_loss = np.inf
for epoch in range(epochs):
for inputs, output in dataset:
y_predicted = model(inputs)
loss = error_func(y_predicted, output)
optimizer.zero_grad()
loss.backward()
optimizer.step()
I would be grateful for any help whatsoever!