skip to content

ML Frameworks & Libraries

The libraries you actually build and ship models with: PyTorch and TensorFlow for deep learning, Keras as the high-level API, scikit-learn for classical ML, OpenCV for vision, and the runtimes that run models on device. Interviews expect fluency in at least one stack and awareness of what the others trade away.

on this pageshow

explore

questions

198 · 7 sections

In PyTorch, what does requires_grad=True do to a tensor?

level: juniorimportance: must knowfreq 78%
basics
~10 s

requires_grad=True tells PyTorch to record every operation that uses the tensor, so a backward pass can compute a derivative with respect to it. After backward(), the gradient shows up in that tensor's .grad attribute.

open as a page

In PyTorch, what must a custom map-style Dataset implement, and what does DataLoader add?

level: juniorimportance: must knowfreq 80%
basics
~10 s

A map-style Dataset implements len, returning the number of samples, and getitem(index), returning one sample (often a tensor/label tuple). DataLoader wraps it and produces shuffled, collated mini-batches, optionally loaded by worker processes.

open as a page

How do you load a GPU-trained PyTorch checkpoint on a CPU-only inference box?

level: juniorimportance: must knowfreq 55%
basics
~20 s

Pass map_location to torch.load, for example torch.load(path, map_location="cpu"), so saved tensors are restored onto the CPU instead of the CUDA device they were saved from. Then load_state_dict into a model and move the model once with .to(device).

open as a page

In PyTorch, why call model(x) instead of model.forward(x)?

level: juniorimportance: must knowfreq 66%
basics
~10 s

Calling the module runs nn.Module.call, which fires registered forward pre-hooks and forward hooks around forward(). Calling model.forward(x) directly skips all of that machinery, so hook-based features silently stop working.

open as a page

Does torch.from_numpy() copy the NumPy array or share memory with it?

level: juniorimportance: must knowfreq 62%
basics
~10 s

torch.from_numpy() shares the array's buffer, so writing through either side is visible to the other. torch.tensor(arr) always copies. Tensor.numpy() shares as well, and only works for CPU tensors.

open as a page

Under tf.distribute MirroredStrategy, is model.fit's batch_size global or per GPU?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Global. Keras splits each batch evenly across the replicas, so batch_size=256 on four GPUs means 64 examples per GPU per step. To keep per-GPU work constant, raise the global number as you add devices.

open as a page

How do you send a prediction request to TensorFlow Serving's REST API?

level: juniorimportance: must knowfreq 58%
basics
~10 s

POST JSON to http://host:8501/v1/models/MODEL:predict with an "instances" list of examples; the response is a JSON object with a "predictions" list in the same order. Port 8501 is REST; 8500 is gRPC.

open as a page

Why does tf.data.Dataset.prefetch(tf.data.AUTOTUNE) go last in a pipeline?

level: middleimportance: must knowfreq 72%
basics
~20 s

prefetch decouples producing elements from consuming them: a background thread fills a buffer while the accelerator trains on the previous batch. Placed last, after batch, its buffer holds ready-to-use batches, so step N+1's input work overlaps step N's compute.

open as a page

In tf.data, what does shuffle(buffer_size) actually do, and why does its position matter?

level: middleimportance: must knowfreq 78%
basics
~20 s

tf.data's shuffle keeps a buffer of buffer_size elements, emits one at random from it, and refills the slot from the source. It is a sliding-window shuffle, not a global one, so a small buffer over sorted data barely mixes anything.

open as a page

What must be created inside a tf.distribute strategy.scope(), and why?

level: middleimportance: must knowfreq 80%
basics
~20 s

Anything that creates variables: the model, the optimizer, and any metric or custom tf.Variable. Inside the scope those become mirrored variables with one synchronized copy per replica. Datasets, fit calls, and the training step itself go outside.

open as a page

In Keras, what does ModelCheckpoint(save_best_only=True) change about when a file is written?

level: juniorimportance: must knowfreq 70%
basics
~20 s

With save_best_only=True, ModelCheckpoint writes a file only on epochs where the monitored value improves on the best seen so far. With the default False it writes every epoch. The monitored value is named by the monitor argument, which defaults to val_loss.

open as a page

In Keras, what does Dense(64) output for an input of shape (32, 10, 8)?

level: juniorimportance: must knowfreq 65%
basics
~20 s

