Training, also called fitting or learning, is the process of teaching a model a mapping from input features to target labels or values. Each training instance is one example in the training set; its features describe it, and its label is the property to predict. In drug discovery, features can encode a molecule’s structure, while a label records a property of interest. section 3.0 section 3.3.2
Training follows data preparation and featurization. After fitting, evaluate the model and compare its predictions with known targets. The goal is not merely to memorize training examples, but to generalize to unseen inputs. Memorization at the expense of new-data performance is called overfitting. section 3.0 section 1.2.2
Training a hERG blocker classifier
A linear classifier learns to predict whether a molecule blocks the hERG channel from its molecular fingerprint. The dataset has a predefined split of 392 training instances and 195 test instances. A Scikit-Learn SGDClassifier is fitted on the training fingerprints and labels. Its training accuracy is 96.4%, but that score alone does not establish performance on unseen molecules. section 3.3.2
Listing 3.7 Training a Linear Classifier
from sklearn.linear_model import SGDClassifier
RANDOM_SEED = 42
def split_data(df, split_col="Random Split"):
train = df[df[split_col].str.contains("Train", na=False)]
test = df[df[split_col].str.contains("Test", na=False)]
return (
train.sample(frac=1, random_state=RANDOM_SEED).reset_index(drop=True),
test.sample(frac=1, random_state=RANDOM_SEED).reset_index(drop=True),
)
train_set, test_set = split_data(herg_blockers)
train_fingerprints = np.stack(
[compute_fingerprint(mol, 2, 2048) for mol in train_set.mol]
)
train_labels = train_set.Class
def train_sgd_classifier(X_train, y_train, **kwargs):
params = {
'random_state': RANDOM_SEED,
'max_iter': 1000,
'tol': 1e-3
}
params.update(kwargs) #A
clf = SGDClassifier(**params) #B
clf.fit(X_train, y_train) #B
return clf
lin_cls = train_sgd_classifier(train_fingerprints, train_labels)The listing splits the data, computes training fingerprints, and fits the classifier. section 3.3.2
A neural network training loop makes the updates explicit. For each batch, it clears old gradients, produces predictions, calculates loss against the targets, computes gradients with backpropagation, and updates the parameters. Loss measures the discrepancy between predictions and targets. section 8.2.3
After an epoch—a pass through the training batches—validation measures loss on held-out data without updating the model. Comparing training and validation loss helps detect overfitting. If validation loss stops improving while training loss keeps falling, consider fewer epochs or early stopping. section 8.2.3
Listing 8.7 Train the Model
def train_model(
model, train_loader, val_loader, criterion, optimizer,
scheduler, device, num_epochs=10
):
model.train() #A
train_losses = [] #B
val_losses = []
for epoch in range(num_epochs): #C
running_loss = 0.0 #D
for inputs, targets in train_loader:
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad() #E
outputs = model(inputs) #F
loss = criterion(outputs, targets) #G
loss.backward() #H
optimizer.step() #I
running_loss += loss.item()
train_loss = running_loss / len(train_loader)
train_losses.append(train_loss)
model.eval() #J
val_loss = 0.0
with torch.no_grad(): #K
for inputs, targets in val_loader:
inputs, targets = inputs.to(device), targets.to(device)
outputs = model(inputs)
loss = criterion(outputs, targets)
val_loss += loss.item()
val_loss /= len(val_loader)
val_losses.append(val_loss)
print(f'Epoch {epoch+1}/{num_epochs}',
f'Train Loss: {train_loss:.4f}',
f'Val Loss: {val_loss:.4f}') #L
scheduler.step(val_loss) #M
model.train() #NThe PyTorch listing shows batch updates and a validation pass. section 8.2.3































