| title | Functions and Scope | ||||||
|---|---|---|---|---|---|---|---|
| sidebar_label | Functions | ||||||
| description | Mastering reusable code blocks in Python: defining functions, handling arguments, and understanding global vs. local scope in ML workflows. | ||||||
| tags |
|
In Machine Learning, we often repeat complex logic—calculating the distance between points, normalizing features, or computing gradients. Functions allow us to package this logic into reusable blocks, reducing errors and making our code "DRY" (Don't Repeat Yourself).
A function takes an input (parameters), performs an action, and returns an output.
def calculate_mse(y_true, y_pred):
"""Calculates Mean Squared Error."""
error = (y_true - y_pred) ** 2
return error.mean()graph LR
In["Input: $$y_{true}, y_{pred}$$"] --> Logic["Function Logic: $$(y - \hat{y})^2$$"]
Logic --> Out["Output: Mean Error"]
style Logic fill:#e1f5fe,stroke:#01579b,color:#333
Python offers flexible ways to pass data into functions, which is essential for managing dozens of hyperparameters.
- Positional: Order matters.
- Keyword: Explicitly naming parameters (safer and more readable).
def train_model(learning_rate, epochs):
print(f"LR: {learning_rate}, Epochs: {epochs}")
# Keyword arguments are preferred in ML for clarity
train_model(epochs=100, learning_rate=0.001)Useful for hyperparameters that have a standard "sane" default.
def initialize_weights(size, distribution="normal"):
# If distribution isn't provided, it defaults to "normal"
passFor simple, one-line operations, Python uses Lambda functions. These are frequently used in data cleaning with pandas.
# Convert Celsius to Fahrenheit for a feature
c_to_f = lambda c: (c * 9/5) + 32
print(c_to_f(0)) # 32.0Scope determines where a variable can be seen or accessed.
graph TD
Global["Global Scope: Variables defined outside any function"] --> Local["Local Scope: Variables defined inside a function"]
Local -->|Access| Global
Global -.->|Cannot Access| Local
- Global Scope: Variables available throughout the entire script (e.g., a dataset loaded at the top).
- Local Scope: Variables created inside a function (e.g., a temporary calculation). They "die" once the function finishes.
In advanced ML libraries (like Scikit-Learn), you'll see these used to pass a variable number of arguments.
*args: Passes a list of positional arguments.**kwargs: Passes a dictionary of keyword arguments.
def build_layer(**hyperparams):
for key, value in hyperparams.items():
print(f"{key}: {value}")
build_layer(units=64, activation="relu", dropout=0.2)In Python, you can pass a function as an argument to another function. This is how we pass different activation functions or optimizers into a training function.
def apply_activation(value, func):
return func(value)
# Passing the 'relu' function as data
output = apply_activation(10, relu)Functions help us stay organized, but sometimes code fails due to unexpected data or math errors. We need a way to catch those errors without crashing our entire training pipeline.
