PYTHORCH: NN

原文:https://pytorch.org/tutorials/beginner/examples_nn/polynomial_nn.html#sphx-glr-beginner-examples-nn-polynomial-nn-py

校对:DrDavidS

一个三阶多项式,通过最小化平方欧几里得距离来训练,并预测函数 y = sin(x)-pipi上的值。

这个实现使用 PyTorch 的nn包来构建神经网络。 PyTorch Autograd 让我们定义计算图和计算梯度变得容易了,但是原始的 Autograd 对于定义复杂的神经网络来说可能太底层了。 这时候nn包就能帮上忙。 nn包定义了一组模块,你可以把它视作一层神经网络,该神经网络层接受输入,产生输出,并且可能有一些可训练的权重。

  1. import torch
  2. import math
  3. # Create Tensors to hold input and outputs.
  4. x = torch.linspace(-math.pi, math.pi, 2000)
  5. y = torch.sin(x)
  6. # For this example, the output y is a linear function of (x, x^2, x^3), so
  7. # we can consider it as a linear layer neural network. Let's prepare the
  8. # tensor (x, x^2, x^3).
  9. p = torch.tensor([1, 2, 3])
  10. xx = x.unsqueeze(-1).pow(p)
  11. # In the above code, x.unsqueeze(-1) has shape (2000, 1), and p has shape
  12. # (3,), for this case, broadcasting semantics will apply to obtain a tensor
  13. # of shape (2000, 3)
  14. # Use the nn package to define our model as a sequence of layers. nn.Sequential
  15. # is a Module which contains other Modules, and applies them in sequence to
  16. # produce its output. The Linear Module computes output from input using a
  17. # linear function, and holds internal Tensors for its weight and bias.
  18. # The Flatten layer flatens the output of the linear layer to a 1D tensor,
  19. # to match the shape of `y`.
  20. model = torch.nn.Sequential(
  21. torch.nn.Linear(3, 1),
  22. torch.nn.Flatten(0, 1)
  23. )
  24. # The nn package also contains definitions of popular loss functions; in this
  25. # case we will use Mean Squared Error (MSE) as our loss function.
  26. loss_fn = torch.nn.MSELoss(reduction='sum')
  27. learning_rate = 1e-6
  28. for t in range(2000):
  29. # Forward pass: compute predicted y by passing x to the model. Module objects
  30. # override the __call__ operator so you can call them like functions. When
  31. # doing so you pass a Tensor of input data to the Module and it produces
  32. # a Tensor of output data.
  33. y_pred = model(xx)
  34. # Compute and print loss. We pass Tensors containing the predicted and true
  35. # values of y, and the loss function returns a Tensor containing the
  36. # loss.
  37. loss = loss_fn(y_pred, y)
  38. if t % 100 == 99:
  39. print(t, loss.item())
  40. # Zero the gradients before running the backward pass.
  41. model.zero_grad()
  42. # Backward pass: compute gradient of the loss with respect to all the learnable
  43. # parameters of the model. Internally, the parameters of each Module are stored
  44. # in Tensors with requires_grad=True, so this call will compute gradients for
  45. # all learnable parameters in the model.
  46. loss.backward()
  47. # Update the weights using gradient descent. Each parameter is a Tensor, so
  48. # we can access its gradients like we did before.
  49. with torch.no_grad():
  50. for param in model.parameters():
  51. param -= learning_rate * param.grad
  52. # You can access the first layer of `model` like accessing the first item of a list
  53. linear_layer = model[0]
  54. # For linear layer, its parameters are stored as `weight` and `bias`.
  55. print(f'Result: y = {linear_layer.bias.item()} + {linear_layer.weight[:, 0].item()} x + {linear_layer.weight[:, 1].item()} x^2 + {linear_layer.weight[:, 2].item()} x^3')

下载 Python 源码:polynomial_nn.py

下载 Jupyter 笔记本:polynomial_nn.ipynb