5 Must-Know Python Concepts for AI Engineers 1. š„ Tensors & Autograd Stop writing backprop by hand. requires_grad=TrueĀ tracks every operation ā .backward()Ā applies the chain rule automatically. import torch x = torch.tensor(2.0) y = torch.tensor(5.0) w = torch.tensor(0.5, requires_grad=True) b = torch.tensor(0.1, requires_grad=True) pred = w * x + b loss = (pred - y) ** 2 loss.backward() print(w.grad.item(), b.grad.item()) ā Exact gradients, zero math errors. 2. āļø The __call__Ā Method Why model(x) works, not model.forward(x). callĀ runs hooks before forward. class LinearLayer: Ā Ā Ā def __init__(self, w, b): Ā Ā Ā Ā Ā Ā Ā self.w, self.b = w, b Ā Ā Ā Ā Ā Ā Ā self._hooks = [] Ā Ā Ā def __call__(self, x): Ā Ā Ā Ā Ā Ā Ā for hook in self._hooks: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā hook(x) Ā Ā Ā Ā Ā Ā Ā return self.forward(x) Ā Ā Ā def forward(self, x): Ā Ā Ā Ā Ā Ā Ā return x * self.w + self.b ā ļø Always call model(x) ā .forward() skips hooks ā silent bugs. 3. š¾ Pickle vs ONNX pickle = Python-locked + code execution risk šØ. ONNX = static, language-agnostic graph. import torch model.eval() dummy_input = torch.randn(1, 10) torch.onnx.export( Ā Ā Ā model, dummy_input, "model.onnx", Ā Ā Ā export_params=True, Ā Ā Ā opset_version=15, Ā Ā Ā input_names=["input"], Ā Ā Ā output_names=["output"], Ā Ā Ā dynamic_axes={"input": {0: "batch_size"}} ) ā Portable, fast, decoupled from training code. 4. š§± Abstract Base Classes @abstractmethodĀ forces subclasses to implement methods. Miss one ā fails at startup, not mid-request. from abc import ABC, abstractmethod class ModelInterface(ABC): Ā Ā Ā @abstractmethod Ā Ā Ā def predict(self, x: list) -> list: ... Ā Ā Ā @abstractmethod Ā Ā Ā def get_metadata(self) -> dict: ... ā Fail fast, fail safe. 5. š Env Variables & Secrets Never hardcode keys. Store in .env, gitignore it, load with python-dotenv. import os from dotenv import load_dotenv load_dotenv() api_key = os.getenv("OPENAI_API_KEY") if not api_key: Ā Ā Ā raise ValueError("OPENAI_API_KEY is not set!") ā Same code locally + Docker/Lambda. Zero leaks. ā¤ļø FollowĀ Ā for more
16
1