CUDA and CPU
device = "cuda" if torch.cuda.is_available() else "cpu"
# move the array to a device
torch_arr = torch_arr.to(device)
print(torch_arr.device)
# move to cuda
torch_arr = torch_arr.to("cuda")
torch_arr = torch_arr.to("cuda:0") # GPU at idx 0
torch_arr = torch_arr.cuda()
# move to cpu
torch_arr = torch_arr.to("cpu")
torch_arr = torch_arr.cpu()
NumPy Array to Torch Tensor (CPU)
# Numpy to Torch
torch_arr = torch.from_numpy(np_arr) # cpu tensor
# Torch to Numpy
np_arr = torch_arr.cpu().numpy() # first move to cpu
Type Checking
type(torch_arr.cuda())
# torch.cuda.FloatTensor
type(torch_arr.cpu())
# torch.cpu.FloatTensor
type(np_arr)
# numpy.ndarray
A neural network model and its components can be represented by a nn.Module
class.
class MLP(nn.Module):
def __init__(self, ):
super().__init__() # you have to call this in all child class!
self.layer1 = nn.Linear(764, 100) # nn.Linear also inherits nn.Module and implements Linear layer (y = w*x + b)
self.layer2 = nn.Linear(100, 10)
def forward(self, x): # forward is called in __call__() so that you can run the forward pass just by module(x)
return self.layer2(F.relu(self.layer(x)))
__init__
: defines the parts that make up the model (sub-module or parameters)forward
: performs the actual forward computationPyTorch pre-defines common modules of the modern deep neural networks. See more at basic building blocks