常用层

常用层对应于core模块,core内部定义了一系列常用的网络层,包括全连接、激活层等

Dense层

keras.layers.core.Dense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)

Dense就是常用的全连接层,所实现的运算是output = activation(dot(input, kernel)+bias)。其中activation是逐元素计算的激活函数,kernel是本层的权值矩阵,bias为偏置向量,只有当use_bias=True才会添加。

如果本层的输入数据的维度大于2,则会先被压为与kernel相匹配的大小。

这里是一个使用示例:

# as first layer in a sequential model:

# 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))

参数:

units:大于0的整数,代表该层的输出维度。

activation:激活函数,为预定义的激活函数名(参考激活函数),或逐元素(element-wise)的Theano函数。如果不指定该参数,将不会使用任何激活函数(即使用线性激活函数:a(x)=x)

use_bias: 布尔值,是否使用偏置项

kernel_initializer:权值初始化方法,为预定义初始化方法名的字符串,或用于初始化权重的初始化器。参考initializers

bias_initializer:偏置向量初始化方法,为预定义初始化方法名的字符串,或用于初始化偏置向量的初始化器。参考initializers

kernel_regularizer:施加在权重上的正则项,为Regularizer对象

bias_regularizer:施加在偏置向量上的正则项,为Regularizer对象

activity_regularizer:施加在输出上的正则项,为Regularizer对象

kernel_constraints:施加在权重上的约束项,为Constraints对象

bias_constraints:施加在偏置上的约束项,为Constraints对象

输入

形如(batch_size, ..., input_dim)的nD张量,最常见的情况为(batch_size, input_dim)的2D张量

输出

形如(batch_size, ..., units)的nD张量,最常见的情况为(batch_size, units)的2D张量

Activation层

keras.layers.core.Activation(activation)

激活层对一个层的输出施加激活函数

参数

activation:将要使用的激活函数,为预定义激活函数名或一个Tensorflow/Theano的函数。参考激活函数

输入shape

任意,当使用激活层作为第一层时,要指定input_shape

输出shape

与输入shape相同

Dropout层

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

为输入数据施加Dropout。Dropout将在训练过程中每次更新参数时按一定概率(rate)随机断开输入神经元,Dropout层用于防止过拟合。

参数

rate:0~1的浮点数,控制需要断开的神经元的比例

noise_shape:整数张量,为将要应用在输入上的二值Dropout mask的shape,例如你的输入为(batch_size, timesteps, features),并且你希望在各个时间步上的Dropout mask都相同,则可传入noise_shape=(batch_size, 1, features)。

seed:整数,使用的随机数种子

参考文献

Flatten层

keras.layers.core.Flatten()

Flatten层用来将输入“压平”,即把多维的输入一维化,常用在从卷积层到全连接层的过渡。Flatten不影响batch的大小。

例子

model = Sequential()

model.add(Convolution2D(64, 3, 3,

border_mode='same',

input_shape=(3, 32, 32)))

# now: model.output_shape == (None, 64, 32, 32)

model.add(Flatten())

# now: model.output_shape == (None, 65536)

Reshape层

keras.layers.core.Reshape(target_shape)

Reshape层用来将输入shape转换为特定的shape

参数

target_shape:目标shape,为整数的tuple,不包含样本数目的维度(batch大小)

输入shape

任意,但输入的shape必须固定。当使用该层为模型首层时,需要指定input_shape参数

输出shape

(batch_size,)+target_shape

例子

# as first layer in a Sequential model

model = Sequential()

model.add(Reshape((3, 4), input_shape=(12,)))

# now: model.output_shape == (None, 3, 4)

# note: `None` is the batch dimension

# as intermediate layer in a Sequential model

model.add(Reshape((6, 2)))

# now: model.output_shape == (None, 6, 2)

# also supports shape inference using `-1` as dimension

model.add(Reshape((-1, 2, 2)))

# now: model.output_shape == (None, 3, 2, 2)

Permute层

keras.layers.core.Permute(dims)

Permute层将输入的维度按照给定模式进行重排,例如,当需要将RNN和CNN网络连接时,可能会用到该层。

参数

