Intro. to pytorch Flashcards

(15 cards)

1
Q

What is a torch.Tensor?

A

The primary data structure in PyTorch, a multi-dimensional array supporting GPU acceleration and autograd.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Name three factory methods to create tensors.

A

torch.zeros(shape), torch.ones(shape), torch.randn(shape) (also torch.arange, torch.linspace).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you slice a tensor to select specific elements or dimensions?

A

Use standard indexing and advanced indexing, e.g., tensor[:, 0], tensor[1:3, [0,2]].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between in-place and out-of-place tensor operations?

A

In-place operations (e.g., tensor.add_()) modify data directly; out-of-place create a new tensor, preserving the original.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you change the shape of a tensor?

A

Use reshape/view for general re-shaping, flatten to collapse dimensions, or stack to combine tensors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What do squeeze() and unsqueeze() do in PyTorch?

A

squeeze() removes dimensions of size 1; unsqueeze(dim) adds a dimension of size 1 at the specified index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you convert between torch.Tensor and numpy.ndarray?

A

Call tensor.numpy() to get a NumPy array; use torch.from_numpy(ndarray) or torch.tensor(ndarray) to go back.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a PyTorch Dataset and how do you access data samples?

A

A wrapper around data implementing __len__() and __getitem__(); use dataset[i] to get (input, label).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you use DataLoader to iterate over a Dataset?

A

Wrap with DataLoader(dataset, batch_size, shuffle) to automatically handle batching and shuffling.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you enable gradient tracking for tensors?

A

Set requires_grad=True when creating the tensor or call tensor.requires_grad_().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain the purpose of loss.backward() in PyTorch.

A

Computes gradients of the loss with respect to all tensors with requires_grad=True via reverse-mode autodiff.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What must you do before calling optimizer.step()?

A

Call optimizer.zero_grad() to clear old gradients, preventing accumulation across backward passes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you perform a parameter update step?

A

After loss.backward(), call optimizer.step() to adjust model parameters based on computed gradients.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you switch a model between training and evaluation modes?

A

Call model.train() for training (enables dropout, batchnorm updates) and model.eval() for inference (disables them).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does torch.no_grad() do and why is it used?

A

Context manager that disables gradient computation for inference, reducing memory use and speeding up execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly