用pytorch1.0快速搭建简单的神经网络

用pytorch1.0搭建简单的神经网络

import torch
import torch.nn.functional as F  # 包含激励函数

# 建立神经网络
# 先定义所有的层属性(__init__()), 然后再一层层搭建(forward(x))层于层的关系链接
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)   # hidden layer  # 隐藏层线性输出
        self.predict = torch.nn.Linear(n_hidden, n_output)   # output layer  # 输出层线性输出 ==== 定义层数

    def forward(self, x):       # 这同时也是 Module 中的 forward 功能
        # 正向传播输入值, 神经网络分析出输出值
        x = F.relu(self.hidden(x))      # activation function for hidden layer  # 激励函数(隐藏层的线性值)
        x = self.predict(x)             # linear output     # 输出值
        return x

net1 = Net(1, 10, 1)

# easy and fast way to build your network
net2 = torch.nn.Sequential(
    torch.nn.Linear(1, 10),     # 第一层
    torch.nn.ReLU(),            # 激活层
    torch.nn.Linear(10, 1)      # 输出层
)

print(net1)     # net1 architecture   ==  显示神经网络结构
"""
Net (
  (hidden): Linear (1 -> 10)
  (predict): Linear (10 -> 1)
)
"""
print(net2)     # net2 architecture
"""
Sequential (
  (0): Linear (1 -> 10)
  (1): ReLU ()
  (2): Linear (10 -> 1)
)
"""
net2多显示了一些内容, 这是为什么呢? 原来它把激励函数也一同纳入进去了,但是net1中, 激励函数实际上是在forward()功能中才被调用的。这也就说明了:
net1相比net2的好处就是, 你可以根据你的个人需要更加个性化你自己的前向传播过程, 比如(RNN).

 

posted @ 2019-07-14 22:04  土博姜山山  阅读(524)  评论(0编辑  收藏  举报