快速搭建神经网络

Posted archer-fang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快速搭建神经网络相关的知识,希望对你有一定的参考价值。

原来-方式一:

# class Net(torch.nn.Module):     # 继承 torch 的 Module
#     def __init__(self, n_feature, n_hidden, n_output):
#         super(Net, self).__init__()     # 继承 __init__ 功能
#         self.hidden = torch.nn.Linear(n_feature, n_hidden)   # 隐藏层线性输出
#         self.out = torch.nn.Linear(n_hidden, n_output)       # 输出层线性输出
#
#     def forward(self, x):
#         # 正向传播输入值, 神经网络分析出输出值
#         x = F.relu(self.hidden(x))      # 激励函数(隐藏层的线性值)
#         x = self.out(x)                 # 输出值, 但是这个不是预测值, 预测值还需要再另外计算
#         return x
# net = Net(n_feature=2, n_hidden=10, n_output=2) # 几个类别就几个 output

输出:

"""
Net (
  (hidden): Linear (1 -> 10)
  (predict): Linear (10 -> 1)
)

 

现在-方式二:

net = torch.nn.Sequential(
    torch.nn.Linear(2, 10),
    torch.nn.ReLU(),
    torch.nn.Linear(10, 2)
)

输出:

"""
Sequential (
  (0): Linear (1 -> 10)
  (1): ReLU ()
  (2): Linear (10 -> 1)
)
"""

比较:

方式一适合于个性化的向前传播配置,如使用RNN。但是麻烦。

方式二搭建简单,省略了很多的过程

以上是关于快速搭建神经网络的主要内容,如果未能解决你的问题,请参考以下文章

c#代码片段快速构建代码

centos7+上搭建cobblerweb远程快速装机

在PaddlePaddle中的Notebook代码片段

pytorch学习-4:快速搭建+保存提取

pytorch学习-4:快速搭建+保存提取

前端开发工具vscode如何快速生成代码片段