Epoch
Epoch
One pass of learning over the entire training data. Repeating multiple epochs improves the model.
Epoch
One pass of learning over the entire training data. Repeating multiple epochs improves the model.
An epoch is a unit that means one complete pass through the entire training dataset. For example, if there are 100,000 training samples and the batch size is 100, one epoch consists of 1,000 batch training steps (iterations).
The epoch concept is a basic unit of training that has been used since the early days of neural network training. Because a model cannot learn sufficiently by seeing the data only once, the same data is trained on repeatedly over multiple epochs, gradually optimizing the weights.
In each epoch, the model iterates through the entire dataset, computes the loss, and updates the weights through backpropagation. In general, as the number of epochs increases, the training loss decreases, but the validation loss may increase after a certain point (overfitting).
The appropriate number of epochs depends on the dataset size, model complexity, and learning rate. In practice, training usually starts at between 10 and 100 epochs, and with Early Stopping, training is terminated early when the validation loss no longer improves.
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, TensorDataset
# 샘플 데이터 생성
X = torch.randn(1000, 10) # 1000개 샘플, 10개 피처
y = torch.randint(0, 2, (1000,)) # 이진 분류
dataset = TensorDataset(X, y)
batch_size = 100 # 배치 사이즈
train_loader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
# 간단한 모델
model = nn.Sequential(
nn.Linear(10, 32),
nn.ReLU(),
nn.Linear(32, 2)
)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
# 에포크 단위 학습
num_epochs = 20
for epoch in range(num_epochs):
epoch_loss = 0.0
num_batches = 0
# 1 에포크 = 전체 데이터셋 1회 순회
for batch_X, batch_y in train_loader:
optimizer.zero_grad()
outputs = model(batch_X)
loss = criterion(outputs, batch_y)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
num_batches += 1
avg_loss = epoch_loss / num_batches
print(f"Epoch [{epoch+1}/{num_epochs}], "
f"Iterations: {num_batches}, "
f"Avg Loss: {avg_loss:.4f}")
# 총 iterations = num_epochs * (데이터 수 / 배치 사이즈)
# 이 경우: 20 * (1000/100) = 200 iterations
print(f"\n총 iterations: {num_epochs * len(train_loader)}")
"We've trained up to 50 epochs now, and the validation loss has kept increasing since epoch 30. I think we should set Early Stopping to 30 epochs or strengthen the regularization."
"The difference between an epoch and an iteration is that an epoch is one pass through the entire dataset, while an iteration is the processing of one batch. With 10,000 data samples and a batch size of 100, one epoch is 100 iterations."
"In large-scale LLM training, the amount of training is measured by the number of tokens rather than by epochs. According to the Chinchilla paper, training on 20 times as many tokens as the number of model parameters is compute-optimal."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Using a fixed value such as always 100 epochs leads to overfitting or unnecessary training time. Terminate dynamically with Early Stopping.
Looking at the training loss alone cannot detect overfitting. You should record and compare the validation loss at every epoch as well.
Set the Early Stopping patience to 5–10, and save the best model based on the validation loss. Using it together with a learning rate scheduler is more effective.