dims:整数tuple,指定重排的模式,不包含样本数的维度。重拍模式的下标从1开始。例如(2,1)代表将输入的第二个维度重拍到输出的第一个维度,而将输入的第一个维度重排到第二个维度

例子

model = Sequential()

model.add(Permute((2, 1), input_shape=(10, 64)))

# now: model.output_shape == (None, 64, 10)

# note: `None` is the batch dimension

输入shape

任意,当使用激活层作为第一层时,要指定input_shape

输出shape

与输入相同,但是其维度按照指定的模式重新排列

RepeatVector层

keras.layers.core.RepeatVector(n)

RepeatVector层将输入重复n次

参数

n:整数,重复的次数

输入shape

形如(nb_samples, features)的2D张量

输出shape

形如(nb_samples, n, features)的3D张量

例子

model = Sequential()

model.add(Dense(32, input_dim=32))

# now: model.output_shape == (None, 32)

# note: `None` is the batch dimension

model.add(RepeatVector(3))

# now: model.output_shape == (None, 3, 32)

Lambda层

keras.layers.core.Lambda(function, output_shape=None, mask=None, arguments=None)

本函数用以对上一层的输出施以任何Theano/TensorFlow表达式

参数

function:要实现的函数,该函数仅接受一个变量,即上一层的输出

output_shape:函数应该返回的值的shape,可以是一个tuple,也可以是一个根据输入shape计算输出shape的函数

mask: 掩膜

arguments:可选,字典,用来记录向函数中传递的其他关键字参数

例子

# add a x -> x^2 layer

model.add(Lambda(lambda x: x ** 2))

# add a layer that returns the concatenation

# of the positive part of the input and

# the opposite of the negative part

def antirectifier(x):

x -= K.mean(x, axis=1, keepdims=True)

x = K.l2_normalize(x, axis=1)

pos = K.relu(x)

neg = K.relu(-x)

return K.concatenate([pos, neg], axis=1)

def antirectifier_output_shape(input_shape):

shape = list(input_shape)

assert len(shape) == 2 # only valid for 2D tensors

shape[-1] *= 2

return tuple(shape)

model.add(Lambda(antirectifier,

output_shape=antirectifier_output_shape))

输入shape

任意,当使用该层作为第一层时,要指定input_shape

输出shape

由output_shape参数指定的输出shape,当使用tensorflow时可自动推断

ActivityRegularizer层

keras.layers.core.ActivityRegularization(l1=0.0, l2=0.0)

经过本层的数据不会有任何变化,但会基于其激活值更新损失函数值

参数

l1:1范数正则因子(正浮点数)

l2:2范数正则因子(正浮点数)

输入shape

任意,当使用该层作为第一层时,要指定input_shape

输出shape

与输入shape相同

Masking层

keras.layers.core.Masking(mask_value=0.0)

使用给定的值对输入的序列信号进行“屏蔽”,用以定位需要跳过的时间步

对于输入张量的时间步,即输入张量的第1维度(维度从0开始算,见例子),如果输入张量在该时间步上都等于mask_value,则该时间步将在模型接下来的所有层(只要支持masking)被跳过(屏蔽)。

如果模型接下来的一些层不支持masking,却接受到masking过的数据,则抛出异常。

例子

考虑输入数据x是一个形如(samples,timesteps,features)的张量,现将其送入LSTM层。因为你缺少时间步为3和5的信号,所以你希望将其掩盖。这时候应该:

赋值x[:,3,:] = 0.,x[:,5,:] = 0.

在LSTM层之前插入mask_value=0.的Masking层

model = Sequential()

model.add(Masking(mask_value=0., input_shape=(timesteps, features)))

model.add(LSTM(32))

