Creating learnable parameters

In our neural network example, we have two learnable parameters, w and b, and two fixed parameters, x and y. We have created variables x and y in our get_data function. Learnable parameters are created using random initialization and have the require_grad parameter set to True, unlike x and y, where it is set to False. There are different practices for initializing learnable parameters, which we will explore in the coming chapters. Let's take a look at our get_weights function:

def get_weights():
w = Variable(torch.randn(1),requires_grad = True)
b = Variable(torch.randn(1),requires_grad=True)
return w,b

Most of the preceding code is self-explanatory; torch.randn creates a random value of any given shape.