百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

Keras中的神经网络层及其应用指南

ztj100 2025-01-01 23:50 24 浏览 0 评论

随着神经网络的发展,现在有大量的神经网络层类型。 在本文中,我将介绍几个神经网络层,解释它们的作用和工作方式,并在Python流行的机器学习库Keras中展示它们的应用。

Core Layers

Dense

Dense层是标准的神经网络层,可输出-


-其中“activation”是通过“激活参数”传递的激活函数,“kernel”是由图层创建的权重矩阵,而“bias”是由图层创建的偏差矢量。 可以使用参数“ use_bias”进行调整,该参数可以设置为False。

# as first layer in a sequential model: 
model = Sequential() 
model.add(Dense(32, input_shape=(16,))) # now the model will take as input arrays of shape (*, 16) 
# and output arrays of shape (*, 32)  
# after the first layer, you don't need to specify 
# the size of the input anymore: 
model.add(Dense(32))

Activation Layer


激活层将激活功能应用于输出。 可以使用以下方法在Keras中完成…

keras.layers.Activation(activation)

…在参数“activation”中填写要使用的激活函数的名称,例如“ Sigmoid”或“ relu”。

Dropout Layer


Dropout Layer用于防止神经网络过度拟合,在训练期间每次更新时,将输入单元的分数“比率”随机设置为0。


这简化了神经网络并减少了训练时间。 可以在Keras中使用...实现Dropout层。

keras.layers.Dropout(rate, noise_shape=None, seed=None)

…其中rate表示介于0和1之间的浮点数,表示要下降的输入单位的分数,noise_shape表示一维整数张量,表示与输入相乘的二进制丢包掩码的形状,seed表示要使用的Python整数 作为随机种子。

Flatten Layer


Flatten layer将多维数据转换为单个向量进行处理。 例如,在应用Flatten层后,28 x 28的图像将成为784位长的矢量。 Keras的实现是…

keras.layers.Flatten()

Convolutional Layers


Conv1D


一维卷积层将卷积应用于两个一维数据类型(矢量,信号)。 长度为n的输入向量f和长度为m的核g的卷积为…


考虑此操作的一种方法是在输入图像上滑动内核-对于内核的每个位置,将内核和图像的重叠值相乘,然后求和。 它只是图像的“窗口平均值”。 一维卷积层是数据的概括,可以在Keras中实现为:

keras.layers.Conv1D(filters, kernel_size, strides=1)

