Pytorch Flashcards

Understand Pytorch Syntax and general NN processes in Pytorch

1
Q

How does one create a tensor in pytorch?

A

x_tensor = torch.tensor(data) # directly from data
print(x_tensor)

np_array = np.array(data)
x_tensor = torch.from_numpy(np_array) # from numpy, this time infers a dtype
print(x_tensor)

x_ones_tensor = torch.ones_like(x_tensor) # this gets the shape of the data passed, then gives ones array
print(x_ones_tensor)

x_rand_tensor = torch.rand_like(x_tensor, dtype=torch.float) # this does the same as above, but random values
print(x_rand_tensor)

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

How do you get the shape of a tensor? type? device?

A

tensor.shape
tensor.dtype
tensor.device

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

How do you slice a tensor?

A

tensor[row,column]

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

How do you concatenate a tensor?

A

t1 = torch.cat([tensor,tensor,tensor], dim=1)

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

How do we multiply tensors?

A

tensor.mul(tensor) #element-wise

tensor*tensor # element wise

tensor.matmul(tensor) # matrix mult

tensor @ tensor # matrix mult

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

What are significance of functions with a “_” in pytorch?

A

They are in place operations

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

What is the typical procedure for creating a neural net in pytorch

A

Define the Model:
Create a subclass of torch.nn.Module, defining the network’s layers in __init__ and the forward pass in forward.

Instantiate the Model:
Create an instance of your model class.

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

Which imports are needed in pytorch when creating a neural net?

A

import torch
import torch.nn as nn
import torch.nn.functional as F

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

how do you set your device to gpu in pytorch

A

device = torch.device(‘cuda:0’ if torch.cuda.is_available() else ‘cpu’)

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

What is the general cycle of creating and training a neural net?

A
  1. Define the NN that has some learnable parameters (weights)
  2. iterate over a dataset of inputs
  3. process the input through the network
  4. compute the loss (how far the output is from being correct)
  5. propogate gradients back into the networks parameters
  6. update the weights of the network, using the SGD update rule: weight=weight - learning_rate*gradient
How well did you know this?
1
Not at all
2
3
4
5
Perfectly