liveBook · Manning

Generation

As explained in Build AI Drug Discovery Pipelines

Inference is the process of applying a trained model to input data to produce predictions.

Inference, or prediction, is the application of a trained model to input data to produce predictions. Unlike training, inference does not update the model’s learned weights. In drug discovery, a model might use molecular features to predict whether a compound inhibits an enzyme. section 3.3.1 section 5.1.2

A model’s usefulness depends on generalization: its ability to make accurate predictions on inputs it has not seen during training. A model that memorizes training examples may perform poorly on new compounds, especially when the training set lacks chemical diversity. section 1.2.2

Predicting enzyme inhibition

A logistic regression model predicts CYP3A4 inhibition from molecular fingerprints. Use predict() for class predictions and predict_proba() for class probability estimates. On the validation set, the model achieves 0.78 accuracy, compared with 0.55 for a model that always predicts the negative class. Its precision is 0.76 and recall is 0.74; these metrics give a more nuanced view than accuracy alone. section 5.1.2

For a PyTorch neural network, call model.eval() before validation or testing to switch off training-specific behavior. Use torch.no_grad() to avoid calculating gradients, then pass batches through the model. Validation loss can be compared with training loss to monitor generalization; a persistent gap can indicate overfitting. section 8.2.3

Listing 8.8 Evaluate the Model
def evaluate_model(model, test_loader, criterion, device):
    model.eval()
    total_loss = 0.0
    predictions = []
    actuals = []
    with torch.no_grad():
        for inputs, targets in test_loader:
            inputs, targets = inputs.to(device), targets.to(device)
            outputs = model(inputs)
            loss = criterion(outputs, targets)
            total_loss += loss.item()
            predictions.extend(outputs.cpu().numpy())
            actuals.extend(targets.cpu().numpy())
    
    predictions = np.array(predictions).flatten()
    actuals = np.array(actuals).flatten()
    
    mse = mean_squared_error(actuals, predictions)
    mae = mean_absolute_error(actuals, predictions)
    
    print(f'Test Loss: {total_loss/len(test_loader):.4f}')
    >>> Test Loss: 0.9235
    print(f'Mean Squared Error: {mse:.4f}')
    >>> Mean Squared Error: 0.9249
    print(f'Mean Absolute Error: {mae:.4f}')
    >>> Mean Absolute Error: 0.7274

The evaluation listing runs test batches without gradients, collects predictions and targets, and calculates error metrics. section 8.2.3

How Build AI Drug Discovery Pipelines covers it

How deep it goes
Defines frozen-weight prediction and demonstrates class outputs, probabilities, and batched neural-network evaluation.
Examples
Uses CYP3A4 inhibition predictions and neural-network test loss to illustrate inference.
What you can do after
Apply it via scikit-learn and PyTorch to produce predictions and evaluate them with appropriate metrics.
How it connects
Connects inference to training, generalization, overfitting, classification, and validation.

Also explained in

Compare how 29 books explain generation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI Drug Discovery Pipelines. The section links point to the parts of the book it draws on.