Shape (32, 10, 64). A Dense layer transforms only the last axis, reusing one kernel of shape (8, 64) at every one of the 10 positions, so it holds 8 * 64 + 64 = 576 parameters no matter the batch or sequence length.

open as a page

In Keras, when is Sequential not enough and you need the Functional API?

level: juniorimportance: must knowfreq 82%
basics
~20 s

keras.Sequential models a straight stack: one input, one output, each layer feeding the next. Anything with branches, several inputs or outputs, skip connections, or one layer instance reused twice needs the Functional API, which wires an explicit graph of layer calls.

open as a page

In Keras 3, what does model.save('model.keras') store and how do you load it?

level: juniorimportance: must knowfreq 72%
basics
~10 s

model.save('model.keras') writes one zip archive holding the architecture config, the weights and the optimizer state. keras.saving.load_model('model.keras') rebuilds the whole model, ready to predict or resume training without re-running the build code.

open as a page

In Keras, what does model.compile() configure before you call fit()?

level: juniorimportance: must knowfreq 78%
basics
~20 s

model.compile() attaches the training configuration to a model: the optimizer that updates the weights, the loss that fit() minimises, and the metrics reported each epoch. Until you call it, fit() and evaluate() have nothing to optimise and raise an error.

open as a page

In scikit-learn, what does a trailing underscore in coef_ or mean_ mean?

level: juniorimportance: must knowfreq 56%
basics
~20 s

A trailing underscore marks state learned during fit, such as coef_, mean_ or n_features_in_. It does not exist on a freshly constructed estimator, and check_is_fitted uses the presence of such attributes to decide whether the estimator has been fitted.

open as a page

In scikit-learn, how do you read the output of confusion_matrix?

level: juniorimportance: must knowfreq 58%
basics
~20 s

confusion_matrix returns a square array where rows are true classes and columns are predicted classes, so C[i, j] counts samples of true class i predicted as class j. Classes appear in sorted order unless you pass labels= to fix the order.

open as a page

In scikit-learn, what does the stratify argument of train_test_split do?

level: juniorimportance: must knowfreq 80%
basics
~20 s

stratify=y tells train_test_split to preserve each class's proportion in both halves instead of splitting purely at random. Without it a rare class can land unevenly in the test set, or be missing from it entirely.

open as a page

In scikit-learn, what happens at each step when you call Pipeline.fit() then predict()?

level: juniorimportance: must knowfreq 72%
basics
~20 s

fit runs fit_transform on every step except the last, feeding each output into the next, then fit on the final estimator. predict runs transform only on those same intermediate steps — never fit again — and calls predict on the final estimator.

open as a page

When is scikit-learn's OrdinalEncoder a safe choice instead of OneHotEncoder?

level: juniorimportance: must knowfreq 68%
basics
~20 s

OrdinalEncoder maps each category to an integer in one column, which implies an ordering. That is safe for genuinely ordered features and for tree-based models that split on thresholds, but misleading for linear models, SVMs and distance-based estimators.

open as a page

In OpenCV, what input does cv2.findContours require and what does it return?

level: juniorimportance: must knowfreq 72%
basics
~20 s

cv2.findContours expects a single-channel 8-bit binary image with white objects on a black background - every non-zero pixel counts as foreground. In OpenCV 4 and 5 it returns two values: a list of contours and a hierarchy array.

open as a page

How do you load an ONNX model with cv2.dnn and run one inference?

level: juniorimportance: must knowfreq 68%
basics
~10 s

Call cv2.dnn.readNet("model.onnx") (or readNetFromONNX), build a blob with cv2.dnn.blobFromImage, pass it in with net.setInput(blob), then call net.forward() to get the output array. No training framework is needed at runtime.

open as a page

In OpenCV, what does detectAndCompute() return for a detector like SIFT or ORB?

level: juniorimportance: must knowfreq 62%
basics
~20 s

detectAndCompute returns a two-element tuple: a list of cv2.KeyPoint objects and a NumPy descriptor array with one row per keypoint. Row i describes keypoints[i]. When nothing is detected, the descriptor value is None, not an empty array.

open as a page

Which cv2.resize interpolation flag should you use when downscaling, and why?

level: juniorimportance: must knowfreq 72%
basics
~20 s

