├── README.md └── demo.py /README.md: -------------------------------------------------------------------------------- 1 | # pytorch_in_5_minutes 2 | This is the code for "PyTorch in 5 Minutes" by Siraj Raval on Youtube 3 | 4 | ## Overview 5 | 6 | This is the code for [this](https://youtu.be/nbJ-2G2GXL0) video on Youtube by Siraj Raval. It's meant to be a demonstration of PyTorch's key features (dynamic computation graphs and imperative programming). This script builds a simple 2 layer neural net that learns the mapping between randomly generated input and output data. 7 | 8 | ## Dependencies 9 | 10 | * pytorch (Install it using the instructions [here](http://pytorch.org/)) 11 | 12 | ## Usage 13 | 14 | Run `python demo.py` in terminal to see the results 15 | 16 | ## Credits 17 | 18 | Credits go to [jcohnson](https://github.com/jcjohnson). I've merely created a wrapper to get people started. 19 | -------------------------------------------------------------------------------- /demo.py: -------------------------------------------------------------------------------- 1 | # Code in file autograd/two_layer_net_autograd.py 2 | import torch 3 | from torch.autograd import Variable 4 | 5 | dtype = torch.FloatTensor 6 | # dtype = torch.cuda.FloatTensor # Uncomment this to run on GPU 7 | 8 | # N is batch size; D_in is input dimension; 9 | # H is hidden dimension; D_out is output dimension. 10 | N, D_in, H, D_out = 64, 1000, 100, 10 11 | 12 | # Create random Tensors to hold input and outputs, and wrap them in Variables. 13 | # Setting requires_grad=False indicates that we do not need to compute gradients 14 | # with respect to these Variables during the backward pass. 15 | x = Variable(torch.randn(N, D_in).type(dtype), requires_grad=False) 16 | y = Variable(torch.randn(N, D_out).type(dtype), requires_grad=False) 17 | 18 | # Create random Tensors for weights, and wrap them in Variables. 19 | # Setting requires_grad=True indicates that we want to compute gradients with 20 | # respect to these Variables during the backward pass. 21 | w1 = Variable(torch.randn(D_in, H).type(dtype), requires_grad=True) 22 | w2 = Variable(torch.randn(H, D_out).type(dtype), requires_grad=True) 23 | 24 | learning_rate = 1e-6 25 | for t in range(500): 26 | # Forward pass: compute predicted y using operations on Variables; these 27 | # are exactly the same operations we used to compute the forward pass using 28 | # Tensors, but we do not need to keep references to intermediate values since 29 | # we are not implementing the backward pass by hand. 30 | y_pred = x.mm(w1).clamp(min=0).mm(w2) 31 | 32 | # Compute and print loss using operations on Variables. 33 | # Now loss is a Variable of shape (1,) and loss.data is a Tensor of shape 34 | # (1,); loss.data[0] is a scalar value holding the loss. 35 | loss = (y_pred - y).pow(2).sum() 36 | print(t, loss.data[0]) 37 | 38 | # Use autograd to compute the backward pass. This call will compute the 39 | # gradient of loss with respect to all Variables with requires_grad=True. 40 | # After this call w1.grad and w2.grad will be Variables holding the gradient 41 | # of the loss with respect to w1 and w2 respectively. 42 | loss.backward() 43 | 44 | # Update weights using gradient descent; w1.data and w2.data are Tensors, 45 | # w1.grad and w2.grad are Variables and w1.grad.data and w2.grad.data are 46 | # Tensors. 47 | w1.data -= learning_rate * w1.grad.data 48 | w2.data -= learning_rate * w2.grad.data 49 | 50 | # Manually zero the gradients 51 | w1.grad.data.zero_() 52 | w2.grad.data.zero_() 53 | --------------------------------------------------------------------------------