1 Star 0 Fork 71

第三方开源工程 / pytorch-doc-zh

forked from OpenDocCN / pytorch-doc-zh 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
14.md 2.76 KB
一键复制 编辑 原始数据 按行查看 历史
布客飞龙 提交于 2021-01-25 00:15 . 2021-01-25 00:15:32

PyTorch:自定义nn模块

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

经过训练的三阶多项式,可以通过最小化平方的欧几里得距离来预测y = sin(x)-pipi

此实现将模型定义为自定义Module子类。 每当您想要一个比现有模块的简单序列更复杂的模型时,都需要以这种方式定义模型。

import torch
import math

class Polynomial3(torch.nn.Module):
    def __init__(self):
        """
        In the constructor we instantiate four parameters and assign them as
        member parameters.
        """
        super().__init__()
        self.a = torch.nn.Parameter(torch.randn(()))
        self.b = torch.nn.Parameter(torch.randn(()))
        self.c = torch.nn.Parameter(torch.randn(()))
        self.d = torch.nn.Parameter(torch.randn(()))

    def forward(self, x):
        """
        In the forward function we accept a Tensor of input data and we must return
        a Tensor of output data. We can use Modules defined in the constructor as
        well as arbitrary operators on Tensors.
        """
        return self.a + self.b * x + self.c * x ** 2 + self.d * x ** 3

    def string(self):
        """
        Just like any class in Python, you can also define custom method on PyTorch modules
        """
        return f'y = {self.a.item()} + {self.b.item()} x + {self.c.item()} x^2 + {self.d.item()} x^3'

# Create Tensors to hold input and outputs.
x = torch.linspace(-math.pi, math.pi, 2000)
y = torch.sin(x)

# Construct our model by instantiating the class defined above
model = Polynomial3()

# Construct our loss function and an Optimizer. The call to model.parameters()
# in the SGD constructor will contain the learnable parameters of the nn.Linear
# module which is members of the model.
criterion = torch.nn.MSELoss(reduction='sum')
optimizer = torch.optim.SGD(model.parameters(), lr=1e-6)
for t in range(2000):
    # Forward pass: Compute predicted y by passing x to the model
    y_pred = model(x)

    # Compute and print loss
    loss = criterion(y_pred, y)
    if t % 100 == 99:
        print(t, loss.item())

    # Zero gradients, perform a backward pass, and update the weights.
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

print(f'Result: {model.string()}')

脚本的总运行时间:(0 分钟 0.000 秒)

下载 Python 源码:polynomial_module.py

下载 Jupyter 笔记本:polynomial_module.ipynb

由 Sphinx 画廊生成的画廊

1
https://gitee.com/gz_hengkang/pytorch-doc-zh.git
git@gitee.com:gz_hengkang/pytorch-doc-zh.git
gz_hengkang
pytorch-doc-zh
pytorch-doc-zh
master

搜索帮助