Use cv2.INTER_AREA. It averages over the source pixel area that maps to each output pixel, so detail is integrated rather than skipped. The default cv2.INTER_LINEAR samples only a few points and produces aliasing and moire on large reductions.

open as a page

Why do images read with cv2.imread() look blue when shown by matplotlib?

level: juniorimportance: must knowfreq 88%
basics
~10 s

cv2.imread() returns pixels in BGR channel order, while matplotlib's imshow expects RGB. The first and third channels are interpreted swapped, so reds render as blues. Convert first with cv2.cvtColor(img, cv2.COLOR_BGR2RGB).

open as a page

Why does a data-dependent if in forward() break torch.export for on-device use?

level: middleimportance: must knowfreq 58%
basics
~20 s

Export captures one static graph, so a branch whose condition is a tensor value either fails with a data-dependent guard error or gets frozen to whichever side the example input took. The device runtime has no Python to re-evaluate it.

open as a page

How does a PyTorch nn.Module become an ExecuTorch .pte file?

level: middleimportance: must knowfreq 70%
basics
~20 s

Three ahead-of-time steps: torch.export.export captures the model as an ExportedProgram graph, to_edge_transform_and_lower converts it to the Edge dialect and hands supported subgraphs to a backend, and to_executorch emits the flatbuffer you write out as .pte.

open as a page

Why does PT2 export quantization need a backend quantizer like XNNPACKQuantizer?

level: middleimportance: must knowfreq 48%
basics
~20 s

Because only the backend knows which operator patterns it can actually execute in int8. The quantizer encodes that contract, annotating exactly the nodes the delegate can fuse; quantizing anything else adds quantize/dequantize work around an op that still runs in float.

open as a page

In ExecuTorch int8 quantization, when do you need calibration data?

level: middleimportance: must knowfreq 52%
basics
~20 s

Only for static quantization, where activation scales are frozen ahead of time and must come from observed data. Dynamic quantization quantizes weights offline and computes activation scales at runtime per input, so no calibration set is required.

open as a page

What do prepare_pt2e and convert_pt2e do to an exported PyTorch graph?

level: middleimportance: must knowfreq 60%
basics
~20 s

prepare_pt2e inserts observer modules at the tensors a quantizer annotated, so a calibration or training run records their value ranges. convert_pt2e then replaces each observer with quantize/dequantize op pairs carrying the scale and zero-point it computed.

open as a page

How do you convert a TensorFlow SavedModel into a .tflite file with tf.lite.TFLiteConverter?

level: juniorimportance: must knowfreq 62%
basics
~10 s

Build a converter with tf.lite.TFLiteConverter.from_saved_model(path), then call convert(). It returns the model as a bytes FlatBuffer that you write to a .tflite file yourself. Nothing is quantized unless you set converter.optimizations.

open as a page

In LiteRT, what calls does a minimal Interpreter inference pass need?

level: juniorimportance: must knowfreq 72%
basics
~10 s

Construct the Interpreter with the .tflite file, call allocate_tensors(), read the input's index from get_input_details(), set_tensor(index, data), invoke(), then get_tensor() on the output index. Without allocate_tensors() there are no buffers to write into.

open as a page

Which tf.lite post-training quantization modes require a representative_dataset, and why?

level: middleimportance: must knowfreq 72%
basics
~20 s

Only full-integer quantization needs converter.representative_dataset. Dynamic-range and float16 touch weights alone, which the converter can read from the file. Integer activations need real sample inputs so the converter can observe each tensor's value range and pick a scale and zero-point.

open as a page

In LiteRT, what happens to ops a GPU or NNAPI delegate cannot run?

level: middleimportance: must knowfreq 66%
basics
~20 s

The graph is partitioned. The delegate claims the largest contiguous runs of ops it supports; everything else stays on CPU kernels. Nothing errors — but each partition boundary copies tensors between CPU and accelerator memory, so a fragmented graph can run slower than pure CPU.

open as a page

In TFLite conversion, what changes when you set converter.inference_input_type to tf.int8?

level: middleimportance: should knowfreq 44%
basics
~20 s

It removes the float boundary. By default even a fully quantized model exposes float32 input and output with quantize/dequantize ops at the edges; setting inference_input_type (and inference_output_type) to tf.int8 makes the model take and return raw integers, so the caller must apply the recorded scale and zero-point itself.

open as a page