参数如下:

  • filters — an integer representing the dimensionality of the output space, or the number of output filters in the convolution
  • kernel_size — an integer specifying the length of the 1D convolution window
  • strides — an integer specifying the stride length of the convolution
  • Conv2D


    与Conv1D相似,在图像上进行二维卷积意味着使用移动窗口对图像(二维数据)进行泛化。 图像和内核的每种排列后面的重叠值将相乘并求和。


    Keras中的二维卷积可以实现为

    keras.layers.Conv2D(filters, kernel_size, strides=(1, 1))

    参数:

  • filter: An integer describing the dimensionality of the output space (number of output filters in convolution)
  • kernel_size: a tuple/list of 2 integers, specifying the height and width of the 2D convolution window
  • strides — a tuple or list of 2 integers specifying the strides of each convolution along the height and width
    对于Conv3d,只需将“ Conv2D”更改为“ Conv3D”并添加其他尺寸。
  • Locally Connected Layer


    本地连接的层类似于卷积层,但是不是将单个mask应用于每个区域,而是将不同的mask应用于每个区域。 这确实会导致大量的超参数,并且仅应用于需要超参数和随后增加训练时间的高级图像识别问题中。

    keras.layers.LocallyConnected1D(filters, kernel_size, strides=1)

    与往常一样,将“ LocallyConnected1D”替换为“ LocallyConnected2D”和“ LocallyConnected3D”,并根据需要调整参数以获取其他尺寸。

    Pooling Layers


    注—在构建神经网络时,通常在卷积层之后是池化层或池化层以及激活函数。

    Max Pooling


    最大池化是将数据分成几个区域,获取这些区域中单元格的最大值,然后将其插入输出矩阵的过程。

    以下是一维最大池化的代码

    keras.layers.MaxPooling1D(pool_size=2, strides=None)

    参数:

  • Pool_size is an integer representing the size of the max pooling windows
  • Strides is an integer representing the factor by which to downscale — for instance, ‘2’ will halve the input
  • 对于2D和3D池化,只需将“ MaxPooling1D”替换为适当的名称(“ MaxPooling2D”和“ MaxPooling3D”),然后在必要的参数中添加一个附加的尺寸即可。


    Average Pooling


    平均池化类似于最大池化,但是不是取每个区域的最大值,而是取平均值。

    keras.layers.AveragePooling1D(pool_size=2, strides=None)

    参数:

  • pool_size is an integer representing the size of the max pooling windows
  • strides is an integer representing the factor by which to downscale.
  • 对于2D和3D平均池化,只需将“ AveragePooling1D”替换为适当的名称(“ AveragePooling2D”和“ AveragePooling3D”),然后在必要的参数中添加其他尺寸即可。

    相关推荐

    Java项目宝塔搭建实战MES-Springboot开源MES智能制造系统源码

    大家好啊,我是测评君,欢迎来到web测评。...

    一个令人头秃的问题,Logback 日志级别设置竟然无效?

    原文链接:https://mp.weixin.qq.com/s/EFvbFwetmXXA9ZGBGswUsQ原作者:小黑十一点半...

    实战!SpringBoot + RabbitMQ死信队列实现超时关单

    需求背景之为什么要有超时关单原因一:...

    火了!阿里P8架构师编写堪称神级SpringBoot手册,GitHub星标99+

    Springboot现在已成为企业面试中必备的知识点,以及企业应用的重要模块。今天小编给大家分享一份来着阿里P8架构师编写的...

    Java本地搭建宝塔部署实战springboot仓库管理系统源码

    大家好啊,我是测评君,欢迎来到web测评。...

    工具尝鲜(1)-Fleet构建运行一个Springboot入门Web项目

    Fleet是JetBrains公司推出的轻量级编辑器,对标VSCode。该款产品还在公测当中,具体下载链接如下JetBrainsFleet:由JetBrains打造的下一代IDE。想要尝试的...

    SPRINGBOOT WEB 实现文件夹上传(保留目录结构)

    网上搜到的SpringBoot的代码不多,完整的不多,能用的也不多,基本上大部分的文章只是提供了少量的代码,讲一下思路,或者实现方案。之前一般的做法都是使用HTML5来做的,大部都是传文件的,传文件夹...

    Java项目本地部署宝塔搭建实战报修小程序springboot版系统源码

    大家好啊,我是测评君,欢迎来到web测评。...

    新年IT界大笑料“工行取得基于SpringBoot的web系统后端实现专利

    先看看专利描述...

    看完SpringBoot源码后,整个人都精神了

    前言当读完SpringBoot源码后,被Spring的设计者们折服,Spring系列中没有几行代码是我们看不懂的,而是难在理解设计思路,阅读Spring、SpringMVC、SpringBoot需要花...

    阿里大牛再爆神著:SpringBoot+Cloud微服务手册

    今天给大家分享的这份“Springboot+Springcloud微服务开发实战手册”共有以下三大特点...

    WebClient是什么?SpringBoot中如何使用WebClient?

    WebClient是什么?WebClient是SpringFramework5引入的一个非阻塞、响应式的Web客户端库。它提供了一种简单而强大的方式来进行HTTP请求,并处理来自服务器的响应。与传...

    SpringBoot系列——基于mui的H5套壳APP开发web框架

      前言  大致原理:创建一个main主页面,只有主页面有头部、尾部,中间内容嵌入iframe内容子页面,如果在当前页面进行跳转操作,也是在iframe中进行跳转,而如果点击尾部按钮切换模块、页面,那...

    在Spring Boot中使用 jose4j 实现 JSON Web Token (JWT)

    JSONWebToken或JWT作为服务之间安全通信的一种方式而闻名。...

    Spring Boot使用AOP方式实现统一的Web请求日志记录?

    AOP简介AOP(AspectOrientedProgramming),面相切面编程,是通过代码预编译与运行时动态代理的方式来实现程序的统一功能维护的方案。AOP作为Spring框架的核心内容,通...

    取消回复欢迎 发表评论: