PyTorch 1

0.0(0)
studied byStudied by 0 people
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/70

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

71 Terms

1
New cards

my_tensor = torch.tensor([0, 1, 2, 3, 4])

Create tensor my_tensor as a list of numbers 0 to 4

2
New cards

my_tensor.dtype

Get the datatype from my_tensor

3
New cards

my_tensor.type()

Get the tensor type from my_tensor (back)

4
New cards

type(my_tensor)

Get the tensor type from my_tensor (front)

5
New cards

my_tensor = torch.FloatTensor([0, 1, 2, 3, 4])

Create tensor my_tensor as a list of numbers 0 to 4. The numbers need to be floats

6
New cards

my_tensor = my_tensor.type(torch.FloatTensor)

Convert tensor my_tensor to a float tensor

7
New cards

my_tensor.size()

Get the size of my_tensor

8
New cards

my_tensor.ndimension()

Get the number of dimensions of my_tensor

9
New cards

my_tensor = my_tensor.view(5,1)

Change the size of my_tensor to 5x1

10
New cards

my_tensor = my_tensor.view(-1, 1)

Convert my_tensor into a column

11
New cards

my_tensor = torch.from_numpy(np_a)

Create tensor my_tensor from numpy array np_a

12
New cards

np_a = my_tensor.numpy()

Create numpy array np_a from tensor my_tensor

13
New cards

my_tensor = torch.from_numpy(pd_series.values)

Create tensor my_tensor from pandas series pd_series

14
New cards

my_tensor[0].item()

Get the first number from tensor my_tensor as a python number

15
New cards

my_list = my_tensor.tolist()

Create a list my_list from tensor my_tensor

16
New cards

my_tensor[0] = 100

Change the first number of my_tensor to 100

17
New cards

my_tensor[2:4] = torch.tensor([300, 400])

Change the third and fourth number of tensor my_tensor to 300 and 400.

18
New cards

subset_tensor = my_tensor[index_list]

Create tensor subset_tensor from tensor my_tensor using indexes contained in list index_list

19
New cards

my_tensor[index_list] = 9

Change values of tensor my_tensor's index values contained in list index_list to 9

20
New cards

my_tensor.mean()

Get the average from my_tensor

21
New cards

my_tensor.std()

Get the standard deviation from my_tensor

22
New cards

my_tensor.max()

Get the max value from my_tensor

23
New cards

my_tensor.min()

Get the min value from my_tensor

24
New cards

my_tensor = torch.tensor([0, np.pi/2, np.pi])

Create tensor my_tensor with values 0, half pi and pi.

25
New cards

sin_tensor = torch.sin(my_tensor)

Create tensor sin_tensor by taking the sin values of tensor my_tensor

26
New cards

my_tensor = torch.linspace(-2, 2, 9)

Create tensor my_tensor, which has 9 values evenly distributes between -2 and 2

27
New cards

my_tensor.numel()

Get the total number of values of my_tensor

28
New cards

my_tensor = torch.tensor(df.values)

Create tensor my_tensor from pandas dataframe df

29
New cards

0

What index is the vertical axis?

30
New cards

1

What index is the horizontal axis?

31
New cards

my_tensor[2, 4]

How do you get the element at row 3, and column 5 of my_tensor

32
New cards

23

In tensor torch.tensor([[11, 12, 13], [21, 22, 23], [31, 32, 33]]), what element is my_tensor[1,2]?

33
New cards

my_tensor[0, 0:2]

How to get the value on the 1st-row's first two columns from my_tensor?

34
New cards

my_tensor[1:3, 1] = 0

How to change the values on the second column and the 2~3 rows of my_tensor to 0?

35
New cards

torch.mm(A, B)

How do you matrix multiple tensor A and B?

36
New cards

x = torch.tensor(my_list, requires_grad = True)

Create a tensor x from list my_list, which allows derivative calculations.

37
New cards

y.backward()

You just calculated y = x². How do you pass the gradient to x?