dropout层加在哪里_常用层 - Keras中文文档相关推荐

  1. keras中文文档_【DL项目实战02】图像识别分类——Keras框架+卷积神经网络CNN(使用VGGNet)

    版权声明:小博主水平有限,希望大家多多指导. 目录: [使用传统DNN] BG大龍:[DL项目实战02]图像分类--Keras框架+使用传统神经网络DNN​zhuanlan.zhihu.com [使用 ...

  2. python pptx库中文文档_基于python-pptx库中文文档及使用详解

    个人使用样例及部分翻译自官方文档,并详细介绍chart的使用 一:基础应用 1.创建pptx文档类并插入一页幻灯片 from pptx import Presentation prs = Presen ...

  3. python pptx教学_基于python-pptx库中文文档及使用详解

    个人使用样例及部分翻译自官方文档,并详细介绍chart的使用 一:基础应用 1.创建pptx文档类并插入一页幻灯片 from pptx import Presentation prs = Presen ...

  4. lavaral中文手册_【laravel7.x中文文档】路由

    路由 [TOC] 基本路由 构建基本路由只需要一个 URI 与一个 闭包 ,这里提供了一个非常简单优雅定义路由的方法: Route::get('foo', function () { return ' ...

  5. kibana 更新 索引模式_升级 Kibana - Kibana 中文文档

    IMPORTANT: 在升级 Kibana 之前: 请参考重要变更文档. 在升级生产服务之前请先在测试环境测试升级. 使用 Elasticsearch 的 snapshots 特性备份数据.除非存在备 ...

  6. angular乱码_号外!Angular 中文文档已同步翻译至 7.0

    从 Angular 7 发布(2018-10-18)至今已经过去四天了.四天的时间够干嘛的?只够我把它的文档(几乎)同步翻译完而已! 现在,它已经发布在了 https://angular.cn/doc ...

  7. rclone中文文档:常用命令大全

    rclone中文文档:常用命令大全 1. 概述 rclone是一个命令行程序,用于同步文件和目录,并支持网盘同步,可同步网盘包括如下: Amazon Drive Amazon S3 Backblaze ...

  8. springboot中文文档_登顶 Github 的 Spring Boot 仓库!艿艿写的最肝系列

    源码精品专栏 中文详细注释的开源项目 RPC 框架 Dubbo 源码解析 网络应用框架 Netty 源码解析 消息中间件 RocketMQ 源码解析 数据库中间件 Sharding-JDBC 和 My ...

  9. aspose excel中文文档_除了VBA,还有哪些编程语言可以操作Excel文件?

    Excel(Microsoft office)是现在最常用的办公软件,主要涉及电子表格制作.数据处理.报表输出展示以及更高端的还有金融建模等:我们知道,在需要批处理多个Excel工作表以及工作簿的时候 ...

最新文章

  1. 买不到回家的票,都是“抢票加速包”惹的祸?
  2. 号称下一代监控系统?
  3. anconda安装及opencv配置
  4. ubuntu18.04 VirtualBox 开启虚拟机出错 Kernel driver not installed (rc=-1908)
  5. 在程序员的道路上,义无反顾的努力,有思想的人,很多,好的想法,需要学习。(以此共勉)...
  6. 解决Springboot get请求是参数过长的情况
  7. 【Liunx】manjaro双系统安装(折腾)教程
  8. 个人站立会议第二阶段04
  9. 蓝桥杯 ADV-104算法提高 打水问题
  10. 【Linux-shell】shell脚本基础语法练习
  11. C语言编程QQ管理系统,c语言制作学生管理系统srrpqq67.doc
  12. kosbie的python课程视频_Python视频教程
  13. 上位机与欧姆龙PLC的Fins tcp通讯
  14. 关于 Swift Package Manager 的一些经验分享
  15. javaweb的问卷调查系统
  16. 昨天,我又“装”上了Windows 1.0
  17. [codeforces 1359A] Berland Poker 抽屉原理
  18. 易保全:览契约文化,传契约精神
  19. 计蒜客 蒜头君的训练室
  20. 别让用户发呆—设计中的防呆的6个策略

热门文章

  1. python matplotlib坐标轴设置的方法
  2. 猫猫新开通了新浪微博,欢迎小伙伴们来关注哟
  3. 非标常用焊接工艺应用
  4. hadoop 权威指南 HBase
  5. Fortran教程3:函数和子过程
  6. 正则匹配过滤空格字符串
  7. 陆奇:奇绩创坛选项目不看赛道而是看人,本质上是一个创业者社区
  8. 字符串长度的计算与字符串比较
  9. NekoHtml解析 html 文件
  10. 可视化讲解:什么是拉丁方阵问题?