PyTorch Tensor Flashcards

(66 cards)

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

What is PyTorch?

A

PyTorch is an open-source machine learning framework based on the Torch library

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

What is the main difference between PyTorch and NumPy?

A

PyTorch provides similar array/tensor operations as NumPy but also supports GPU acceleration and building/training neural networks.

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

What does PyTorch’s dynamic computational graph mean?

A

It means the computation graph is created on the fly as operations are executed

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

List the typical PyTorch workflow steps.

A

1) Prepare data; 2) Build model (e.g.

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

What is torch?

A

The core Python module of the PyTorch library

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

How do you import core PyTorch components in Python?

A

import torch as t; from torch import nn; import matplotlib.pyplot as plt

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

What does t.__version__ do?

A

Prints the version of the installed PyTorch library.

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

What does t.empty(5

A

3) do?

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

What is the risk of using t.empty()?

A

Values are arbitrary (may be NaN or random) because memory is allocated but not initialized.

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

How to create an empty 5x5 tensor?

A

t.empty(5

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

How do you create a tensor from a scalar?

A

t.tensor(10)

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

How do you check a tensor’s dimension?

A

tensor.ndim

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

How do you create a tensor from a list of floats?

A

t.tensor([5.5

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

How do you create a 2D tensor from nested lists?

A

t.tensor([[7

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

Given Q2 = [[2

A

3

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

How do you create a random tensor of shape 5x3?

A

t.rand(5

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

How do you create a random tensor from normal distribution?

A

t.randn(5

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

How do you create a tensor with the same shape as another but with random values?

A

t.randn_like(existing_tensor)

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

What does a.size() or a.shape return?

A

Returns the shape of tensor a as a tuple

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

How do you create a random RGB image tensor?

A

random_image_size = t.rand(size=(3

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

How to print shape

A

dimension

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

How do you convert a tensor to a PIL image?

A

Use torchvision.transforms.ToPILImage() and call it on the tensor.

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

What is the difference between torch.range and torch.arange?

A

torch.arange is preferred; torch.range is deprecated and differs from Python’s range; arange excludes the stop value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How do you create a tensor of integers from 0 to 9?
t.arange(10)
26
How do you get the min
max
27
How do you create tensors of zeros
ones
28
How do you create a zeros tensor with dtype long?
t.zeros(5
29
How do you create a tensor of ones with a specific dtype?
t.ones(5
30
How do new_* methods work (e.g.
new_ones
31
How do you convert the dtype of a tensor?
my_tensor.type(t.float16) or .to(dtype)
32
How do you create a tensor on GPU?
t.ones_like(existing_tensor
33
How do you move a tensor between devices?
tensor.to("cuda") or tensor.to("cpu"
34
How to convert Torch tensor to NumPy array?
a.numpy()
35
How to convert NumPy array to Torch tensor?
t.from_numpy(np_array)
36
What happens if you change a shared NumPy array or tensor?
The change is reflected in both because they share memory.
37
How do you create a tensor with gradient tracking?
t.tensor(data
38
How do you perform element-wise addition of tensors?
a+b or t.add(a
39
How do you perform element-wise subtraction
multiplication
40
What does an operation with an underscore (e.g.
add_) mean?
41
How do you reshape a tensor?
tensor.view(new_shape) or tensor.reshape(new_shape)
42
How do you flatten a tensor?
tensor.flatten()
43
How do you get a Python number from a one-element tensor?
tensor.item()
44
How do you index a tensor?
tensor[0]
45
How do you get the data type of a tensor?
tensor.dtype
46
What is a scalar tensor?
A 0-dimensional tensor (single value)
47
How do you create 1D
2D
48
What does t.transpose do?
Swaps the specified dimensions of a tensor
49
How do you transpose a 2D tensor?
tensor.T or t.transpose(tensor
50
What does unsqueeze() do?
Adds a dimension of size 1 at a specified position
51
What does squeeze() do?
Removes dimensions of size 1 from a tensor.
52
What does flatten() do?
Flattens all dimensions of a tensor into a 1D array.
53
What does resize() do?
Resizes or reshapes the tensor to new dimensions (use with care).
54
How do you convert an image to a tensor?
transforms.ToTensor()(image)
55
How do you convert a tensor to an image?
transforms.ToPILImage()(tensor)
56
Why is tensor shape (C
H
57
How do you select a single image from a batch tensor?
tensor[0]
58
Why should text/strings not be converted to tensors?
PyTorch tensors only support numeric types (float
59
How do you check the number of dimensions of a tensor?
tensor.ndim
60
How do you check the type of a tensor?
type(tensor)
61
How do you check a tensor's shape and size?
tensor.shape
62
How to perform comparison operations between tensors?
a==b
63
What is the PyTorch NumPy bridge?
You can convert tensors and NumPy arrays back and forth
64
How do you check if CUDA (GPU) is available?
torch.cuda.is_available()
65
How do you create a tensor directly on GPU?
t.ones_like(x
66
Where can you find documentation for 100+ tensor operations?
http://pytorch.org/docs/torch