PyTorch 实现与追问
- 题目来源
- Devinterview 其中第 85—146 题的题目索引来自 Devinterview,答案由 AIMLInterviews 重新撰写;原文的 50 题补充部分在此收录为 62 道,含拆分追问。
Implement the following function without Python loops: ```python def normalize_rows(x: torch.Tensor, eps: float = 1e-8) -> torch.Tensor: """Standardize each row to zero mean and unit variance.""" ... ``` Requirements: - x has shape [batch_size, num_features]. - Preserve x's device and dtype. - Avoid division by zero. - Do not modify x in place.
原题What should happen when a row has zero variance?
原题How would this change for [B, C, H, W]?
原题此追问承接前面的逐行标准化函数;四维输入未指定归一化的维度。
view, reshape, squeeze, and unsqueeze?
原题When can broadcasting be wrong?
原题Write a reusable preprocessing component that replaces non-finite values, standardizes features using training statistics, works on CPU or GPU, saves its state, and prevents validation leakage.
原题Implement binary precision, recall, and F1 from logits. Inputs may be on GPU, targets may contain only one class, no autograd graph should be created, and the result should contain Python floats.
原题Implement one training epoch and a complete evaluation pass with correct modes, gradient handling, device placement, sample-weighted loss, empty-loader behavior, and final predictions.
原题Review the following code: ```python def evaluate(model, loader): model.train() losses = [] for x, y in loader: prediction = model(x) loss = torch.nn.CrossEntropyLoss()(prediction, y) losses.append(loss) return torch.tensor(losses).mean().item() ```
原题Write a function that splits a sequence into batches while optionally retaining the final incomplete batch.
原题Implement a reproducible train/validation split without scikit-learn.
原题Write a generator that lazily reads and transforms records from a large dataset.
原题Flatten a nested dictionary while preserving a reversible mapping.
原题Merge metric dictionaries and compute weighted averages.
原题Implement an execution-time decorator.
原题Design a registry for losses, optimizers, or preprocessing functions.
原题Refactor a monolithic training script into testable modules.
原题Define a typed configuration object.
原题Make a utility deterministic without mutable global state.
原题When should you use a dataclass, function, class, or abstract base class?
原题What is wrong with mutable default arguments?
原题Explain shallow versus deep copies with an ML configuration.
原题Design errors for an invalid preprocessing shape or dtype.
原题Accept either NumPy arrays or PyTorch tensors without unclear behavior.
原题Compute the masked mean of [B, T, D] using a [B, T] mask.
原题Compute pairwise cosine similarity for [N, D] and [M, D].
原题Determine whether the true label is in the top-k predictions.
原题Implement one-hot encoding without F.one_hot.
原题Create label-smoothed targets.
原题Compute a batched confusion matrix without per-example loops.
原题Pad variable-length tensors and create a mask.
原题Gather one class score per example from [B, C].
原题Advanced indexing versus gather versus index_select?
原题Diagnose CPU targets with GPU predictions.
原题Explain contiguous tensors and .contiguous().
原题Find the squeeze() batch-size-one bug.
原题How do in-place operations affect autograd?
原题Compare detach(), clone(), and torch.no_grad().
原题Why can .item() slow a GPU loop?
原题Implement fit, transform, and inverse_transform standardization.
原题Normalize images with per-channel statistics.
原题Implement a custom Dataset for numerical and categorical features.
原题Write a collate_fn for variable-length sequences.
原题Handle missing values without leakage.
原题Handle an unseen category at inference.
原题Where should preprocessing live?
原题Make stochastic augmentation reproducible with DataLoader workers.
原题Why might multiple DataLoader workers be slower?
原题Pinned memory and non-blocking transfers?
原题Implement binary accuracy from logits.
原题Implement multiclass top-k accuracy.
原题Micro, macro, and weighted F1?
原题Masked mean squared error?
原题MAPE and its failure cases?
原题Confusion matrix without storing all predictions?
原题Design a streaming metric.
原题Why is averaging batch F1 wrong?
原题Metrics for imbalanced fraud detection?
原题How should a classification threshold be selected?
原题Implement early stopping.
原题Add gradient clipping.
原题Add gradient accumulation.
原题Add automatic mixed precision.
原题Save and resume a checkpoint exactly.
原题When should a learning-rate scheduler step?
原题Freeze and later unfreeze a backbone.
原题Why does validation differ between train() and eval()?
原题Prevent gradients during validation.
原题Calculate epoch loss with a smaller final batch.
原题Handle a non-finite loss.
原题Support classification and regression in one loop.
原题Add callback hooks cleanly.
原题Move nested batches to a device.
原题Preserve input order and sample identifiers during prediction.
原题Test a standardization utility, including constant features.
原题Verify evaluation does not update model state.
原题Compare CPU and GPU metric implementations.
原题Test empty input, batch size one, and unexpected shapes.
原题Use a tiny dataset to verify overfitting.
原题Diagnose a loss that never changes.
原题Diagnose validation changes across identical runs.
原题Find a memory leak from retained graphs.
原题Check gradients numerically.
原题Verify identical predictions after checkpoint restoration.
原题What is PyTorch, and how does it differ from frameworks such as TensorFlow?
原题What are tensors in PyTorch?
原题What is the difference between a Tensor and a Variable?
原题How do you convert a NumPy array to a tensor?
原题What is the purpose of .grad?
原题What is CUDA, and how does it relate to PyTorch?
原题How does autograd work?
原题What are the main steps for creating a neural-network model?
原题How does nn.Sequential differ from subclassing nn.Module?
原题How do you implement a custom layer?
原题What is the role of forward?
原题What are optimizers, and how are they used?
原题Why and when do you call zero_grad()?
原题How do you implement learning-rate scheduling?
原题Describe backpropagation in PyTorch.
原题How does gradient clipping work, and why is it useful?
原题How do you check whether a model is using the GPU?
原题How do you monitor and reduce overfitting?
原题What is batch normalization, and how does it affect training?
原题How does PyTorch initialize neural-network weights?
原题What common training problems occur, and how do you debug them?
原题How do you build a DataLoader for a custom dataset?
原题What are torchvision transforms used for?
原题How do you preprocess time-series data for RNNs?
原题What is data augmentation, and how is it implemented?
原题How do you use GPUs for distributed PyTorch training?
原题How do you implement transfer learning?
原题Compare RNNs, LSTMs, and GRUs.
原题What is TorchScript, and how does it help deployment?
原题Implement a DataLoader for a CSV dataset.
原题Demonstrate slicing, indexing, concatenation, and transposition.
原题Create a feedforward MNIST model.
原题Manually compute linear-regression gradients.
原题Implement a CNN for image classification.
原题Save and load a trained model.
原题How do you handle imbalanced classes?
原题How can PyTorch support real-time inference?
原题When would you convert a model to ONNX?
原题How would you deploy a model as a REST API?
原题How do you fine-tune a pretrained model?
原题What are GNNs, and how are they implemented in PyTorch?
原题What are important NAS directions, and how can PyTorch support them?
原题How are GANs implemented, and what makes them difficult?
原题What is model quantization, and when is it useful?
原题What role does PyTorch play in reinforcement learning?
原题How do you create a custom C++ or CUDA operation?
原题How should you discuss open-source contributions or community tools?
原题How should you discuss a project where PyTorch was central?
原题How do you improve experiment reproducibility?
原题How can PyTorch Lightning simplify a PyTorch workflow?
原题How would you normalize each row of a tensor without using a Python loop?
原题What is the difference between view, reshape, and squeeze?
原题How would you design a reusable preprocessing component?
原题What is data leakage, and how do you prevent it during preprocessing?
原题Explain precision, recall, and F1. When would you use each?
原题Why is averaging F1 across batches usually incorrect?
原题What are the essential steps in a PyTorch training loop?
原题What is the difference between model.eval() and torch.no_grad() or torch.inference_mode()?
原题What happens if you forget optimizer.zero_grad()?
原题How would you refactor a monolithic ML script into clean modules?
原题How would you write a utility that works on both CPU and GPU?
原题What tests would you write for a tensor preprocessing or metric utility?
原题What makes an ML utility reusable without excessive abstraction?
原题Which validation belongs at module boundaries?
原题When should a utility return tensors versus Python numbers?
原题How do you keep preprocessing consistent across training, evaluation, and inference?
原题How should an ML codebase be structured for independent testing?
原题What can PyTorch control about reproducibility?
原题How do you balance readable tensor code and vectorization?
原题What should a training loop log?
原题How do you review correctness across devices, dtypes, and shapes?
原题What changes before moving notebook code into production?
原题
暂无题目