38
New cards

x.grad

You just used backpropagation. How do you get the slope from x?

39
New cards

x.detach().numpy()

How do you exclude gradient tracking and put a tensor x in a numpy array so you can plot it?

40
New cards

from torch.utils.data import Dataset

import Dataset.

41
New cards

import torchvision.transforms as transforms

import transforms.

42
New cards

X = toy_set(length = 50, transform = multi)

Create dataset X from class toy_set with length 50, using transform object multi.

43
New cards

2trans = transforms.Compose([multi(), add4()])

Combine two transformation objects multi and add4 and name the new object 2trans.

44
New cards

2trans(my_dataset[0])

Get the first value of dataset my_dataset, but have it be transformed using object 2trans.

45
New cards

trans_set = toy_set(transform = 2trans)

Create a transformed dataset trans_set using transformer object 2trans and Dataset class toy_set.

46
New cards

from PIL import Image

import Image

47
New cards

from torch.utils.data import DataLoader

import DataLoader

48
New cards

file_path = os.path.join(directory_name, file_name)

Combine directory_name with file_name file into file_path.

49
New cards

df.iloc[0,1]

Get the first row and 2nd column from a dataframe df

50
New cards

image = Image.open(file_path)

Get image using file_path

51
New cards

df = pd.read_csv(file_path)

Create dataframe df from a csv file at file_path

52
New cards

transforms.RandomVerticalFlip(p=1)

What transformer do you use to flip all images in a dataset vertically?

53
New cards

transforms.RandomHorizontalFlip(p=1)

What transformer do you use to flip all images in a dataset horizontally?

54
New cards

import torchvision.datasets

What library to import MNIST?

55
New cards

self.linear = nn.Linear(4, 2)

Create a linear layer called self.linear with 4 input and 2 output

56
New cards

self.bn = nn.BatchNorm1d(6)

Create a batch normalize layer called self.bn which processes 6 neurons

57
New cards

self.cnn = nn.Conv2d(in_channels = 1, out_channels = 5, kernel_size = 3, padding = 2)

Create a convolutional layer called self.cnn, with 1 input channel, 5 output channels, 3 kernel size, 2 padding.

58
New cards

self.pool = nn.MaxPool2d(kernel_size = 2)

Create a pooling layer called self.pool that takes the greatest value in a 2×2 kernel

59
New cards

self.cnn_bn = nn.BatchNorm2d(6)

Create a batch normalize layer for a convolutional layer with 6 outputs. Call it self.cnn_bn

60
New cards

x = torch.relu(x)

Apply relu to x

61
New cards

torch.cuda.is_available()

Check whether a gpu is available

62
New cards

my_device = torch.device(“cuda:0”)

Create my_device with cuda:0

63
New cards

my_tensor.to(my_device)

Send my_tensor to my_device

64
New cards

my_model.to(my_device)

Send the tensor in the my_model constructor to my_device

65
New cards

my_optimizer = torch.optim.SGD(my_model.parameters(), lr = 0.1, momentum = 0.4)

Create optimizer my_optimizer for model my_model. Use a learning rate of 0.1 and a momentum of 0.4

66
New cards

self.drop = nn.Dropout(p= 0.2)

Create a drop out layer self.drop with a dropout probability of 20%

67
New cards

torch.nn.init.xavier_uniform_(linear1.weight)

Initialize the weights of layer linear1 using Xavier.

68
New cards

torch.nn.init.kaiming_uniform_(linear1.weight, nonlinearity="relu")

Initialize the weights of layer linear1 using He.

69
New cards

my_model.train()

Turn on dropout for my_model

70
New cards

my_model.eval()

Turn off dropout for my_model

71
New cards

self.layers = nn.ModuleList([nn.Linear(in_f, out_f) for in_f, out_f in zip(sizes, sizes[1:])])

Create self.layers, which is a module of layers that uses the list ‘sizes’ for the inputs and outputs (in_f, out_f).