前馈神经网络(Feedforward Neural Network,FNN)是一种最基本的神经网络结构,其特点是信息从输入层经过隐藏层单向传递到输出层,没有反馈或循环连接

全连接神经网络(Fully Connected Neural Network,FCNN)是前馈神经网络的一种,每一层的神经元与上一层的所有神经元全连接,常用于图像分类、文本分类等任务。

一、构建连接神经网络

 (1)自定义网络类继承nn.Module

 (2)实现__init__方法,定义线性层组件

 (3)实现forward方法,实现前向传播

import torch
from torch import nn

class MyNet(nn.Module):#继承父类nn.Module
    def __init__(self,input_featrues,out_features):
        super().__init__()
        self.fc1 = nn.Linear(input_features, 64)
        self.fc2 = nn.Linear(64, 32)
        self.fc3 = nn.Linear(32, output_features)
    def forward(self, x):
        x = self.fc1(x)
        x = self.fc2(x)
        x = self.fc3(x)
        return x
model = MyNet(10, 1)
print(model)
"""
MyNet(
  (fc1): Linear(in_features=10, out_features=64, bias=True)
  (fc2): Linear(in_features=64, out_features=32, bias=True)
  (fc3): Linear(in_features=32, out_features=1, bias=True)
)
"""

1、单层网络直接使用Linear构建

model = nn.Linear(10, 1)
print(model)
#Linear(in_features=10, out_features=1, bias=True)

2、Sequential顺序容器,默认从上到下一次加载实现forward

model = nn.Sequential(
    nn.Linear(10, 64),
    nn.Linear(64, 32),
    nn.Linear(32, 10),
)
print(model)
"""
Sequential(
  (0): Linear(in_features=10, out_features=64, bias=True)
  (1): Linear(in_features=64, out_features=32, bias=True)
  (2): Linear(in_features=32, out_features=10, bias=True)
)
"""

二、全连接基本组件认知

import torch
from torch import nn,optim

def test01():
    #单层网络
    model = nn.Linear(128,10)

    #定义损失函数
    criterion = nn.MSELoss()

    #定义优化器
    opt = optim.SGD(model.parameters(),lr=0.01)
    
    # 数据准备
    x = torch.randn(1000, 128)
    y = torch.randn(1000, 10)
    
    for epoch in range(10):
        #得到预测值
        y_pred = model(x)

        #计算loss
        loss = criterion(y_pred,y)

        # 梯度清零
        opt.zero_grad()
        # 反向传播,计算梯度
        loss.backward()

        # 模型参数更新
        opt.step()

        print(f'epoch:{epoch}, loss:{loss.item()}')
"""
epoch:0, loss:1.3215197324752808
epoch:1, loss:1.3195496797561646
epoch:2, loss:1.3175891637802124
epoch:3, loss:1.3156381845474243
epoch:4, loss:1.3136966228485107
epoch:5, loss:1.3117645978927612
epoch:6, loss:1.3098417520523071
epoch:7, loss:1.3079285621643066
epoch:8, loss:1.306024432182312
epoch:9, loss:1.304129719734192
"""


if __name__ == '__main__':
    test01()

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