1/70
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
my_tensor = torch.tensor([0, 1, 2, 3, 4])
Create tensor my_tensor as a list of numbers 0 to 4
my_tensor.dtype
Get the datatype from my_tensor
my_tensor.type()
Get the tensor type from my_tensor (back)
type(my_tensor)
Get the tensor type from my_tensor (front)
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
my_tensor = my_tensor.type(torch.FloatTensor)
Convert tensor my_tensor to a float tensor
my_tensor.size()
Get the size of my_tensor
my_tensor.ndimension()
Get the number of dimensions of my_tensor
my_tensor = my_tensor.view(5,1)
Change the size of my_tensor to 5x1
my_tensor = my_tensor.view(-1, 1)
Convert my_tensor into a column
my_tensor = torch.from_numpy(np_a)
Create tensor my_tensor from numpy array np_a
np_a = my_tensor.numpy()
Create numpy array np_a from tensor my_tensor
my_tensor = torch.from_numpy(pd_series.values)
Create tensor my_tensor from pandas series pd_series
my_tensor[0].item()
Get the first number from tensor my_tensor as a python number
my_list = my_tensor.tolist()
Create a list my_list from tensor my_tensor
my_tensor[0] = 100
Change the first number of my_tensor to 100
my_tensor[2:4] = torch.tensor([300, 400])
Change the third and fourth number of tensor my_tensor to 300 and 400.
subset_tensor = my_tensor[index_list]
Create tensor subset_tensor from tensor my_tensor using indexes contained in list index_list
my_tensor[index_list] = 9
Change values of tensor my_tensor's index values contained in list index_list to 9
my_tensor.mean()
Get the average from my_tensor
my_tensor.std()
Get the standard deviation from my_tensor
my_tensor.max()
Get the max value from my_tensor
my_tensor.min()
Get the min value from my_tensor
my_tensor = torch.tensor([0, np.pi/2, np.pi])
Create tensor my_tensor with values 0, half pi and pi.
sin_tensor = torch.sin(my_tensor)
Create tensor sin_tensor by taking the sin values of tensor my_tensor
my_tensor = torch.linspace(-2, 2, 9)
Create tensor my_tensor, which has 9 values evenly distributes between -2 and 2
my_tensor.numel()
Get the total number of values of my_tensor
my_tensor = torch.tensor(df.values)
Create tensor my_tensor from pandas dataframe df
0
What index is the vertical axis?
1
What index is the horizontal axis?
my_tensor[2, 4]
How do you get the element at row 3, and column 5 of my_tensor
23
In tensor torch.tensor([[11, 12, 13], [21, 22, 23], [31, 32, 33]]), what element is my_tensor[1,2]?
my_tensor[0, 0:2]
How to get the value on the 1st-row's first two columns from my_tensor?
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?
torch.mm(A, B)
How do you matrix multiple tensor A and B?
x = torch.tensor(my_list, requires_grad = True)
Create a tensor x from list my_list, which allows derivative calculations.
y.backward()
You just calculated y = x². How do you pass the gradient to x?
x.grad
You just used backpropagation. How do you get the slope from x?
x.detach().numpy()
How do you exclude gradient tracking and put a tensor x in a numpy array so you can plot it?
from torch.utils.data import Dataset
import Dataset.
import torchvision.transforms as transforms
import transforms.
X = toy_set(length = 50, transform = multi)
Create dataset X from class toy_set with length 50, using transform object multi.
2trans = transforms.Compose([multi(), add4()])
Combine two transformation objects multi and add4 and name the new object 2trans.
2trans(my_dataset[0])
Get the first value of dataset my_dataset, but have it be transformed using object 2trans.
trans_set = toy_set(transform = 2trans)
Create a transformed dataset trans_set using transformer object 2trans and Dataset class toy_set.
from PIL import Image
import Image
from torch.utils.data import DataLoader
import DataLoader
file_path = os.path.join(directory_name, file_name)
Combine directory_name with file_name file into file_path.
df.iloc[0,1]
Get the first row and 2nd column from a dataframe df
image = Image.open(file_path)
Get image using file_path
df = pd.read_csv(file_path)
Create dataframe df from a csv file at file_path
transforms.RandomVerticalFlip(p=1)
What transformer do you use to flip all images in a dataset vertically?
transforms.RandomHorizontalFlip(p=1)
What transformer do you use to flip all images in a dataset horizontally?
import torchvision.datasets
What library to import MNIST?
self.linear = nn.Linear(4, 2)
Create a linear layer called self.linear with 4 input and 2 output
self.bn = nn.BatchNorm1d(6)
Create a batch normalize layer called self.bn which processes 6 neurons
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.
self.pool = nn.MaxPool2d(kernel_size = 2)
Create a pooling layer called self.pool that takes the greatest value in a 2×2 kernel
self.cnn_bn = nn.BatchNorm2d(6)
Create a batch normalize layer for a convolutional layer with 6 outputs. Call it self.cnn_bn
x = torch.relu(x)
Apply relu to x
torch.cuda.is_available()
Check whether a gpu is available
my_device = torch.device(“cuda:0”)
Create my_device with cuda:0
my_tensor.to(my_device)
Send my_tensor to my_device
my_model.to(my_device)
Send the tensor in the my_model constructor to my_device
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
self.drop = nn.Dropout(p= 0.2)
Create a drop out layer self.drop with a dropout probability of 20%
torch.nn.init.xavier_uniform_(linear1.weight)
Initialize the weights of layer linear1 using Xavier.
torch.nn.init.kaiming_uniform_(linear1.weight, nonlinearity="relu")
Initialize the weights of layer linear1 using He.
my_model.train()
Turn on dropout for my_model
my_model.eval()
Turn off dropout for my_model
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).