pytorch快速搭建神经网络_Sequential操作

作者:troublemaker、 时间:2023-01-06 01:47:44 

之前用Class类来搭建神经网络


class Neuro_net(torch.nn.Module):
 """神经网络"""
 def __init__(self, n_feature, n_hidden_layer, n_output):
   super(Neuro_net, self).__init__()
   self.hidden_layer = torch.nn.Linear(n_feature, n_hidden_layer)
   self.output_layer = torch.nn.Linear(n_hidden_layer, n_output)

def forward(self, input):
   hidden_out = torch.relu(self.hidden_layer(input))
   out = self.output_layer(hidden_out)
   return out

net = Neuro_net(2, 10, 2)
print(net)

class类图结构:

pytorch快速搭建神经网络_Sequential操作

使用torch.nn.Sequential() 快速搭建神经网络


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

Sequential图结构

pytorch快速搭建神经网络_Sequential操作

总结:

我们可以发现,使用torch.nn.Sequential会自动加入激励函数, 但是 class类net 中, 激励函数实际上是在 forward() 功能中才被调用的

使用class类中的torch.nn.Module,我们可以根据自己的需求改变传播过程

如果你需要快速构建或者不需要过多的过程,直接使用torch.nn.Sequential吧

补充知识:【PyTorch神经网络】使用Moudle和Sequential搭建神经网络

Module:

init中定义每个神经层的神经元个数,和神经元层数;

forward是继承nn.Moudle中函数,来实现前向反馈(加上激励函数)


# -*- coding: utf-8 -*-
# @Time  : 2019/11/5 10:43
# @Author : Chen
# @File  : neural_network_impl.py
# @Software: PyCharm

import torch
import torch.nn.functional as F

#data
x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1)
y = x.pow(2) + 0.2 * torch.rand(x.size())

#第一种搭建方法:Module
# 其中,init中定义每个神经层的神经元个数,和神经元层数;
# forward是继承nn.Moudle中函数,来实现前向反馈(加上激励函数)
class Net(torch.nn.Module):
 def __init__(self):
   #继承__init__函数
   super(Net, self).__init__()
   #定义每层的形式
   #隐藏层线性输出feature->hidden
   self.hidden = torch.nn.Linear(1, 10)
   #输出层线性输出hidden->output
   self.predict = torch.nn.Linear(10, 1)

#实现所有层的连接关系。正向传播输入值,神经网络分析输出值
 def forward(self, x):
   #x首先在隐藏层经过激励函数的计算
   x = F.relu(self.hidden(x))
   #到输出层给出预测值
   x = self.predict(x)
   return x

net = Net()
print(net)

print('\n\n')

#快速搭建:Sequential
#模板:net2 = torch.nn.Sequential()

net2 = torch.nn.Sequential(
 torch.nn.Linear(1, 10),
 torch.nn.ReLU(),
 torch.nn.Linear(10, 1)
)
print(net2)

pytorch快速搭建神经网络_Sequential操作

来源:https://blog.csdn.net/weixin_44912159/article/details/105052329

标签:pytorch,神经网络,Sequential
0
投稿

猜你喜欢

  • MySql like模糊查询通配符使用详细介绍

    2024-01-24 12:25:03
  • 使用Python+Appuim 清理微信的方法

    2021-05-24 12:22:05
  • Python使用win32com.client的方法示例

    2021-03-22 14:32:48
  • PHP删除数组中指定值的元素常用方法实例分析【4种方法】

    2024-06-05 09:51:58
  • Vue3中简单实现动态添加路由

    2023-07-02 16:58:45
  • Vue3 全局实例上挂载属性方法案例讲解

    2023-07-02 16:46:14
  • 使用堆实现Top K算法(JS实现)

    2024-06-17 00:38:07
  • Yii框架登录流程分析

    2024-05-11 09:19:43
  • JavaScript状态模式及适配器模式使用讲解

    2024-04-19 10:14:05
  • 将SQL 2000日志迁移到SQL Server 2008

    2009-03-25 16:20:00
  • mysql 8.0.16 压缩包安装配置方法图文教程

    2024-01-14 13:06:39
  • vue keep-alive请求数据的方法示例

    2024-06-05 09:19:11
  • SQL Server高级内容之子查询和表链接概述及使用

    2024-01-24 22:34:27
  • python的mysql数据库建立表与插入数据操作示例

    2024-01-22 22:27:32
  • PHP.vs.JAVA

    2023-11-17 20:28:51
  • Thinkphp微信公众号支付接口

    2024-05-11 09:16:51
  • 通过Python把学姐照片做成拼图游戏

    2022-03-24 17:21:01
  • centOS7安装MySQL数据库

    2024-01-16 21:58:07
  • Python3 实现随机生成一组不重复数并按行写入文件

    2021-11-25 18:05:35
  • FusionCharts图表显示双Y轴双(多)曲线

    2023-08-22 17:55:38
  • asp之家 网络编程 m.aspxhome.com