Tutorial 1 Flashcards

1
Q

torch.tensor()

A

A torch.Tensor is a multi-dimensional matrix containing elements of a single data type

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

How CPU and GPU torch.tensor() types exist?

A

8 CPU and 8 GPU

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

Can a torch.tensor() be created from a Python list?

A

Yes, like torch.tensor(np.array([[1, 2, 3], [4, 5, 6]]))

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

Does torch.tensor() copy data?

A

Yes. If you want to change a numpy array to a tensor, use as_tensor()

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

How do you use slice notation with torch.tensor()?

A

Like you would a numpy array.

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

what is requires_grad argument in torch.tensor()?

A

It allows Pytorch to record operations including that tensor for automatic differentiation.

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

How do you see the value of a torch.tensor()?

A

.item()

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

Where do gradients accumulate when calling .backward()?

A

In the leaves

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

What does a .backward() call do?

A

Computes the dloss/dx for every gradient enabled variable, and accumulates into the x.grad for every parameter x

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

torch.randn(size)?

A

provides a pytorch tensor with each element sampled from the standard normal distribution

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

torch.nn.Linear(in_features, out_features, bias=True)?

A

Applies a linear transformation to the incoming data: y = xW^T + b. W and b are randomly initialized

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

what does nn.loss() create?

A

Creates a criterion, which you can then supply an input and target for that loss function.

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

what does optimizer.step() do?

A

Updates the parameters based on current gradients stored in the .grad attribute of each paramter.

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

What is a Pytorch parameter?

A

A parameter is still a Tensor, much like a normal variable, however when associated with a model, it becomes part of the model attributes. This means we can easily feed model parameters to the optimizer. Parameters automatically has requires_grad() set to true.

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

What does sub() and add() do?

A

Add the argument to the tensor. If multiple elements, the argument is added to each element.

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

What is the difference between sub() and sub_()?

A

sub_() is an in-place tensor operation.

17
Q

How do you convert from Pytorch tensor to a numpy array?

A

.numpy()

18
Q

What methods do you have to override to create a custom python dataset?

A

__len__(self) and __getitem__(self, index)

19
Q

What should __len__() do in a dataset?

A

Give the length of the dataset

20
Q

What should __getitem__(index) do in a dataset?

A

Give you the value in the dataset at index, after loading, transforming, etc.

21
Q

What should __init__(self) do in a dataset?

A

Initialize filepaths or list of filenames

22
Q

What does .state_dict() return

A

A dictionary containing the whole state of the module.

23
Q

How do you save a pytorch model?

A

You can do torch.save(model, ‘model.ckpt’), or torch.save(model.state_dict, ‘model.ckpt’). The latter is preferred as then intermediates are not saved.

24
Q

How do you load from state_dict?

A

model = TheModelClass(*args, **kwargs)

model.load_state_dict(torch.load(PATH)). Note you have to load the model first before you need to load_state_dict